react-financial-charts
Version:
React charts specific to finance.
72 lines • 2.32 kB
JavaScript
import { scaleLinear } from "d3-scale";
import * as PropTypes from "prop-types";
import * as React from "react";
import { find, noop, } from "./utils";
import { PureComponent } from "./utils/PureComponent";
export class Chart extends PureComponent {
constructor(props, context) {
super(props, context);
this.yScale = this.yScale.bind(this);
this.listener = this.listener.bind(this);
}
componentDidMount() {
const { id } = this.props;
const { subscribe } = this.context;
subscribe(`chart_${id}`, {
listener: this.listener,
});
}
componentWillUnmount() {
const { id } = this.props;
const { unsubscribe } = this.context;
unsubscribe(`chart_${id}`);
}
listener(type, moreProps, state, e) {
const { id, onContextMenu } = this.props;
if (type === "contextmenu") {
const { currentCharts } = moreProps;
if (currentCharts.indexOf(id) > -1) {
if (onContextMenu !== undefined) {
onContextMenu(moreProps, e);
}
}
}
}
yScale() {
const chartConfig = find(this.context.chartConfig, (each) => each.id === this.props.id);
return chartConfig.yScale.copy();
}
getChildContext() {
const { id: chartId } = this.props;
const chartConfig = find(this.context.chartConfig, (each) => each.id === chartId);
return {
chartId,
chartConfig,
};
}
render() {
const { origin } = find(this.context.chartConfig, (each) => each.id === this.props.id);
const [x, y] = origin;
return (React.createElement("g", { transform: `translate(${x}, ${y})` }, this.props.children));
}
}
Chart.defaultProps = {
id: 0,
origin: [0, 0],
padding: 0,
yScale: scaleLinear(),
flipYScale: false,
yPan: true,
yPanEnabled: false,
onContextMenu: noop,
};
Chart.contextTypes = {
chartConfig: PropTypes.array,
subscribe: PropTypes.func.isRequired,
unsubscribe: PropTypes.func.isRequired,
};
Chart.childContextTypes = {
chartConfig: PropTypes.object.isRequired,
chartId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
};
//# sourceMappingURL=Chart.js.map