UNPKG

@data-ui/xy-chart

Version:

A package of charts with standard x- and y- axes. https://williaster.github.io/data-ui

83 lines (78 loc) 2.71 kB
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', x: 0, dy: '-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 HorizontalReferenceLine(_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 _xScale$range = xScale.range(), x0 = _xScale$range[0], x1 = _xScale$range[1]; var scaledRef = yScale(reference); var fromPoint = new Point({ x: x0, y: scaledRef }); var toPoint = new Point({ x: x1, y: scaledRef }); 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({ y: scaledRef }, defaultLabelProps, labelProps), label)); } HorizontalReferenceLine.propTypes = propTypes; HorizontalReferenceLine.defaultProps = defaultProps; HorizontalReferenceLine.displayName = 'HorizontalReferenceLine'; export default HorizontalReferenceLine;