mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-22 09:57:00 +01:00
Update bar chart only if dataset is different.
This commit is contained in:
parent
8e68a781c5
commit
1603657995
@ -1,7 +1,3 @@
|
|||||||
.container {
|
|
||||||
display: grid;
|
|
||||||
}
|
|
||||||
|
|
||||||
.chart {
|
.chart {
|
||||||
position: relative;
|
position: relative;
|
||||||
height: 400px;
|
height: 400px;
|
||||||
@ -13,7 +9,3 @@
|
|||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
gap: 10px;
|
gap: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.tooltip .value {
|
|
||||||
text-transform: lowercase;
|
|
||||||
}
|
|
||||||
|
@ -119,7 +119,7 @@ export function BarChart({
|
|||||||
locale,
|
locale,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const createChart = () => {
|
const createChart = (datasets: any[]) => {
|
||||||
Chart.defaults.font.family = 'Inter';
|
Chart.defaults.font.family = 'Inter';
|
||||||
|
|
||||||
chart.current = new Chart(canvas.current, {
|
chart.current = new Chart(canvas.current, {
|
||||||
@ -133,24 +133,36 @@ export function BarChart({
|
|||||||
onCreate?.(chart.current);
|
onCreate?.(chart.current);
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateChart = () => {
|
const updateChart = (datasets: any[]) => {
|
||||||
setTooltipPopup(null);
|
setTooltipPopup(null);
|
||||||
|
|
||||||
chart.current.data.datasets = datasets;
|
const diff = chart.current.data.datasets.reduce(
|
||||||
|
(found: boolean, dataset: { data: any[] }, set: number) => {
|
||||||
|
if (!found) {
|
||||||
|
return dataset.data.find((value, index) => {
|
||||||
|
return datasets[set].data[index].y !== value.y;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return found;
|
||||||
|
},
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (diff) {
|
||||||
|
chart.current.data.datasets = datasets;
|
||||||
chart.current.options = getOptions();
|
chart.current.options = getOptions();
|
||||||
|
chart.current.update();
|
||||||
|
|
||||||
onUpdate?.(chart.current);
|
onUpdate?.(chart.current);
|
||||||
|
}
|
||||||
chart.current.update();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (datasets) {
|
if (datasets) {
|
||||||
if (!chart.current) {
|
if (!chart.current) {
|
||||||
createChart();
|
createChart(datasets);
|
||||||
} else {
|
} else {
|
||||||
updateChart();
|
updateChart(datasets);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [datasets, unit, theme, animationDuration, locale]);
|
}, [datasets, unit, theme, animationDuration, locale]);
|
||||||
|
Loading…
Reference in New Issue
Block a user