-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLegendItem.tsx
45 lines (40 loc) · 1.05 KB
/
LegendItem.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
import { ChartDataset } from "chart.js";
import React, { useState, CSSProperties } from "react";
import { ChartJSOrUndefined } from "react-chartjs-2/dist/types";
import checkmark from "./images/checkmark.svg";
export function LegendItem({
dataset,
chartRef,
}: {
dataset: ChartDataset<"line">;
chartRef: ChartJSOrUndefined<"line">;
}): React.ReactElement {
const [visible, setVisible] = useState(!dataset.hidden);
if (!dataset) {
return <></>;
}
const bgColor = visible ? dataset.borderColor!.toString() : "";
const style: CSSProperties = {
backgroundColor: bgColor,
borderColor: dataset.borderColor!.toString(),
};
return (
<label
onClick={() => {
if (visible) {
dataset.hidden = true;
setVisible(false);
} else {
dataset.hidden = false;
setVisible(true);
}
chartRef?.update();
}}
>
<span style={style} className="checkbox">
{visible && <img src={checkmark} alt="" />}
</span>
{dataset?.label}
</label>
);
}