-
Notifications
You must be signed in to change notification settings - Fork 3
/
MyChartComponent.tsx
47 lines (43 loc) · 1.14 KB
/
MyChartComponent.tsx
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
import {
Themes,
ChartXY,
PointLineAreaSeries,
emptyFill,
} 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
.addPointLineAreaSeries({
dataPattern: "ProgressiveX",
})
.setAreaFillStyle(emptyFill);
setChartState({ chart, lineSeries });
return () => {
chart.dispose();
};
}, [id, lc]);
useEffect(() => {
if (!chartState || !data || chartState.chart.isDisposed()) {
return;
}
chartState.lineSeries.setSamples({ yValues: data });
}, [chartState, data]);
return <div id={id} className="chart"></div>;
}