@react-financial-charts/core
Version:
Core code for react-financial-charts
75 lines • 3.11 kB
JavaScript
import { GenericComponent } from "./GenericComponent";
import { isDefined } from "./utils";
import { ChartContext } from "./Chart";
const ALWAYS_TRUE_TYPES = ["drag", "dragend"];
export class GenericChartComponent extends GenericComponent {
constructor(props, context) {
super(props, context);
this.preCanvasDraw = this.preCanvasDraw.bind(this);
this.postCanvasDraw = this.postCanvasDraw.bind(this);
this.shouldTypeProceed = this.shouldTypeProceed.bind(this);
this.preEvaluate = this.preEvaluate.bind(this);
this.updateMoreProps = this.updateMoreProps.bind(this);
}
preCanvasDraw(ctx, moreProps) {
super.preCanvasDraw(ctx, moreProps);
ctx.save();
const { margin, ratio } = this.context;
const { chartConfig: { width, height, origin }, } = moreProps;
const canvasOriginX = 0.5 * ratio + origin[0] + margin.left;
const canvasOriginY = 0.5 * ratio + origin[1] + margin.top;
const { clip, edgeClip } = this.props;
ctx.setTransform(1, 0, 0, 1, 0, 0);
ctx.scale(ratio, ratio);
if (edgeClip) {
ctx.beginPath();
ctx.rect(-1, canvasOriginY - 10, width + margin.left + margin.right + 1, height + 20);
ctx.clip();
}
ctx.translate(canvasOriginX, canvasOriginY);
if (clip) {
ctx.beginPath();
ctx.rect(-1, -1, width + 1, height + 1);
ctx.clip();
}
}
postCanvasDraw(ctx, moreProps) {
super.postCanvasDraw(ctx, moreProps);
ctx.restore();
}
updateMoreProps(moreProps) {
super.updateMoreProps(moreProps);
const { chartConfig: chartConfigList } = moreProps;
if (chartConfigList && Array.isArray(chartConfigList)) {
const { chartId } = this.context;
const chartConfig = chartConfigList.find((each) => each.id === chartId);
this.moreProps.chartConfig = chartConfig;
}
if (isDefined(this.moreProps.chartConfig)) {
const { origin: [ox, oy], } = this.moreProps.chartConfig;
if (isDefined(moreProps.mouseXY)) {
const { mouseXY: [x, y], } = moreProps;
this.moreProps.mouseXY = [x - ox, y - oy];
}
if (isDefined(moreProps.startPos)) {
const { startPos: [x, y], } = moreProps;
this.moreProps.startPos = [x - ox, y - oy];
}
}
}
preEvaluate( /* type, moreProps */) {
///
}
shouldTypeProceed(type, moreProps) {
if ((type === "mousemove" || type === "click") && this.props.disablePan) {
return true;
}
if (ALWAYS_TRUE_TYPES.indexOf(type) === -1 && isDefined(moreProps) && isDefined(moreProps.currentCharts)) {
return moreProps.currentCharts.indexOf(this.context.chartId) > -1;
}
return true;
}
}
GenericChartComponent.defaultProps = GenericComponent.defaultProps;
GenericChartComponent.contextType = ChartContext;
//# sourceMappingURL=GenericChartComponent.js.map