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