Skip to content

Commit f1987ab

Browse files
committed
Improvement - VueUiStackbar - Add support for negative values
1 parent 1886609 commit f1987ab

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/components/vue-ui-stackbar.vue

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup>
2-
import { ref, computed, onMounted, nextTick } from "vue";
2+
import { ref, computed, onMounted } from "vue";
33
import { useConfig } from "../useConfig";
44
import {
55
adaptColorToBackground,
@@ -246,6 +246,17 @@ const datasetTotals = computed(() => {
246246
return sumSeries(unmutableDataset.value.filter(ds => !segregated.value.includes(ds.id))).slice(slicer.value.start, slicer.value.end);
247247
});
248248
249+
const displayTotals = computed(() => {
250+
return sumSeries(unmutableDataset.value.filter(ds => !segregated.value.includes(ds.id)).map(s => {
251+
return {
252+
...s,
253+
series: s.series.map((dp,i) => {
254+
return s.signedSeries[i] === -1 ? (dp >= 0 ? -dp : dp) : dp
255+
})
256+
}
257+
})).slice(slicer.value.start, slicer.value.end);
258+
})
259+
249260
const datasetSignedTotals = computed(() => {
250261
const src = unmutableDataset.value.filter(ds => !segregated.value.includes(ds.id))
251262
return {
@@ -363,22 +374,18 @@ const formattedDataset = computed(() => {
363374
});
364375
365376
const totalLabels = computed(() => {
366-
const MAX = Math.max(...datasetTotals.value);
367-
const scale = calculateNiceScale(0, MAX, FINAL_CONFIG.value.style.chart.grid.scale.ticks);
368-
const maxTotal = scale.max;
369-
370-
return datasetTotals.value.map((t, i) => {
377+
return displayTotals.value.map((t, i) => {
371378
return {
372379
value: t,
373-
y: (1 - (t / maxTotal)) * drawingArea.value.height
380+
sign: t >= 0 ? 1 : -1
374381
}
375382
});
376383
});
377384
378385
379386
function barDataLabel(val, datapoint, index, dpIndex, signed) {
380387
381-
const appliedValue = FINAL_CONFIG.value.style.chart.bars.distributed ? signed === 1 ? val : -val : val;
388+
const appliedValue = signed === - 1 ? (val >= 0 ? -val : val) : val
382389
return applyDataLabel(
383390
FINAL_CONFIG.value.style.chart.bars.dataLabels.formatter,
384391
appliedValue,
@@ -428,7 +435,7 @@ function useTooltip(seriesIndex) {
428435
const datapoint = JSON.parse(JSON.stringify(formattedDataset.value)).map(fd => {
429436
return {
430437
name: fd.name,
431-
value: fd.series[seriesIndex] === 0 ? 0 : fd.series[seriesIndex] || null,
438+
value: fd.series[seriesIndex] === 0 ? 0 : (fd.signedSeries[seriesIndex] === -1 ? (fd.series[seriesIndex] >= 0 ? -fd.series[seriesIndex] : fd.series[seriesIndex]) : fd.series[seriesIndex]) || null,
432439
proportion: fd.proportions[seriesIndex] || null,
433440
color: fd.color,
434441
id: fd.id
@@ -834,7 +841,7 @@ defineExpose({
834841
:font-weight="FINAL_CONFIG.style.chart.bars.totalValues.bold ? 'bold' : 'normal'"
835842
:fill="FINAL_CONFIG.style.chart.bars.totalValues.color"
836843
>
837-
{{ barDataLabel(total.value, total, i) }}
844+
{{ barDataLabel(total.value, total, i, total.sign) }}
838845
</text>
839846
</g>
840847
</template>

0 commit comments

Comments
 (0)