-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMyChartComponent.tsx
More file actions
43 lines (39 loc) · 1.11 KB
/
MyChartComponent.tsx
File metadata and controls
43 lines (39 loc) · 1.11 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
import { Themes, ChartXY, PointLineAreaSeries } from "@lightningchart/lcjs";
import { useEffect, useState, useContext, useId } from "react";
import { LCContext } from "../LC";
export function MyChartComponent(props: { data: number[] }) {
const { data } = props;
const id = useId();
const lc = useContext(LCContext);
const [chartState, setChartState] = useState<{
chart: ChartXY;
lineSeries: PointLineAreaSeries;
}>();
useEffect(() => {
const container = document.getElementById(id) as HTMLDivElement;
if (!container || !lc) {
return;
}
const chart = lc.ChartXY({
theme: Themes.light,
container,
});
const lineSeries = chart.addLineSeries({
schema: {
y: { pattern: null },
x: { auto: true },
},
});
setChartState({ chart, lineSeries });
return () => {
chart.dispose();
};
}, [id, lc]);
useEffect(() => {
if (!chartState || !data || chartState.chart.isDisposed()) {
return;
}
chartState.lineSeries.setSamples({ y: data });
}, [chartState, data]);
return <div id={id} className="chart"></div>;
}