@data-ui/xy-chart
Version:
A package of charts with standard x- and y- axes. https://williaster.github.io/data-ui
164 lines (152 loc) • 5.91 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 { svgLabel, color } from '@data-ui/theme';
import { FocusBlurHandler } from '@data-ui/shared';
import { Group } from '@vx/group';
import { Text } from '@vx/text';
import PropTypes from 'prop-types';
import React from 'react';
import { callOrValue, isDefined } from '../utils/chartUtils';
import GlyphDotComponent from '../glyph/GlyphDotComponent';
import { pointSeriesDataShape } from '../utils/propShapes';
import sharedSeriesProps from '../utils/sharedSeriesProps';
export var propTypes = _extends({}, sharedSeriesProps, {
data: pointSeriesDataShape.isRequired,
defaultLabelProps: PropTypes.object,
// eslint-disable-line react/forbid-prop-types
pointComponent: PropTypes.oneOfType([PropTypes.func, PropTypes.element]),
fill: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
fillOpacity: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
renderLabel: PropTypes.func,
stroke: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
strokeWidth: PropTypes.oneOfType([PropTypes.func, PropTypes.number]),
strokeDasharray: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
size: PropTypes.oneOfType([PropTypes.func, PropTypes.number])
});
var baseLabel = svgLabel.baseLabel;
export var defaultLabelProps = _extends({}, baseLabel, {
textAnchor: 'start',
verticalAnchor: 'start',
dx: '0.25em',
dy: '0.25em',
pointerEvents: 'none',
stroke: '#fff',
strokeWidth: 2,
paintOrder: 'stroke',
fontSize: 12
});
export var defaultProps = {
defaultLabelProps: defaultLabelProps,
/* eslint-disable react/prop-types */
renderLabel: function renderLabel(_ref) {
var datum = _ref.datum,
labelProps = _ref.labelProps;
return datum.label ? React.createElement(Text, labelProps, datum.label) : null;
},
/* eslint-enable react/prop-types */
pointComponent: GlyphDotComponent,
size: 4,
fill: color.default,
fillOpacity: 0.8,
stroke: '#FFFFFF',
strokeDasharray: null,
strokeWidth: 1
};
var noEventsStyles = {
pointerEvents: 'none'
};
var PointSeries =
/*#__PURE__*/
function (_React$PureComponent) {
_inheritsLoose(PointSeries, _React$PureComponent);
function PointSeries() {
return _React$PureComponent.apply(this, arguments) || this;
}
var _proto = PointSeries.prototype;
_proto.render = function render() {
var _this$props = this.props,
data = _this$props.data,
labelProps = _this$props.defaultLabelProps,
disableMouseEvents = _this$props.disableMouseEvents,
fill = _this$props.fill,
fillOpacity = _this$props.fillOpacity,
renderLabel = _this$props.renderLabel,
size = _this$props.size,
stroke = _this$props.stroke,
strokeWidth = _this$props.strokeWidth,
strokeDasharray = _this$props.strokeDasharray,
xScale = _this$props.xScale,
yScale = _this$props.yScale,
onClick = _this$props.onClick,
onMouseMove = _this$props.onMouseMove,
onMouseLeave = _this$props.onMouseLeave,
pointComponent = _this$props.pointComponent;
if (!xScale || !yScale) return null;
var Labels = [];
return React.createElement(Group, {
style: disableMouseEvents ? noEventsStyles : null
}, data.map(function (d, i) {
var xVal = d.x;
var yVal = d.y;
var defined = isDefined(xVal) && isDefined(yVal);
var x = xScale(xVal);
var y = yScale(yVal);
var computedFill = d.fill || callOrValue(fill, d, i);
var key = d.x + "-" + i;
var computedSize = d.size || callOrValue(size, d, i);
var computedFillOpacity = d.fillOpacity || callOrValue(fillOpacity, d, i);
var computedStroke = d.stroke || callOrValue(stroke, d, i);
var computedStrokeWidth = d.strokeWidth || callOrValue(strokeWidth, d, i);
var computedStrokeDasharray = d.strokeDasharray || callOrValue(strokeDasharray, d, i);
if (defined && renderLabel) {
var Label = renderLabel({
datum: d,
index: i,
labelProps: _extends({
key: key
}, labelProps, {
x: x,
y: y
})
});
if (Label) Labels.push(Label);
}
var props = {
x: x,
y: y,
size: computedSize,
fill: computedFill,
fillOpacity: computedFillOpacity,
stroke: computedStroke,
strokeWidth: computedStrokeWidth,
strokeDasharray: computedStrokeDasharray,
onClick: disableMouseEvents ? null : onClick,
onMouseMove: disableMouseEvents ? null : onMouseMove,
onMouseLeave: disableMouseEvents ? null : onMouseLeave,
data: data,
datum: d
};
return defined && React.createElement(FocusBlurHandler, {
key: key,
xlinkHref: "#",
onBlur: disableMouseEvents ? null : props.onMouseLeave,
onFocus: disableMouseEvents ? null : function (event) {
onMouseMove({
event: event,
data: data,
datum: d,
color: computedFill,
index: i
});
}
}, React.createElement(pointComponent, props));
}), Labels.map(function (Label) {
return Label;
}));
};
return PointSeries;
}(React.PureComponent);
export { PointSeries as default };
PointSeries.propTypes = propTypes;
PointSeries.defaultProps = defaultProps;
PointSeries.displayName = 'PointSeries';