-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample.tsx
More file actions
192 lines (179 loc) · 4.75 KB
/
Copy pathExample.tsx
File metadata and controls
192 lines (179 loc) · 4.75 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import React, { useEffect, useRef } from "react";
import * as echarts from "echarts/core";
import {
LineChart as EchartsLineChart,
LineSeriesOption,
} from "echarts/charts";
import {
TitleComponent,
TooltipComponent,
GridComponent,
LegendComponent,
} from "echarts/components";
import { CanvasRenderer } from "echarts/renderers";
import { palette } from "@leafygreen-ui/palette";
import { Theme } from "@leafygreen-ui/lib";
import {
borderRadius,
spacing,
fontFamilies,
color,
Variant,
State,
} from "@leafygreen-ui/tokens";
import { LineChartProps } from "./LineChart.types";
// Register the required components. By using separate imports, we can avoid
// importing the entire echarts library which will reduce the bundle size.
echarts.use([
TitleComponent,
TooltipComponent,
GridComponent,
LegendComponent,
EchartsLineChart,
CanvasRenderer,
]);
// TODO: This will be passed as a prop
const testSeries = [
{
name: "Dataset 1",
data: [
[new Date("2021-01-01T00:00:00"), 50],
[new Date("2021-01-01T01:00:00"), 60],
[new Date("2021-01-01T02:00:00"), 70],
],
},
{
name: "Dataset 2",
data: [
[new Date("2021-01-01T00:00:00"), 100],
[new Date("2021-01-01T01:00:00"), 40],
[new Date("2021-01-01T02:00:00"), 80],
],
},
];
// TODO: forwardRef
export function LineChart({}: LineChartProps) {
const chartRef = useRef(null);
useEffect(() => {
const chartInstance = echarts.init(chartRef.current);
const option = {
series: testSeries.map(
(seriesOption: LineSeriesOption): LineSeriesOption => ({
type: "line",
showSymbol: false,
clip: false,
...seriesOption,
})
),
animation: false, // Disabled to optimize performance
title: {
show: true,
text: "Chart Title", // TODO: This will be passed as a prop
padding: 20,
textStyle: {
color: palette.black,
fontFamily: fontFamilies.default,
},
},
toolbox: {
orient: "vertical",
// sizes set so that the toolbox is out of view
itemSize: 13,
top: 15,
right: -6,
feature: {
dataZoom: {
icon: {
zoom: "path://", // hack to remove zoom button which renders by default when zoom is enabled
back: "path://", // hack to remove restore button which renders by default when zoom is enabled
},
},
},
},
tooltip: {
trigger: "axis",
// TODO: Will set darkMode via prop in later PR
backgroundColor:
color[Theme.Light].background[Variant.InversePrimary][State.Default],
borderRadius: borderRadius[150],
},
xAxis: {
type: "time",
splitLine: { show: true },
axisLine: {
lineStyle: {
color: palette.gray.light2,
},
},
axisLabel: {
textStyle: {
color: palette.gray.dark1,
},
},
axisTick: {
show: false,
},
},
yAxis: {
type: "value",
splitLine: { show: true },
axisLine: {
lineStyle: {
color: palette.gray.light2,
},
},
axisLabel: {
textStyle: {
color: palette.gray.dark1,
},
},
axisTick: {
show: false,
},
},
grid: {
left: spacing[1000],
right: spacing[500],
top: spacing[1600],
bottom: spacing[500],
containLabel: true,
show: true,
},
};
chartInstance.setOption(option);
// This enables zooming by default without the need to click a zoom button in the toolbox.
chartInstance.dispatchAction({
type: "takeGlobalCursor",
key: "dataZoomSelect",
dataZoomSelectActive: true,
});
// ECharts does not automatically resize when the window resizes so we need to handle it manually.
const handleResize = () => {
chartInstance.resize();
};
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize);
chartInstance.dispose();
};
}, []);
return (
<div style={{ width: "100%", height: "100%" }}>
{/* Chart will fill 100% of width of the container on render */}
<div
ref={chartRef}
className="echart"
style={{
width: "100%",
height: "100%",
// TODO: Will set darkMode via prop in later PR
border: `1px solid ${
color[Theme.Light].border[Variant.Disabled][State.Default]
}`,
borderRadius: borderRadius[200],
}}
/>
</div>
);
}
LineChart.displayName = "LineChart";