-
-
Notifications
You must be signed in to change notification settings - Fork 575
/
chart.tsx
102 lines (93 loc) · 2.05 KB
/
chart.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
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
import {
BarElement,
CategoryScale,
Chart as ChartJS,
defaults,
Legend,
LinearScale,
Title,
Tooltip,
} from 'chart.js';
import { Bar } from 'react-chartjs-2';
import { memo } from 'react';
import { useDarkMode } from './use-dark-mode';
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend,
);
const data = [
{ framework: 'Million.js', val: 0.92 },
{ framework: 'Preact', val: 0.59 },
{ framework: 'React', val: 0.26 },
];
const options = {
plugins: {
legend: {
display: false,
},
},
indexAxis: 'y' as const,
scales: {
x: {
max: 1,
ticks: {
format: {
style: 'percent',
minimumFractionDigits: 0,
maximumFractionDigits: 1,
},
},
},
},
};
export function Chart() {
const isDarkMode = useDarkMode();
defaults.borderColor = isDarkMode ? '#2b2b2d' : '#e1e3eb';
defaults.color = isDarkMode ? '#e1e3eb' : '#545864';
defaults.font.size = 16;
defaults.font.family = 'Inter, sans-serif';
const color = isDarkMode ? '#54527b' : '#dcc9e8';
const backgroundColor = ['#b073d9', color, color];
return (
<div className="p-4 rounded-lg w-auto">
{isDarkMode !== null && (
<BarChart darkMode={isDarkMode} backgroundColor={backgroundColor} />
)}
</div>
);
}
interface BarChartProps {
backgroundColor: string[];
darkMode: boolean;
}
export const BarChart = memo(
function BarChart({ backgroundColor, darkMode: _darkMode }: BarChartProps) {
return (
<Bar
redraw
options={options}
data={{
labels: data.map((row) => row.framework),
datasets: [
{
label: '% of vanilla JavaScript',
data: data.map((row) => row.val),
backgroundColor,
barPercentage: 0.5,
},
],
}}
/>
);
},
(prevProps, nextProps) => {
return (
prevProps.backgroundColor[0] === nextProps.backgroundColor[0] &&
prevProps.darkMode === nextProps.darkMode
);
},
);