Skip to content

Commit 31d3f14

Browse files
committed
Fix - VueUiSparkbar - Fix chart not updating when dataset changes
1 parent 54f8d5e commit 31d3f14

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/components/vue-ui-sparkbar.vue

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup>
2-
import { ref, computed, onMounted } from "vue";
2+
import { ref, computed, onMounted, watch, nextTick } from "vue";
33
import {
44
convertColorToHex,
55
convertCustomPalette,
@@ -78,14 +78,19 @@ const safeDatasetCopy = ref(props.dataset.map(d => {
7878
}
7979
}));
8080
81-
onMounted(() => {
81+
const rafId = ref(null);
82+
83+
onMounted(async () => {
8284
if(objectIsEmpty(props.dataset)) {
8385
error({
8486
componentName: 'VueUiSparkbar',
8587
type: 'dataset'
8688
})
8789
}
90+
useAnimation();
91+
})
8892
93+
function useAnimation() {
8994
if (FINAL_CONFIG.value.style.animation.show) {
9095
const chunks = FINAL_CONFIG.value.style.animation.animationFrames;
9196
const chunkSet = props.dataset.map((d, i) => d.value / chunks);
@@ -101,7 +106,7 @@ onMounted(() => {
101106
value: d.value += chunkSet[i]
102107
}
103108
});
104-
requestAnimationFrame(animate)
109+
rafId.value = requestAnimationFrame(animate)
105110
} else {
106111
safeDatasetCopy.value = props.dataset.map(d => {
107112
return {
@@ -113,7 +118,19 @@ onMounted(() => {
113118
}
114119
animate()
115120
}
116-
})
121+
}
122+
123+
watch(() => props.dataset, async (v) => {
124+
cancelAnimationFrame(rafId.value);
125+
126+
safeDatasetCopy.value = props.dataset.map(d => {
127+
return {
128+
...d,
129+
value: FINAL_CONFIG.value.style.animation.show ? 0 : d.value || 0
130+
}});
131+
132+
nextTick(useAnimation);
133+
}, { deep: true })
117134
118135
119136
const svg = ref({

0 commit comments

Comments
 (0)