Skip to content

Commit df2db75

Browse files
committed
Improvement - VueUiSparkline - Added #before scoped slot
1 parent 77f8778 commit df2db75

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

src/components/vue-ui-sparkline.vue

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<script setup>
22
import { ref, computed, onMounted } from "vue";
33
import {
4+
calcLinearProgression,
5+
calcMedian,
46
createSmoothPath,
57
createUid,
68
dataLabel as dl,
@@ -199,16 +201,44 @@ function unselectPlot() {
199201
emits('hoverIndex', {index:undefined})
200202
}
201203
204+
const dataLabelValues = computed(() => {
205+
if (!isDataset.value) {
206+
return {
207+
latest: null,
208+
sum: null,
209+
average: null,
210+
median: null,
211+
trend: null
212+
}
213+
} else {
214+
const ds = mutableDataset.value.map(m => m.absoluteValue);
215+
const sum = ds.reduce((a, b) => a + b, 0)
216+
return {
217+
latest: mutableDataset.value[mutableDataset.value.length -1].absoluteValue,
218+
sum,
219+
average: sum / mutableDataset.value.length,
220+
median: calcMedian(ds),
221+
trend: calcLinearProgression(mutableDataset.value.map(({x, y, absoluteValue}) => {
222+
return {
223+
x,
224+
y,
225+
value: absoluteValue
226+
}
227+
})).trend
228+
}
229+
}
230+
});
231+
202232
const dataLabel = computed(() => {
203233
if(!isDataset.value) {
204234
return 0
205235
}
206236
if (sparklineConfig.value.style.dataLabel.valueType === 'latest') {
207-
return mutableDataset.value[mutableDataset.value.length -1].absoluteValue;
237+
return dataLabelValues.value.latest
208238
} else if(sparklineConfig.value.style.dataLabel.valueType === 'sum') {
209-
return mutableDataset.value.map(m => m.absoluteValue).reduce((a,b) => a + b);
239+
return dataLabelValues.value.sum;
210240
} else if(sparklineConfig.value.style.dataLabel.valueType === "average") {
211-
return mutableDataset.value.map(m => m.absoluteValue).reduce((a,b) => a + b) / mutableDataset.value.length;
241+
return dataLabelValues.value.average;
212242
} else {
213243
return 0;
214244
}
@@ -225,12 +255,26 @@ function selectDatapoint(datapoint, index) {
225255

226256
<template>
227257
<div class="vue-ui-sparkline" :id="uid" :style="`width:100%;font-family:${sparklineConfig.style.fontFamily}`">
258+
<!-- SLOT BEFORE -->
259+
<slot
260+
name="before"
261+
v-bind="{
262+
selected: selectedPlot,
263+
latest: dataLabelValues.latest,
264+
sum: dataLabelValues.sum,
265+
average: dataLabelValues.average,
266+
median: dataLabelValues.median,
267+
trend: dataLabelValues.trend
268+
}"
269+
/>
270+
228271
<!-- TITLE -->
229272
<div v-if="sparklineConfig.style.title.show && showInfo" class="vue-ui-sparkline-title" :style="`display:flex;align-items:center;width:100%;color:${sparklineConfig.style.title.color};background:${sparklineConfig.style.backgroundColor};justify-content:${sparklineConfig.style.title.textAlign === 'left' ? 'flex-start' : sparklineConfig.style.title.textAlign === 'right' ? 'flex-end' : 'center'};height:${sparklineConfig.style.title.fontSize * 2}px;font-size:${sparklineConfig.style.title.fontSize}px;font-weight:${sparklineConfig.style.title.bold ? 'bold' : 'normal'};`">
230273
<span data-cy="sparkline-period-label" :style="`padding:${sparklineConfig.style.title.textAlign === 'left' ? '0 0 0 12px' : sparklineConfig.style.title.textAlign === 'right' ? '0 12px 0 0' : '0'}`">
231274
{{ selectedPlot ? selectedPlot.period : sparklineConfig.style.title.text }}
232275
</span>
233276
</div>
277+
234278
<!-- CHART -->
235279
<svg :xmlns="XMLNS" v-if="isDataset" data-cy="sparkline-svg" :viewBox="`0 0 ${svg.width} ${svg.height}`" :style="`background:${sparklineConfig.style.backgroundColor};overflow:visible`">
236280
<!-- DEFS -->

0 commit comments

Comments
 (0)