umami/components/metrics/BarChart.js

164 lines
3.6 KiB
JavaScript
Raw Normal View History

import { useState, useRef, useEffect, useCallback } from 'react';
import { Loading } from 'react-basics';
2020-08-26 16:58:24 +00:00
import classNames from 'classnames';
import Chart from 'chart.js/auto';
import HoverTooltip from 'components/common/HoverTooltip';
2020-10-15 05:09:00 +00:00
import Legend from 'components/metrics/Legend';
2020-09-09 03:46:31 +00:00
import useLocale from 'hooks/useLocale';
2020-09-20 08:33:39 +00:00
import useTheme from 'hooks/useTheme';
import { DEFAULT_ANIMATION_DURATION } from 'lib/constants';
import { renderNumberLabels } from 'lib/charts';
2020-10-10 00:58:27 +00:00
import styles from './BarChart.module.css';
2020-08-26 16:58:24 +00:00
2023-04-21 15:00:42 +00:00
export function BarChart({
2020-08-26 16:58:24 +00:00
datasets,
unit,
2020-10-10 00:58:27 +00:00
animationDuration = DEFAULT_ANIMATION_DURATION,
2020-08-27 10:42:24 +00:00
stacked = false,
loading = false,
renderXLabel,
renderYLabel,
XAxisType = 'time',
YAxisType = 'linear',
2023-06-16 03:15:31 +00:00
renderTooltipPopup,
onCreate,
onUpdate,
2023-03-16 22:56:05 +00:00
className,
2020-08-26 16:58:24 +00:00
}) {
const canvas = useRef();
2023-03-16 22:56:05 +00:00
const chart = useRef(null);
2023-06-16 03:15:31 +00:00
const [tooltip, setTooltipPopup] = useState(null);
const { locale } = useLocale();
const { theme, colors } = useTheme();
2020-09-14 03:09:18 +00:00
2023-03-16 22:56:05 +00:00
const getOptions = useCallback(() => {
return {
2020-08-28 01:44:20 +00:00
responsive: true,
maintainAspectRatio: false,
animation: {
duration: animationDuration,
resize: {
duration: 0,
},
active: {
duration: 0,
},
2020-09-20 08:33:39 +00:00
},
plugins: {
legend: {
display: false,
},
tooltip: {
enabled: false,
2023-06-16 03:15:31 +00:00
external: renderTooltipPopup ? renderTooltipPopup.bind(null, setTooltipPopup) : undefined,
},
},
2020-08-27 20:46:05 +00:00
scales: {
x: {
type: XAxisType,
stacked: true,
2023-03-16 09:05:56 +00:00
time: {
unit,
},
grid: {
display: false,
},
2023-03-16 09:05:56 +00:00
border: {
color: colors.chart.line,
2023-03-16 09:05:56 +00:00
},
ticks: {
color: colors.chart.text,
autoSkip: false,
maxRotation: 0,
2023-04-19 18:41:31 +00:00
callback: renderXLabel,
2020-08-27 20:46:05 +00:00
},
},
y: {
type: YAxisType,
min: 0,
beginAtZero: true,
stacked,
2023-03-16 09:05:56 +00:00
grid: {
color: colors.chart.line,
2023-03-16 09:05:56 +00:00
},
border: {
color: colors.chart.line,
2023-03-16 09:05:56 +00:00
},
ticks: {
2023-03-16 09:05:56 +00:00
color: colors.text,
callback: renderYLabel || renderNumberLabels,
2020-08-27 20:46:05 +00:00
},
},
2020-08-27 20:46:05 +00:00
},
};
}, [
animationDuration,
2023-06-16 03:15:31 +00:00
renderTooltipPopup,
renderXLabel,
XAxisType,
YAxisType,
stacked,
colors,
unit,
locale,
]);
2023-03-16 22:56:05 +00:00
const createChart = () => {
Chart.defaults.font.family = 'Inter';
const options = getOptions();
2020-08-27 20:46:05 +00:00
chart.current = new Chart(canvas.current, {
2020-08-27 10:42:24 +00:00
type: 'bar',
data: {
datasets,
},
2020-08-27 20:46:05 +00:00
options,
2020-08-27 10:42:24 +00:00
});
2023-03-26 11:15:08 +00:00
onCreate?.(chart.current);
2023-03-16 09:05:56 +00:00
};
2020-08-26 16:58:24 +00:00
2023-03-16 09:05:56 +00:00
const updateChart = () => {
2023-06-16 03:15:31 +00:00
setTooltipPopup(null);
2020-08-26 16:58:24 +00:00
datasets.forEach((dataset, index) => {
chart.current.data.datasets[index].data = dataset.data;
chart.current.data.datasets[index].label = dataset.label;
});
2023-03-16 22:56:05 +00:00
chart.current.options = getOptions();
2020-08-27 10:42:24 +00:00
onUpdate?.(chart.current);
chart.current.update();
2023-03-16 09:05:56 +00:00
};
2020-08-26 16:58:24 +00:00
useEffect(() => {
if (datasets) {
2020-08-27 10:42:24 +00:00
if (!chart.current) {
createChart();
} else {
updateChart();
}
2020-08-26 16:58:24 +00:00
}
2023-04-19 18:41:31 +00:00
}, [datasets, unit, theme, animationDuration, locale]);
2020-08-26 16:58:24 +00:00
return (
2020-08-28 01:44:20 +00:00
<>
<div className={classNames(styles.chart, className)}>
2023-04-19 18:41:31 +00:00
{loading && <Loading position="page" icon="dots" />}
2020-08-28 01:44:20 +00:00
<canvas ref={canvas} />
</div>
2020-10-15 05:09:00 +00:00
<Legend chart={chart.current} />
{tooltip && (
<HoverTooltip>
<div className={styles.tooltip}>{tooltip}</div>
</HoverTooltip>
)}
2020-08-28 01:44:20 +00:00
</>
2020-08-26 16:58:24 +00:00
);
}
2023-04-21 15:00:42 +00:00
export default BarChart;