@data-ui/xy-chart
Version:
A package of charts with standard x- and y- axes. https://williaster.github.io/data-ui
120 lines (106 loc) • 3.93 kB
JavaScript
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 React from 'react';
import PropTypes from 'prop-types';
import { BarStack } from '@vx/shape';
import { color } from '@data-ui/theme';
import { stackedBarSeriesDataShape } from '../utils/propShapes';
import { scaleTypeToScale } from '../utils/getScaleForAccessor';
import sharedSeriesProps from '../utils/sharedSeriesProps';
var propTypes = _extends({}, sharedSeriesProps, {
data: stackedBarSeriesDataShape.isRequired,
stackKeys: PropTypes.arrayOf(PropTypes.string).isRequired,
stackFills: PropTypes.arrayOf(PropTypes.string),
stroke: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
strokeWidth: PropTypes.oneOfType([PropTypes.func, PropTypes.number])
});
var defaultProps = {
stackFills: color.categories,
stroke: '#FFFFFF',
strokeWidth: 1
};
var x = function x(d) {
return d.x;
};
var noEventsStyles = {
pointerEvents: 'none'
};
var StackedBarSeries =
/*#__PURE__*/
function (_React$PureComponent) {
_inheritsLoose(StackedBarSeries, _React$PureComponent);
function StackedBarSeries() {
return _React$PureComponent.apply(this, arguments) || this;
}
var _proto = StackedBarSeries.prototype;
_proto.render = function render() {
var _this$props = this.props,
data = _this$props.data,
disableMouseEvents = _this$props.disableMouseEvents,
stackKeys = _this$props.stackKeys,
stackFills = _this$props.stackFills,
stroke = _this$props.stroke,
strokeWidth = _this$props.strokeWidth,
xScale = _this$props.xScale,
yScale = _this$props.yScale,
onClick = _this$props.onClick,
onMouseMove = _this$props.onMouseMove,
onMouseLeave = _this$props.onMouseLeave;
if (!xScale || !yScale) return null;
if (!xScale.bandwidth) {
// @todo figure this out/be more graceful
throw new Error("'StackedBarSeries' requires a 'band' type xScale");
}
var maxHeight = (yScale.range() || [0])[0];
var zScale = scaleTypeToScale.ordinal({
range: stackFills,
domain: stackKeys
});
return React.createElement(BarStack, {
style: disableMouseEvents ? noEventsStyles : null,
data: data,
keys: stackKeys,
height: maxHeight,
x: x,
xScale: xScale,
yScale: yScale,
zScale: zScale,
stroke: stroke,
strokeWidth: strokeWidth,
onClick: disableMouseEvents ? null : onMouseMove && function (d) {
return function (event) {
var datum = d.data,
seriesKey = d.key;
onClick({
event: event,
data: data,
datum: datum,
seriesKey: seriesKey,
color: zScale(seriesKey)
});
};
},
onMouseMove: disableMouseEvents ? null : onMouseMove && function (d) {
return function (event) {
var datum = d.data,
key = d.key;
onMouseMove({
event: event,
data: data,
datum: datum,
seriesKey: key,
color: zScale(key)
});
};
},
onMouseLeave: disableMouseEvents ? null : onMouseLeave && function () {
return onMouseLeave;
}
});
};
return StackedBarSeries;
}(React.PureComponent);
export { StackedBarSeries as default };
StackedBarSeries.propTypes = propTypes;
StackedBarSeries.defaultProps = defaultProps;
StackedBarSeries.displayName = 'StackedBarSeries';