@@ -18,6 +18,8 @@ import themes from "../themes.json";
18
18
import { useNestedProp } from " ../useNestedProp" ;
19
19
import Skeleton from " ./vue-ui-skeleton.vue" ;
20
20
import { useConfig } from " ../useConfig" ;
21
+ import { useResponsive } from " ../useResponsive" ;
22
+ import { throttle } from " ../canvas-lib" ;
21
23
22
24
const { vue_ui_sparkline: DEFAULT_CONFIG } = useConfig ();
23
25
@@ -49,6 +51,8 @@ const isDataset = computed(() => {
49
51
});
50
52
51
53
const uid = ref (createUid ());
54
+ const sparklineChart = ref (null );
55
+ const chartTitle = ref (null );
52
56
53
57
const FINAL_CONFIG = computed (() => {
54
58
const mergedConfig = useNestedProp ({
@@ -79,7 +83,9 @@ const safeDatasetCopy = ref(props.dataset.map(d => {
79
83
value: ! [undefined ].includes (d .value ) ? d .value : null
80
84
}
81
85
}
82
- }))
86
+ }));
87
+
88
+ const resizeObserver = ref (null );
83
89
84
90
onMounted (() => {
85
91
if (objectIsEmpty (props .dataset )) {
@@ -122,11 +128,28 @@ onMounted(() => {
122
128
animate ()
123
129
}
124
130
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
+ };
125
146
})
126
147
127
148
const svg = ref ({
128
149
height: 80 ,
129
150
width: 500 ,
151
+ chartWidth: FINAL_CONFIG .value .style .chartWidth ,
152
+ padding: 30
130
153
});
131
154
132
155
const emits = defineEmits ([' hoverIndex' , ' selectDatapoint' ])
@@ -140,8 +163,8 @@ const drawingArea = computed(() => {
140
163
left: 0 ,
141
164
right: svg .value .width ,
142
165
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 ,
145
168
height: svg .value .height - topPadding
146
169
}
147
170
});
@@ -267,7 +290,7 @@ function selectDatapoint(datapoint, index) {
267
290
</script >
268
291
269
292
<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}`" >
271
294
<!-- SLOT BEFORE -->
272
295
<slot
273
296
name =" before"
@@ -282,7 +305,7 @@ function selectDatapoint(datapoint, index) {
282
305
/>
283
306
284
307
<!-- 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'};`" >
286
309
<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'}`" >
287
310
{{ selectedPlot ? selectedPlot.period : FINAL_CONFIG.style.title.text }}
288
311
</span >
0 commit comments