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

177 lines
4.5 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-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);
onBarHover({ time: null, totalChecks: null });
}}
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"
barSize={5}
2024-09-04 12:57:08 -04:00
background={{ fill: "transparent" }}
>
{data.map((entry, index) => (
<Cell
key={`cell-${index}`}
fill={
hoveredBarIndex === index
? theme.palette.success.main
: chartHovered
? theme.palette.success.light
: theme.palette.success.main
}
onMouseEnter={() => {
setHoveredBarIndex(index);
onBarHover(entry);
}}
onMouseLeave={() => {
setHoveredBarIndex(null);
onBarHover({ time: null, totalChecks: null });
}}
/>
))}
</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);
onBarHover({ time: null, totalIncidents: null });
}}
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"
barSize={5}
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);
onBarHover({ time: null, totalIncidents: null });
}}
/>
))}
</Bar>
2024-09-03 20:00:48 -04:00
</BarChart>
</ResponsiveContainer>
);
};