Skip to content

Commit 52dccb0

Browse files
committed
Improvement - VueUiSparkline - Add responsive config attribute
1 parent 0c1faac commit 52dccb0

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/components/vue-ui-sparkline.vue

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import themes from "../themes.json";
1818
import { useNestedProp } from "../useNestedProp";
1919
import Skeleton from "./vue-ui-skeleton.vue";
2020
import { useConfig } from "../useConfig";
21+
import { useResponsive } from "../useResponsive";
22+
import { throttle } from "../canvas-lib";
2123
2224
const { vue_ui_sparkline: DEFAULT_CONFIG } = useConfig();
2325
@@ -49,6 +51,8 @@ const isDataset = computed(() => {
4951
});
5052
5153
const uid = ref(createUid());
54+
const sparklineChart = ref(null);
55+
const chartTitle = ref(null);
5256
5357
const FINAL_CONFIG = computed(() => {
5458
const mergedConfig = useNestedProp({
@@ -79,7 +83,9 @@ const safeDatasetCopy = ref(props.dataset.map(d => {
7983
value: ![undefined].includes(d.value) ? d.value : null
8084
}
8185
}
82-
}))
86+
}));
87+
88+
const resizeObserver = ref(null);
8389
8490
onMounted(() => {
8591
if(objectIsEmpty(props.dataset)) {
@@ -122,11 +128,28 @@ onMounted(() => {
122128
animate()
123129
}
124130
131+
if (FINAL_CONFIG.value.responsive) {
132+
const handleResize = throttle(() => {
133+
const { width, height } = useResponsive({
134+
chart: sparklineChart.value,
135+
title: FINAL_CONFIG.value.style.title.show && props.showInfo ? chartTitle.value : null
136+
});
137+
svg.value.width = width;
138+
svg.value.height = height;
139+
svg.value.chartWidth = FINAL_CONFIG.value.style.chartWidth / 500 * width;
140+
svg.value.padding = 30 / 500 * width;
141+
});
142+
143+
resizeObserver.value = new ResizeObserver(handleResize);
144+
resizeObserver.value.observe(sparklineChart.value.parentNode);
145+
};
125146
})
126147
127148
const svg = ref({
128149
height: 80,
129150
width: 500,
151+
chartWidth: FINAL_CONFIG.value.style.chartWidth,
152+
padding: 30
130153
});
131154
132155
const emits = defineEmits(['hoverIndex', 'selectDatapoint'])
@@ -140,8 +163,8 @@ const drawingArea = computed(() => {
140163
left: 0,
141164
right: svg.value.width,
142165
bottom: svg.value.height - 3,
143-
start: props.showInfo && FINAL_CONFIG.value.style.dataLabel.show && FINAL_CONFIG.value.style.dataLabel.position === 'left' ? svg.value.width - FINAL_CONFIG.value.style.chartWidth : 30,
144-
width: props.showInfo && FINAL_CONFIG.value.style.dataLabel.show ? FINAL_CONFIG.value.style.chartWidth : svg.value.width - 30,
166+
start: props.showInfo && FINAL_CONFIG.value.style.dataLabel.show && FINAL_CONFIG.value.style.dataLabel.position === 'left' ? svg.value.width - svg.value.chartWidth : svg.value.padding,
167+
width: props.showInfo && FINAL_CONFIG.value.style.dataLabel.show ? svg.value.chartWidth : svg.value.width - svg.value.padding,
145168
height: svg.value.height - topPadding
146169
}
147170
});
@@ -267,7 +290,7 @@ function selectDatapoint(datapoint, index) {
267290
</script>
268291

269292
<template>
270-
<div class="vue-ui-sparkline" :id="uid" :style="`width:100%;font-family:${FINAL_CONFIG.style.fontFamily}`">
293+
<div ref="sparklineChart" class="vue-ui-sparkline" :id="uid" :style="`width:100%;font-family:${FINAL_CONFIG.style.fontFamily}`">
271294
<!-- SLOT BEFORE -->
272295
<slot
273296
name="before"
@@ -282,7 +305,7 @@ function selectDatapoint(datapoint, index) {
282305
/>
283306

284307
<!-- TITLE -->
285-
<div v-if="FINAL_CONFIG.style.title.show && showInfo" class="vue-ui-sparkline-title" :style="`display:flex;align-items:center;width:100%;color:${FINAL_CONFIG.style.title.color};background:${FINAL_CONFIG.style.backgroundColor};justify-content:${FINAL_CONFIG.style.title.textAlign === 'left' ? 'flex-start' : FINAL_CONFIG.style.title.textAlign === 'right' ? 'flex-end' : 'center'};height:${FINAL_CONFIG.style.title.fontSize * 2}px;font-size:${FINAL_CONFIG.style.title.fontSize}px;font-weight:${FINAL_CONFIG.style.title.bold ? 'bold' : 'normal'};`">
308+
<div ref="chartTitle" v-if="FINAL_CONFIG.style.title.show && showInfo" class="vue-ui-sparkline-title" :style="`display:flex;align-items:center;width:100%;color:${FINAL_CONFIG.style.title.color};background:${FINAL_CONFIG.style.backgroundColor};justify-content:${FINAL_CONFIG.style.title.textAlign === 'left' ? 'flex-start' : FINAL_CONFIG.style.title.textAlign === 'right' ? 'flex-end' : 'center'};height:${FINAL_CONFIG.style.title.fontSize * 2}px;font-size:${FINAL_CONFIG.style.title.fontSize}px;font-weight:${FINAL_CONFIG.style.title.bold ? 'bold' : 'normal'};`">
286309
<span data-cy="sparkline-period-label" :style="`padding:${FINAL_CONFIG.style.title.textAlign === 'left' ? '0 0 0 12px' : FINAL_CONFIG.style.title.textAlign === 'right' ? '0 12px 0 0' : '0'}`">
287310
{{ selectedPlot ? selectedPlot.period : FINAL_CONFIG.style.title.text }}
288311
</span>

0 commit comments

Comments
 (0)