|
1 | 1 | import './graph.scss';
|
2 | 2 |
|
3 | 3 | // import dependencies
|
4 |
| -import 'chartjs-adapter-dayjs-3'; |
5 | 4 | 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, |
15 | 10 | Tooltip,
|
16 |
| - CategoryScale, |
17 |
| -} from 'chart.js'; |
18 |
| -import { Chart } from 'react-chartjs-2'; |
| 11 | + ResponsiveContainer, |
| 12 | +} from 'recharts'; |
| 13 | +import dayjs from 'dayjs'; |
19 | 14 |
|
20 | 15 | // import local files
|
21 | 16 | import useLocalStorageContext from '../../../hooks/useLocalstorage';
|
22 | 17 | import GraphMenu from './menu';
|
23 | 18 | import useGraphStatus from '../../../hooks/useGraphStatus';
|
24 | 19 | import { fullMonitorPropType } from '../../../../shared/utils/propTypes';
|
25 | 20 |
|
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 |
| - |
39 | 21 | const MonitorGraph = ({ monitor }) => {
|
40 | 22 | const { dateformat, timeformat, theme } = useLocalStorageContext();
|
41 | 23 |
|
42 | 24 | const { statusType, statusHeartbeats, setStatusType } =
|
43 | 25 | useGraphStatus(monitor);
|
44 | 26 |
|
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 | + |
47 | 31 | const gridColor =
|
48 | 32 | theme.type === 'light' ? 'rgba(0,0,0,0.1)' : 'rgba(255,255,255,0.1)';
|
49 | 33 |
|
50 | 34 | return (
|
51 | 35 | <div className="monitor-chart-container">
|
52 | 36 | <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> |
108 | 81 | </div>
|
109 | 82 | );
|
110 | 83 | };
|
|
0 commit comments