Files
Checkmate/Client/src/Pages/Monitors/Details/Charts/index.jsx
T

188 lines
4.9 KiB
React
Raw Normal View History

2024-09-03 20:00:48 -04:00
import { useTheme } from "@emotion/react";
import {
BarChart,
Bar,
XAxis,
CartesianGrid,
ResponsiveContainer,
2024-09-04 12:57:08 -04:00
Cell,
2024-09-03 20:00:48 -04:00
} from "recharts";
import { formatDate } from "../../../../Utils/timeUtils";
2024-09-04 12:57:08 -04:00
import { useState } from "react";
2024-09-03 20:00:48 -04:00
2024-09-04 12:57:08 -04:00
const CustomLabels = ({
x,
width,
height,
firstDataPoint,
lastDataPoint,
type,
}) => {
2024-09-03 20:00:48 -04:00
let options = {
month: "short",
year: undefined,
hour: undefined,
minute: undefined,
};
2024-09-04 12:57:08 -04:00
if (type === "day") delete options.hour;
2024-09-03 20:00:48 -04:00
return (
<>
2024-09-04 12:57:08 -04:00
<text x={x} y={height} dy={-3} textAnchor="start">
{formatDate(new Date(firstDataPoint.time), options)}
2024-09-03 20:00:48 -04:00
</text>
2024-09-04 12:57:08 -04:00
<text x={width} y={height} dy={-3} textAnchor="end">
{formatDate(new Date(lastDataPoint.time), options)}
2024-09-03 20:00:48 -04:00
</text>
</>
);
};
2024-09-04 12:57:08 -04:00
export const UpBarChart = ({ data, type, onBarHover }) => {
2024-09-03 20:00:48 -04:00
const theme = useTheme();
2024-09-04 12:57:08 -04:00
const [chartHovered, setChartHovered] = useState(false);
const [hoveredBarIndex, setHoveredBarIndex] = useState(null);
2024-09-04 16:41:23 -04:00
const getColorRange = (uptime) => {
return uptime > 80
? { main: theme.palette.success.main, light: theme.palette.success.light }
: uptime > 50
? { main: theme.palette.warning.main, light: theme.palette.warning.light }
: { main: theme.palette.error.text, light: theme.palette.error.light };
};
2024-09-03 20:00:48 -04:00
return (
2024-09-04 12:57:08 -04:00
<ResponsiveContainer width="100%" height={155}>
<BarChart
width="100%"
height="100%"
data={data}
onMouseEnter={() => {
setChartHovered(true);
2024-09-04 16:41:23 -04:00
onBarHover({ time: null, totalChecks: 0, uptimePercentage: 0 });
2024-09-04 12:57:08 -04:00
}}
onMouseLeave={() => {
setChartHovered(false);
setHoveredBarIndex(null);
onBarHover(null);
}}
>
2024-09-03 20:00:48 -04:00
<XAxis
stroke={theme.palette.border.dark}
height={15}
tick={false}
label={
<CustomLabels
x={0}
y={0}
width="100%"
height="100%"
firstDataPoint={data[0]}
lastDataPoint={data[data.length - 1]}
2024-09-04 12:57:08 -04:00
type={type}
2024-09-03 20:00:48 -04:00
/>
}
/>
<Bar
dataKey="totalChecks"
2024-09-04 16:41:23 -04:00
maxBarSize={7}
2024-09-04 12:57:08 -04:00
background={{ fill: "transparent" }}
>
2024-09-04 16:41:23 -04:00
{data.map((entry, index) => {
let { main, light } = getColorRange(entry.uptimePercentage);
return (
<Cell
key={`cell-${index}`}
fill={
hoveredBarIndex === index ? main : chartHovered ? light : main
}
onMouseEnter={() => {
setHoveredBarIndex(index);
onBarHover(entry);
}}
onMouseLeave={() => {
setHoveredBarIndex(null);
onBarHover({
time: null,
totalChecks: 0,
uptimePercentage: 0,
});
}}
/>
);
})}
2024-09-04 12:57:08 -04:00
</Bar>
2024-09-03 20:00:48 -04:00
</BarChart>
</ResponsiveContainer>
);
};
2024-09-04 12:57:08 -04:00
export const DownBarChart = ({ data, type, onBarHover }) => {
2024-09-03 20:00:48 -04:00
const theme = useTheme();
2024-09-04 12:57:08 -04:00
const [chartHovered, setChartHovered] = useState(false);
const [hoveredBarIndex, setHoveredBarIndex] = useState(null);
2024-09-03 20:00:48 -04:00
return (
2024-09-04 12:57:08 -04:00
<ResponsiveContainer width="100%" height={155}>
<BarChart
width="100%"
height="100%"
data={data}
onMouseEnter={() => {
setChartHovered(true);
2024-09-04 16:41:23 -04:00
onBarHover({ time: null, totalIncidents: 0 });
2024-09-04 12:57:08 -04:00
}}
onMouseLeave={() => {
setChartHovered(false);
setHoveredBarIndex(null);
onBarHover(null);
}}
>
2024-09-03 20:00:48 -04:00
<XAxis
stroke={theme.palette.border.dark}
height={15}
tick={false}
label={
<CustomLabels
x={0}
y={0}
width="100%"
height="100%"
firstDataPoint={data[0]}
lastDataPoint={data[data.length - 1]}
2024-09-04 12:57:08 -04:00
type={type}
2024-09-03 20:00:48 -04:00
/>
}
/>
<Bar
dataKey="totalIncidents"
2024-09-04 16:41:23 -04:00
maxBarSize={7}
2024-09-04 12:57:08 -04:00
background={{ fill: "transparent" }}
>
{data.map((entry, index) => (
<Cell
key={`cell-${index}`}
fill={
hoveredBarIndex === index
? theme.palette.error.text
: chartHovered
? theme.palette.error.light
: theme.palette.error.text
}
onMouseEnter={() => {
setHoveredBarIndex(index);
onBarHover(entry);
}}
onMouseLeave={() => {
setHoveredBarIndex(null);
2024-09-04 16:41:23 -04:00
onBarHover({ time: null, totalIncidents: 0 });
2024-09-04 12:57:08 -04:00
}}
/>
))}
</Bar>
2024-09-03 20:00:48 -04:00
</BarChart>
</ResponsiveContainer>
);
};