-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathApp.tsx
More file actions
98 lines (86 loc) · 1.97 KB
/
App.tsx
File metadata and controls
98 lines (86 loc) · 1.97 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
import React, { useRef, useEffect } from 'react';
import {
Chart as ChartJS,
LinearScale,
CategoryScale,
BarElement,
PointElement,
LineElement,
Legend,
Tooltip,
} from 'chart.js';
import { Chart } from 'react-chartjs-2';
import { faker } from '@faker-js/faker';
ChartJS.register(
LinearScale,
CategoryScale,
BarElement,
PointElement,
LineElement,
Legend,
Tooltip
);
const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
export const data = {
labels,
datasets: [
{
type: 'line' as const,
label: 'Dataset 1',
borderColor: 'rgb(255, 99, 132)',
borderWidth: 2,
fill: false,
data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
},
{
type: 'bar' as const,
label: 'Dataset 2',
backgroundColor: 'rgb(75, 192, 192)',
data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
borderColor: 'white',
borderWidth: 2,
},
{
type: 'bar' as const,
label: 'Dataset 3',
backgroundColor: 'rgb(53, 162, 235)',
data: labels.map(() => faker.datatype.number({ min: -1000, max: 1000 })),
},
],
};
function triggerTooltip(chart: ChartJS | null) {
const tooltip = chart?.tooltip;
if (!tooltip) {
return;
}
if (tooltip.getActiveElements().length > 0) {
tooltip.setActiveElements([], { x: 0, y: 0 });
} else {
const { chartArea } = chart;
tooltip.setActiveElements(
[
{
datasetIndex: 0,
index: 2,
},
{
datasetIndex: 1,
index: 2,
},
],
{
x: (chartArea.left + chartArea.right) / 2,
y: (chartArea.top + chartArea.bottom) / 2,
}
);
}
chart.update();
}
export function App() {
const chartRef = useRef<ChartJS>(null);
useEffect(() => {
const chart = chartRef.current;
triggerTooltip(chart);
}, []);
return <Chart ref={chartRef} type='bar' data={data} />;
}