This repository was archived by the owner on May 13, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathAreaChartComponent.tsx
More file actions
201 lines (193 loc) · 4.78 KB
/
AreaChartComponent.tsx
File metadata and controls
201 lines (193 loc) · 4.78 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React from 'react';
import { Line } from 'react-chartjs-2';
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
PointElement,
LineElement,
Tooltip,
ChartOptions,
Filler,
} from 'chart.js';
import Annotation from 'chartjs-plugin-annotation';
import zoomPlugin from 'chartjs-plugin-zoom';
import { HumanizeNumber } from '@/utils/formatBytes';
import timeRangeUtils from '@/utils/timeRangeUtils';
const { makeTimeRangeLabel } = timeRangeUtils;
ChartJS.register(CategoryScale, LinearScale, PointElement, LineElement, Tooltip, Annotation, Filler, zoomPlugin);
interface ChartComponentProps {
graphData: any;
avgEventCount: number;
setTimeRangeFromGraph: (minute: string) => void;
hasData: boolean;
onZoomOrPanComplete: (startTime: string, endTime: string) => void;
}
const AreaChartComponent: React.FC<ChartComponentProps> = ({
graphData,
avgEventCount,
setTimeRangeFromGraph,
hasData,
onZoomOrPanComplete,
}) => {
const chartData = {
labels: graphData.map((item: any) => item.minute),
datasets: [
{
label: 'Events',
data: graphData.map((item: any) => item.events),
fill: true,
// backgroundColor: 'rgba(99, 102, 241, 0.5)',
borderColor: 'rgb(99, 102, 241)',
borderWidth: 1.25,
pointRadius: 2.5,
pointBorderWidth: 1,
pointBackgroundColor: 'rgb(99, 102, 241)',
tension: 0.4,
backgroundColor: (context: any) => {
const ctx = context.chart.ctx;
const gradient = ctx.createLinearGradient(0, 0, 0, context.chart.height);
gradient.addColorStop(0, 'rgba(99, 102, 241, 0.5)');
gradient.addColorStop(1, 'rgba(99, 102, 241, 0)');
return gradient;
},
},
],
};
const handleRangeComplete = (chart: any) => {
const { min, max } = chart.scales.x;
const startIndex = Math.floor(min);
const endIndex = Math.ceil(max);
// Get the time range for the zoomed area
if (startIndex >= 0 && endIndex < graphData.length) {
const startTime = graphData[startIndex].startTime;
const endTime = graphData[endIndex].endTime;
onZoomOrPanComplete(startTime, endTime);
}
};
const chartOptions: ChartOptions<'line'> = {
responsive: true,
maintainAspectRatio: false,
plugins: {
tooltip: {
position: 'nearest',
backgroundColor: 'white',
titleColor: 'black',
bodyColor: 'black',
footerColor: 'black',
borderColor: 'rgb(99, 102, 241)',
borderWidth: 1,
callbacks: {
title: (tooltipItems: any) => {
const index = tooltipItems[0].dataIndex;
const { startTime, endTime } = graphData[index];
return makeTimeRangeLabel(new Date(startTime), new Date(endTime));
},
label: (tooltipItem: any) => {
return `Events: ${new Intl.NumberFormat('en-US').format(tooltipItem.raw)}`;
},
// footer: (tooltipItems: any) => {
// const index = tooltipItems[0].dataIndex;
// const { aboveAvgPercent } = graphData[index];
// const isAboveAvg = aboveAvgPercent > 0;
// return `${isAboveAvg ? '+' : ''}${aboveAvgPercent}% ${
// isAboveAvg ? 'above' : 'below'
// } average in the given time-range`;
// },
},
},
annotation: {
annotations: {
avgLine: {
type: 'line',
yMin: avgEventCount,
yMax: avgEventCount,
borderColor: 'rgb(156, 163, 175)',
borderWidth: 1,
label: {
content: 'Avg',
position: 'start',
backgroundColor: 'transparent',
color: 'black',
font: {
size: 12,
},
},
},
},
},
zoom: {
limits: {
x: { min: 'original', max: 'original' },
y: { min: 'original', max: 'original' },
},
zoom: {
wheel: {
enabled: false,
},
pinch: {
enabled: true,
},
mode: 'x',
drag: {
enabled: true,
backgroundColor: 'rgba(99, 102, 241, 0.1)',
},
onZoomComplete: ({ chart }) => {
handleRangeComplete(chart);
},
},
},
},
scales: {
x: {
type: 'category',
display: false,
},
y: {
display: hasData,
ticks: {
count: 2,
callback: (value: string | number) => {
const numericValue = typeof value === 'number' ? value : parseFloat(value);
return HumanizeNumber(numericValue);
},
},
grid: {
drawTicks: false,
},
},
},
elements: {
line: {
borderWidth: 1.25,
},
point: {
radius: 2.5,
hitRadius: 6,
},
},
layout: {
padding: {
top: 0,
},
},
hover: {
mode: 'nearest',
axis: 'x',
intersect: false,
},
onClick: (_event, elements) => {
if (elements && elements.length > 0) {
const index = elements[0].index;
setTimeRangeFromGraph(graphData[index].minute);
}
},
};
return (
<div style={{ height: '100%', width: '100%', cursor: 'pointer' }}>
<Line data={chartData} options={chartOptions} />
</div>
);
};
export default AreaChartComponent;