umami/components/metrics/BarChart.js

159 lines
3.6 KiB
JavaScript
Raw Normal View History

import { useState, useRef, useEffect, useMemo } from 'react';
import { StatusLight } from 'react-basics';
2020-08-26 18:58:24 +02:00
import classNames from 'classnames';
import Chart from 'chart.js/auto';
import HoverTooltip from 'components/common/HoverTooltip';
2020-10-15 07:09:00 +02:00
import Legend from 'components/metrics/Legend';
2020-09-02 18:56:29 +02:00
import { formatLongNumber } from 'lib/format';
2021-02-27 07:41:05 +01:00
import { dateFormat } from 'lib/date';
2020-09-09 05:46:31 +02:00
import useLocale from 'hooks/useLocale';
2020-09-20 10:33:39 +02:00
import useTheme from 'hooks/useTheme';
2022-01-06 10:45:53 +01:00
import useForceUpdate from 'hooks/useForceUpdate';
2022-03-02 04:28:44 +01:00
import { DEFAULT_ANIMATION_DURATION, THEME_COLORS } from 'lib/constants';
2020-10-10 02:58:27 +02:00
import styles from './BarChart.module.css';
2020-08-26 18:58:24 +02:00
export default function BarChart({
datasets,
unit,
2020-10-10 02:58:27 +02:00
animationDuration = DEFAULT_ANIMATION_DURATION,
2020-08-26 18:58:24 +02:00
className,
2020-08-27 12:42:24 +02:00
stacked = false,
loading = false,
2020-08-27 22:46:05 +02:00
onCreate = () => {},
2020-08-26 18:58:24 +02:00
onUpdate = () => {},
}) {
const canvas = useRef();
const chart = useRef();
2020-09-22 06:34:55 +02:00
const [tooltip, setTooltip] = useState(null);
const { locale } = useLocale();
2020-09-20 10:33:39 +02:00
const [theme] = useTheme();
2020-10-15 18:10:59 +02:00
const forceUpdate = useForceUpdate();
const colors = useMemo(
() => ({
text: THEME_COLORS[theme].gray700,
line: THEME_COLORS[theme].gray200,
zeroLine: THEME_COLORS[theme].gray500,
}),
[theme],
);
2020-08-26 18:58:24 +02:00
2020-09-02 18:56:29 +02:00
function renderYLabel(label) {
2020-10-11 11:29:55 +02:00
return +label > 1000 ? formatLongNumber(label) : label;
2020-09-02 18:56:29 +02:00
}
2020-09-02 18:56:29 +02:00
function renderTooltip(model) {
const { opacity, labelColors, dataPoints } = model.tooltip;
2020-08-26 18:58:24 +02:00
if (!dataPoints?.length || !opacity) {
2020-08-26 18:58:24 +02:00
setTooltip(null);
2020-09-22 06:34:55 +02:00
return;
2020-08-26 18:58:24 +02:00
}
2020-09-22 06:34:55 +02:00
const format = unit === 'hour' ? 'EEE p — PPP' : 'PPPP';
setTooltip(
<>
<div>{dateFormat(new Date(dataPoints[0].raw.x), format, locale)}</div>
<div>
<StatusLight color={labelColors?.[0]?.backgroundColor}>
<b>
{formatLongNumber(dataPoints[0].raw.y)} {dataPoints[0].dataset.label}
</b>
</StatusLight>
</div>
</>,
);
2020-09-14 05:09:18 +02:00
}
2020-09-02 18:56:29 +02:00
function createChart() {
2020-08-27 22:46:05 +02:00
const options = {
2020-08-28 03:44:20 +02:00
responsive: true,
maintainAspectRatio: false,
animation: {
duration: animationDuration,
resize: {
duration: 0,
},
active: {
duration: 0,
},
2020-09-20 10:33:39 +02:00
},
plugins: {
legend: {
display: false,
},
tooltip: {
enabled: false,
external: renderTooltip,
},
},
2020-08-27 22:46:05 +02:00
scales: {
x: {
type: 'time',
stacked: true,
grid: {
display: false,
},
ticks: {
autoSkip: false,
maxRotation: 0,
2020-08-27 22:46:05 +02:00
},
},
y: {
type: 'linear',
min: 0,
beginAtZero: true,
stacked,
ticks: {
callback: renderYLabel,
2020-08-27 22:46:05 +02:00
},
},
2020-08-27 22:46:05 +02:00
},
};
onCreate(options);
chart.current = new Chart(canvas.current, {
2020-08-27 12:42:24 +02:00
type: 'bar',
data: {
datasets,
},
2020-08-27 22:46:05 +02:00
options,
2020-08-27 12:42:24 +02:00
});
2020-09-02 18:56:29 +02:00
}
2020-08-26 18:58:24 +02:00
2020-09-02 18:56:29 +02:00
function updateChart() {
2020-08-27 12:42:24 +02:00
const { options } = chart.current;
2020-08-26 18:58:24 +02:00
options.animation.duration = animationDuration;
2020-08-27 12:42:24 +02:00
onUpdate(chart.current);
chart.current.update();
2020-10-15 18:10:59 +02:00
forceUpdate();
2020-09-02 18:56:29 +02:00
}
2020-08-26 18:58:24 +02:00
useEffect(() => {
if (datasets) {
2020-08-27 12:42:24 +02:00
if (!chart.current) {
createChart();
} else {
setTooltip(null);
updateChart();
}
2020-08-26 18:58:24 +02:00
}
}, [datasets, unit, animationDuration, locale, loading]);
2020-08-26 18:58:24 +02:00
return (
2020-08-28 03:44:20 +02:00
<>
<div className={classNames(styles.chart, className)}>
2020-08-28 03:44:20 +02:00
<canvas ref={canvas} />
</div>
2020-10-15 07:09:00 +02:00
<Legend chart={chart.current} />
{tooltip && <HoverTooltip tooltip={tooltip} />}
2020-08-28 03:44:20 +02:00
</>
2020-08-26 18:58:24 +02:00
);
}