Skip to content

Commit e0713e2

Browse files
committed
Fix - VueUiSparkline - Fix edge cases layout bugs when dataset contains a single datapoint
1 parent ed12818 commit e0713e2

File tree

2 files changed

+11
-20
lines changed

2 files changed

+11
-20
lines changed

src/components/vue-ui-sparkline.cy.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('<VueUiSparkline />', () => {
3434
cy.get('[data-cy="sparkline-angle-area"]').should('exist').and('be.visible');
3535

3636
cy.log('line');
37-
cy.get('[data-cy="segment-line"]').should('exist').and('be.visible').and('have.length', dataset.length - 1);
37+
cy.get('[data-cy="sparkline-straight-line"]').should('exist').and('be.visible');
3838

3939
});
4040
});

src/components/vue-ui-sparkline.vue

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
calcLinearProgression,
66
calcMedian,
77
createSmoothPath,
8+
createStraightPath,
89
createUid,
910
dataLabel as dl,
1011
error,
@@ -59,7 +60,7 @@ const props = defineProps({
5960
});
6061
6162
const isDataset = computed(() => {
62-
return !!props.dataset && props.dataset.length;
63+
return Array.isArray(props.dataset) && props.dataset.length > 0;
6364
});
6465
6566
const uid = ref(createUid());
@@ -143,7 +144,7 @@ const safeDatasetCopy = ref(prepareDsCopy());
143144
function prepareDsCopy() {
144145
return largestTriangleThreeBucketsArrayObjects({
145146
data: props.dataset.map(d => {
146-
if (FINAL_CONFIG.value.style.animation.show) {
147+
if (FINAL_CONFIG.value.style.animation.show && props.dataset.length > 1) {
147148
return {
148149
...d,
149150
value: null
@@ -278,24 +279,25 @@ const absoluteZero = computed(() => {
278279
})
279280
280281
function ratioToMax(v) {
281-
return v / absoluteMax.value;
282+
return isNaN(v / absoluteMax.value) ? 0 : v / absoluteMax.value;
282283
}
283284
284285
const len = computed(() => downsampled.value.length - 1);
285286
286287
const mutableDataset = computed(() => {
287288
return safeDatasetCopy.value.map((s, i) => {
288289
const absoluteValue = isNaN(s.value) || [undefined, null, 'NaN', NaN, Infinity, -Infinity].includes(s.value) ? 0 : (s.value || 0);
290+
const width = (drawingArea.value.width / (len.value + 1)) > svg.padding ? svg.padding : (drawingArea.value.width / (len.value + 1));
289291
return {
290292
absoluteValue,
291293
period: s.period,
292294
plotValue: absoluteValue + absoluteMin.value,
293295
toMax: ratioToMax(absoluteValue + absoluteMin.value),
294-
x: drawingArea.value.start + (i * ((drawingArea.value.width / (len.value + 1)) > svg.padding ? svg.padding : (drawingArea.value.width / (len.value + 1)))),
296+
x: drawingArea.value.start + (i * (width > drawingArea.value.width / 12 ? drawingArea.value.width / 12 : width)),
295297
y: drawingArea.value.bottom - (drawingArea.value.height * ratioToMax(absoluteValue + absoluteMin.value)),
296298
id: `plot_${uid.value}_${i}`,
297299
color: isBar.value ? FINAL_CONFIG.value.style.bar.color : FINAL_CONFIG.value.style.area.useGradient ? shiftHue(FINAL_CONFIG.value.style.line.color, 0.05 * ( 1 - (i / len.value))) : FINAL_CONFIG.value.style.line.color,
298-
width: (drawingArea.value.width / (len.value + 1)) > svg.padding ? svg.padding : (drawingArea.value.width / (len.value + 1))
300+
width: width > drawingArea.value.width / 12 ? drawingArea.value.width / 12 : width
299301
}
300302
})
301303
});
@@ -451,21 +453,10 @@ function selectDatapoint(datapoint, index) {
451453
</g>
452454

453455
<path data-cy="sparkline-smooth-path" v-if="FINAL_CONFIG.style.line.smooth && !isBar" :d="`M ${createSmoothPath(mutableDataset)}`" :stroke="FINAL_CONFIG.style.line.color" fill="none" :stroke-width="FINAL_CONFIG.style.line.strokeWidth" stroke-linecap="round"/>
456+
457+
<path data-cy="sparkline-straight-line" v-if="!FINAL_CONFIG.style.line.smooth && !isBar" :d="`M ${createStraightPath(mutableDataset)}`" :stroke="FINAL_CONFIG.style.line.color" fill="none" :stroke-width="FINAL_CONFIG.style.line.strokeWidth" stroke-linecap="round"/>
454458

455459
<g v-for="(plot, i) in mutableDataset">
456-
<line
457-
v-if="i < mutableDataset.length - 1 && !FINAL_CONFIG.style.line.smooth && !isBar"
458-
data-cy="segment-line"
459-
:x1="plot.x"
460-
:x2="mutableDataset[i + 1].x"
461-
:y1="plot.y || 0"
462-
:y2="mutableDataset[i + 1].y || 0"
463-
:stroke="plot.color"
464-
:stroke-width="FINAL_CONFIG.style.line.strokeWidth"
465-
stroke-linecap="round"
466-
stroke-linejoin="round"
467-
shape-rendering="geometricPrecision"
468-
/>
469460
<rect
470461
data-cy="datapoint-bar"
471462
v-if="isBar"
@@ -509,7 +500,7 @@ function selectDatapoint(datapoint, index) {
509500
<g v-if="FINAL_CONFIG.style.plot.show" v-for="(plot, i) in mutableDataset">
510501
<circle
511502
data-cy="selection-plot"
512-
v-if="(selectedPlot && plot.id === selectedPlot.id) || selectedIndex === i"
503+
v-if="(selectedPlot && plot.id === selectedPlot.id) || selectedIndex === i || dataset.length === 1"
513504
:cx="plot.x"
514505
:cy="plot.y"
515506
:r="FINAL_CONFIG.style.plot.radius"

0 commit comments

Comments
 (0)