@data-ui/xy-chart
Version:
A package of charts with standard x- and y- axes. https://williaster.github.io/data-ui
84 lines (79 loc) • 2.77 kB
JavaScript
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
import PropTypes from 'prop-types';
import React from 'react';
import { Group } from '@vx/group';
import { Line } from '@vx/shape';
import { Point } from '@vx/point';
import { Text } from '@vx/text';
import { color, svgLabel } from '@data-ui/theme';
var baseLabel = svgLabel.baseLabel;
export var defaultLabelProps = _extends({}, baseLabel, {
textAnchor: 'start',
verticalAnchor: 'middle',
stroke: '#fff',
strokeWidth: 2,
paintOrder: 'stroke',
y: 0,
dx: '0.4em'
});
export var propTypes = {
label: PropTypes.node,
labelProps: PropTypes.objectOf(PropTypes.oneOfType([PropTypes.number, PropTypes.string])),
reference: PropTypes.oneOfType([PropTypes.number, PropTypes.string, PropTypes.object]).isRequired,
stroke: PropTypes.string,
strokeDasharray: PropTypes.string,
strokeLinecap: PropTypes.oneOf(['butt', 'square', 'round', 'inherit']),
strokeWidth: PropTypes.number,
xScale: PropTypes.func,
yScale: PropTypes.func
};
var defaultProps = {
label: null,
labelProps: defaultLabelProps,
stroke: color.darkGray,
strokeDasharray: null,
strokeLinecap: 'round',
strokeWidth: 1,
xScale: null,
yScale: null
};
function VerticalReferenceLine(_ref) {
var label = _ref.label,
labelProps = _ref.labelProps,
reference = _ref.reference,
stroke = _ref.stroke,
strokeDasharray = _ref.strokeDasharray,
strokeLinecap = _ref.strokeLinecap,
strokeWidth = _ref.strokeWidth,
xScale = _ref.xScale,
yScale = _ref.yScale;
if (!xScale || !yScale) return null;
var _yScale$range = yScale.range(),
y0 = _yScale$range[0],
y1 = _yScale$range[1];
var scaledRef = //
xScale(reference) + (xScale.offset || xScale.bandwidth && xScale.bandwidth() / 2 || 0);
var fromPoint = new Point({
x: scaledRef,
y: y0
});
var toPoint = new Point({
x: scaledRef,
y: y1
});
return React.createElement(Group, null, React.createElement(Line, {
from: fromPoint,
to: toPoint,
stroke: stroke,
strokeDasharray: strokeDasharray,
strokeLinecap: strokeLinecap,
strokeWidth: strokeWidth,
vectorEffect: "non-scaling-stroke"
}), !!label && React.createElement(Text, _extends({
x: scaledRef
}, defaultLabelProps, labelProps), label));
}
VerticalReferenceLine.propTypes = propTypes;
VerticalReferenceLine.defaultProps = defaultProps;
VerticalReferenceLine.displayName = 'VerticalReferenceLine';
export default VerticalReferenceLine;