-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathindex.tsx
More file actions
105 lines (93 loc) · 2.34 KB
/
Copy pathindex.tsx
File metadata and controls
105 lines (93 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import React, { useImperativeHandle, forwardRef } from "react";
import { Chart } from "frappe-charts/dist/frappe-charts.min.esm";
type ChartType = "line" | "bar" | "axis-mixed" | "pie" | "percentage" | "heatmap";
type AxisMode = "span" | "tick";
type ChartData = {
labels?: Array<string>;
datasets?: Array<{
name?: string;
chartType?: ChartType;
values: Array<number>;
}>;
dataPoints?: { [x: string]: number };
start?: Date;
end?: Date;
};
type SelectEvent = {
label: string;
values: number[];
index: number;
};
type TooltipOptions = {
formatTooltipX?: (value: number) => any;
formatTooltipY?: (value: number) => any;
};
type Props = {
animate?: 0 | 1;
title?: string;
type?: ChartType;
data: ChartData;
height?: number;
colors?: Array<string>;
axisOptions?: {
xAxisMode?: AxisMode;
yAxisMode?: AxisMode;
xIsSeries?: 0 | 1;
shortenYAxisNumbers?: 0 | 1;
};
barOptions?: {
spaceRatio?: number;
stacked?: 0 | 1;
height?: number;
depth?: number;
};
lineOptions?: {
dotSize?: number;
hideLine?: 0 | 1;
hideDots?: 0 | 1;
heatline?: 0 | 1;
regionFill?: number;
areaFill?: number;
spline?: 0 | 1;
};
isNavigable?: boolean;
maxSlices?: number;
truncateLegends?: 0 | 1;
tooltipOptions?: TooltipOptions;
onDataSelect?: (event: SelectEvent) => void;
valuesOverPoints?: 0 | 1;
};
const ReactFrappeChart = forwardRef((props: Props, parentRef) => {
const ref = React.useRef<HTMLDivElement>(null);
const chart = React.useRef<any>(null);
const initialRender = React.useRef<boolean>(true);
const { onDataSelect } = props;
useImperativeHandle(parentRef, () => ({
export: () => {
if (chart && chart.current) {
chart.current.export();
}
},
}));
React.useEffect(() => {
chart.current = new Chart(ref.current, {
isNavigable: onDataSelect !== undefined,
...props,
});
if (onDataSelect) {
chart.current.parent.addEventListener("data-select", (e: SelectEvent & React.SyntheticEvent) => {
e.preventDefault();
onDataSelect(e);
});
}
}, []);
React.useEffect(() => {
if (initialRender.current) {
initialRender.current = false;
return;
}
chart.current.update(props.data);
}, [props.data]);
return <div ref={ref} />;
});
export default ReactFrappeChart;