Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Update bar chart only if dataset is different.

Mike Cao16036579953e
diff --git a/src/components/metrics/BarChart.module.css b/src/components/metrics/BarChart.module.css
index 6af22abe6..ee61f29bf 100644
--- a/src/components/metrics/BarChart.module.css
+++ b/src/components/metrics/BarChart.module.css
@@ -1,7 +1,3 @@
-.container {
- display: grid;
-}
-
.chart {
position: relative;
height: 400px;
@@ -13,7 +9,3 @@
flex-direction: column;
gap: 10px;
}
-
-.tooltip .value {
- text-transform: lowercase;
-}
diff --git a/src/components/metrics/BarChart.tsx b/src/components/metrics/BarChart.tsx
index 1b1c75888..8c26afa01 100644
--- a/src/components/metrics/BarChart.tsx
+++ b/src/components/metrics/BarChart.tsx
@@ -119,7 +119,7 @@ export function BarChart({
locale,
]);
- const createChart = () => {
+ const createChart = (datasets: any[]) => {
Chart.defaults.font.family = 'Inter';
chart.current = new Chart(canvas.current, {
@@ -133,24 +133,36 @@ export function BarChart({
onCreate?.(chart.current);
};
- const updateChart = () => {
+ const updateChart = (datasets: any[]) => {
setTooltipPopup(null);
- chart.current.data.datasets = datasets;
-
- chart.current.options = getOptions();
+ 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,
+ );
- onUpdate?.(chart.current);
+ if (diff) {
+ chart.current.data.datasets = datasets;
+ chart.current.options = getOptions();
+ chart.current.update();
- chart.current.update();
+ onUpdate?.(chart.current);
+ }
};
useEffect(() => {
if (datasets) {
if (!chart.current) {
- createChart();
+ createChart(datasets);
} else {
- updateChart();
+ updateChart(datasets);
}
}
}, [datasets, unit, theme, animationDuration, locale]);