victory-chart
Version:
Chart Component for Victory
189 lines (173 loc) • 6.51 kB
JavaScript
import { partialRight } from "lodash";
import React, { PropTypes } from "react";
import LineHelpers from "./helper-methods";
import {
PropTypes as CustomPropTypes, Helpers, VictoryTransition, VictoryLabel, addEvents,
VictoryContainer, VictoryTheme, DefaultTransitions, Curve, VictoryClipContainer,
Data, Domain
} from "victory-core";
const fallbackProps = {
width: 450,
height: 300,
padding: 50,
interpolation: "linear"
};
class VictoryLine extends React.Component {
static displayName = "VictoryLine";
static role = "line";
static defaultTransitions = DefaultTransitions.continuousTransitions();
static continuous = true;
static propTypes = {
animate: PropTypes.object,
categories: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.shape({
x: PropTypes.arrayOf(PropTypes.string), y: PropTypes.arrayOf(PropTypes.string)
})
]),
containerComponent: PropTypes.element,
data: PropTypes.array,
domainPadding: PropTypes.oneOfType([
PropTypes.shape({
x: PropTypes.oneOfType([ PropTypes.number, CustomPropTypes.domain ]),
y: PropTypes.oneOfType([ PropTypes.number, CustomPropTypes.domain ])
}),
PropTypes.number
]),
dataComponent: PropTypes.element,
domain: PropTypes.oneOfType([
CustomPropTypes.domain,
PropTypes.shape({ x: CustomPropTypes.domain, y: CustomPropTypes.domain })
]),
events: PropTypes.arrayOf(PropTypes.shape({
target: PropTypes.oneOf(["data", "labels", "parent"]),
eventKey: PropTypes.oneOf(["all"]),
eventHandlers: PropTypes.object
})),
groupComponent: PropTypes.element,
height: CustomPropTypes.nonNegative,
interpolation: PropTypes.oneOf([
"basis", "bundle", "cardinal", "catmullRom", "linear", "monotoneX",
"monotoneY", "natural", "radial", "step", "stepAfter", "stepBefore"
]),
label: PropTypes.string,
labelComponent: PropTypes.element,
name: PropTypes.string,
padding: PropTypes.oneOfType([
PropTypes.number,
PropTypes.shape({
top: PropTypes.number, bottom: PropTypes.number,
left: PropTypes.number, right: PropTypes.number
})
]),
samples: CustomPropTypes.nonNegative,
scale: PropTypes.oneOfType([
CustomPropTypes.scale,
PropTypes.shape({ x: CustomPropTypes.scale, y: CustomPropTypes.scale })
]),
sharedEvents: PropTypes.shape({
events: PropTypes.array,
getEventState: PropTypes.func
}),
sortKey: PropTypes.oneOfType([
PropTypes.func,
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
PropTypes.string,
PropTypes.arrayOf(PropTypes.string)
]),
standalone: PropTypes.bool,
style: PropTypes.shape({
parent: PropTypes.object, data: PropTypes.object, labels: PropTypes.object
}),
theme: PropTypes.object,
width: CustomPropTypes.nonNegative,
x: PropTypes.oneOfType([
PropTypes.func,
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
PropTypes.string,
PropTypes.arrayOf(PropTypes.string)
]),
y: PropTypes.oneOfType([
PropTypes.func,
CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]),
PropTypes.string,
PropTypes.arrayOf(PropTypes.string)
])
};
static defaultProps = {
samples: 50,
scale: "linear",
standalone: true,
sortKey: "x",
dataComponent: <Curve/>,
labelComponent: <VictoryLabel/>,
containerComponent: <VictoryContainer/>,
groupComponent: <VictoryClipContainer/>,
theme: VictoryTheme.grayscale
};
static getDomain = Domain.getDomain.bind(Domain);
static getData = Data.getData.bind(Data);
static getBaseProps = partialRight(LineHelpers.getBaseProps.bind(LineHelpers),
fallbackProps);
static getScale = partialRight(LineHelpers.getScale.bind(LineHelpers),
fallbackProps);
static expectedComponents = [
"dataComponent", "labelComponent", "groupComponent", "containerComponent"
];
renderData(props) { // eslint-disable-line max-statements
const { dataComponent, labelComponent, groupComponent } = props;
const dataComponents = [];
const labelComponents = [];
for (let index = 0, len = this.dataKeys.length; index < len; index++) {
const dataProps = this.getComponentProps(dataComponent, "data", index);
dataComponents[index] = React.cloneElement(dataComponent, dataProps);
const labelProps = this.getComponentProps(labelComponent, "labels", index);
if (labelProps && labelProps.text !== undefined && labelProps.text !== null) {
labelComponents[index] = React.cloneElement(labelComponent, labelProps);
}
}
return labelComponents.length > 0 ?
React.cloneElement(groupComponent, {}, ...dataComponents, ...labelComponents) :
dataComponents;
}
renderContainer(props, group) {
const { containerComponent } = props;
const parentProps = this.getComponentProps(containerComponent, "parent", "parent");
return React.cloneElement(containerComponent, parentProps, group);
}
renderGroup(children, style) {
return React.cloneElement(
this.props.groupComponent,
{ role: "presentation", style},
children
);
}
shouldAnimate() {
return !!this.props.animate;
}
render() {
const { role } = this.constructor;
const props = Helpers.modifyProps(this.props, fallbackProps, role);
const { animate, style, standalone, theme } = props;
if (this.shouldAnimate()) {
// Do less work by having `VictoryAnimation` tween only values that
// make sense to tween. In the future, allow customization of animated
// prop whitelist/blacklist?
// TODO: extract into helper
const whitelist = [
"data", "domain", "height", "padding", "samples",
"style", "width", "x", "y"
];
return (
<VictoryTransition animate={animate} animationWhitelist={whitelist}>
{React.createElement(this.constructor, props)}
</VictoryTransition>
);
}
const styleObject = theme && theme.line && theme.line.style ? theme.line.style : {};
const baseStyles = Helpers.getStyles(style, styleObject, "auto", "100%");
const group = this.renderGroup(this.renderData(props), baseStyles.parent);
return standalone ? this.renderContainer(props, group) : group;
}
}
export default addEvents(VictoryLine);