UNPKG

@data-ui/xy-chart

Version:

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

130 lines (113 loc) 4.34 kB
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; } 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 { color } from '@data-ui/theme'; import { Threshold } from '@vx/threshold'; import PropTypes from 'prop-types'; import React, { Children } from 'react'; import interpolatorLookup from '../utils/interpolatorLookup'; import { interpolationShape } from '../utils/propShapes'; import sharedSeriesProps from '../utils/sharedSeriesProps'; var propTypes = _extends({}, sharedSeriesProps, { children: PropTypes.node.isRequired, // AreaSeries type interpolation: interpolationShape }); var defaultProps = { interpolation: 'monotoneX' }; var DEFAULT_OPACITY = 0.4; var getX = function getX(d) { return d.x; }; var getY0 = function getY0(d) { return d.y0; }; var getY1 = function getY1(d) { return d.y1; }; var AreaDifferenceSeries = /*#__PURE__*/ function (_React$PureComponent) { _inheritsLoose(AreaDifferenceSeries, _React$PureComponent); function AreaDifferenceSeries() { return _React$PureComponent.apply(this, arguments) || this; } var _proto = AreaDifferenceSeries.prototype; _proto.render = function render() { var _this$props = this.props, disableMouseEvents = _this$props.disableMouseEvents, interpolation = _this$props.interpolation, xScale = _this$props.xScale, yScale = _this$props.yScale, onClick = _this$props.onClick, onMouseMove = _this$props.onMouseMove, onMouseLeave = _this$props.onMouseLeave, children = _this$props.children, margin = _this$props.margin; if (!xScale || !yScale) return null; var childArray = Children.toArray(children); var child1 = childArray[0], child2 = childArray[1]; if (childArray.length !== 2 || child1.type.displayName !== 'AreaSeries' || child2.type.displayName !== 'AreaSeries') { console.warn('AreaDifferenceSeries expects exactly two AreaSeries children'); return null; } var _child1$props = child1.props, data1 = _child1$props.data, fill1 = _child1$props.fill, opacity1 = _child1$props.fillOpacity; var _child2$props = child2.props, data2 = _child2$props.data, fill2 = _child2$props.fill, opacity2 = _child2$props.fillOpacity; if (data1.length !== data2.length) { console.warn('AreaDifferenceSeries children should have the same data length'); return null; } var curve = interpolatorLookup[interpolation] || interpolatorLookup.monotoneX; var yExtent = yScale.range(); var mergedData = data1.map(function (d, i) { return { x: d.x, y0: d.y, y1: data2[i].y }; }); return React.createElement("g", null, React.createElement(Threshold, { data: mergedData, x: getX, y0: getY0, y1: getY1, xScale: xScale, yScale: yScale, clipAboveTo: Math.min.apply(Math, yExtent), clipBelowTo: Math.max.apply(Math, yExtent), curve: curve, aboveAreaProps: { fill: fill1 || color.categories[0], fillOpacity: opacity1 || DEFAULT_OPACITY }, belowAreaProps: { fill: fill2 || color.categories[0], fillOpacity: opacity2 || DEFAULT_OPACITY } }), childArray.map(function (Child) { return React.cloneElement(Child, { xScale: xScale, yScale: yScale, onClick: onClick, onMouseMove: onMouseMove, onMouseLeave: onMouseLeave, interpolation: interpolation, disableMouseEvents: Child.props.disableMouseEvents || disableMouseEvents, fill: 'transparent', margin: margin }); })); }; return AreaDifferenceSeries; }(React.PureComponent); export { AreaDifferenceSeries as default }; AreaDifferenceSeries.propTypes = propTypes; AreaDifferenceSeries.defaultProps = defaultProps; AreaDifferenceSeries.displayName = 'AreaDifferenceSeries';