Skip to content

Commit ec6f5b3

Browse files
committed
Moves from chart.js to recharts
1 parent 392ac24 commit ec6f5b3

File tree

4 files changed

+377
-134
lines changed

4 files changed

+377
-134
lines changed

app/components/monitor/graph/graph.scss

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
@use '../../../styles/breakpoints.scss' as *;
22
@use '../../../styles/pxToRem.scss' as *;
33

4+
.monitor-chart-content {
5+
width: 100%;
6+
height: 425px;
7+
display: flex;
8+
justify-content: center;
9+
align-items: center;
10+
padding: 1rem 1.75rem;
11+
}
12+
413
.monitor-chart-container {
514
background-color: var(--accent-900);
615
border-radius: pxToRem(16);
@@ -21,7 +30,23 @@
2130
display: none;
2231
}
2332

33+
@include laptop {
34+
.monitor-chart-content {
35+
height: 325px;
36+
}
37+
}
38+
39+
@include tablet {
40+
.monitor-chart-content {
41+
height: 280px;
42+
}
43+
}
44+
2445
@include mobile {
46+
.monitor-chart-content {
47+
height: 250px;
48+
}
49+
2550
.monitor-chart-buttons-container {
2651
display: none;
2752
}

app/components/monitor/graph/index.jsx

Lines changed: 56 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,83 @@
11
import './graph.scss';
22

33
// import dependencies
4-
import 'chartjs-adapter-dayjs-3';
54
import {
6-
BarController,
7-
BarElement,
8-
Chart as ChartJs,
9-
Filler,
10-
LinearScale,
11-
LineController,
12-
LineElement,
13-
PointElement,
14-
TimeScale,
5+
AreaChart,
6+
Area,
7+
XAxis,
8+
YAxis,
9+
CartesianGrid,
1510
Tooltip,
16-
CategoryScale,
17-
} from 'chart.js';
18-
import { Chart } from 'react-chartjs-2';
11+
ResponsiveContainer,
12+
} from 'recharts';
13+
import dayjs from 'dayjs';
1914

2015
// import local files
2116
import useLocalStorageContext from '../../../hooks/useLocalstorage';
2217
import GraphMenu from './menu';
2318
import useGraphStatus from '../../../hooks/useGraphStatus';
2419
import { fullMonitorPropType } from '../../../../shared/utils/propTypes';
2520

26-
ChartJs.register(
27-
LineController,
28-
BarController,
29-
LineElement,
30-
PointElement,
31-
TimeScale,
32-
BarElement,
33-
LinearScale,
34-
Tooltip,
35-
Filler,
36-
CategoryScale
37-
);
38-
3921
const MonitorGraph = ({ monitor }) => {
4022
const { dateformat, timeformat, theme } = useLocalStorageContext();
4123

4224
const { statusType, statusHeartbeats, setStatusType } =
4325
useGraphStatus(monitor);
4426

45-
const labels = statusHeartbeats.map((heartbeat = {}) => heartbeat.date);
46-
const data = statusHeartbeats.map((heartbeat = {}) => heartbeat.latency);
27+
const data = statusHeartbeats.map((heartbeat = {}) => {
28+
return { Latency: heartbeat.latency, time: heartbeat.date };
29+
});
30+
4731
const gridColor =
4832
theme.type === 'light' ? 'rgba(0,0,0,0.1)' : 'rgba(255,255,255,0.1)';
4933

5034
return (
5135
<div className="monitor-chart-container">
5236
<GraphMenu statusType={statusType} setStatusType={setStatusType} />
53-
<Chart
54-
type={'line'}
55-
data={{
56-
labels,
57-
datasets: [
58-
{
59-
data,
60-
borderColor: '#3ba55c',
61-
backgroundColor: '#3ba55d28',
62-
yAxisID: 'y',
63-
fill: 'origin',
64-
tension: 0.15,
65-
},
66-
],
67-
}}
68-
options={{
69-
responsive: true,
70-
maintainAspectRatio: false,
71-
onResize: (chart) => {
72-
if (window.innerWidth < 576) {
73-
chart.canvas.style.height = '225px';
74-
} else if (window.innerWidth < 768) {
75-
chart.canvas.style.height = '275px';
76-
} else if (window.innerWidth < 1200) {
77-
chart.canvas.style.height = '300px';
78-
} else if (window.innerWidth < 1920) {
79-
chart.canvas.style.height = '320px';
80-
} else {
81-
chart.canvas.style.height = '400px';
82-
}
83-
},
84-
layout: { padding: { left: 10, right: 30, top: 30, bottom: 10 } },
85-
elements: { point: { radius: 0, hitRadius: 100 } },
86-
scales: {
87-
x: {
88-
type: 'time',
89-
time: {
90-
minUnit: 'minute',
91-
round: 'second',
92-
tooltipFormat: `${dateformat} ${timeformat}`,
93-
displayFormats: { minute: 'HH:mm', hour: 'MM-DD HH:mm' },
94-
},
95-
ticks: { maxRotation: 0, autoSkipPadding: 30 },
96-
grid: { color: gridColor, offset: false },
97-
},
98-
y: {
99-
type: 'linear',
100-
title: { display: true, text: 'respTime (ms)' },
101-
offset: true,
102-
grid: { color: gridColor },
103-
min: 0,
104-
},
105-
},
106-
}}
107-
/>
37+
<div className="monitor-chart-content">
38+
<ResponsiveContainer>
39+
<AreaChart data={data}>
40+
<XAxis
41+
type="category"
42+
dataKey="time"
43+
style={{ fill: 'var(--accent-200)' }}
44+
tick={{ fontSize: 12 }}
45+
tickFormatter={(date) => dayjs(date).format(timeformat)}
46+
/>
47+
<YAxis
48+
label={{
49+
value: 'respTime (ms)',
50+
angle: -90,
51+
position: 'insideLeft',
52+
style: { textAnchor: 'middle' },
53+
}}
54+
style={{ fill: 'var(--accent-200)' }}
55+
/>
56+
<CartesianGrid vertical={false} stroke={gridColor} />
57+
<Tooltip
58+
formatter={(value) => `${value} ms`}
59+
labelFormatter={(value) =>
60+
dayjs(value).format(`${dateformat} - ${timeformat}`)
61+
}
62+
separator=": "
63+
contentStyle={{
64+
backgroundColor: 'var(--accent-800)',
65+
border: '1px solid var(--accent-600)',
66+
padding: '10px',
67+
borderRadius: '5px',
68+
fontSize: '0.8rem',
69+
color: 'var(--font-color)',
70+
}}
71+
/>
72+
<Area
73+
type="monotone"
74+
dataKey="Latency"
75+
stroke="var(--primary-400)"
76+
fill="var(--primary-500)"
77+
/>
78+
</AreaChart>
79+
</ResponsiveContainer>
80+
</div>
10881
</div>
10982
);
11083
};

0 commit comments

Comments
 (0)