Skip to content

Commit 5ef69d4

Browse files
authored
Merge pull request #91 from graphieros/ft-ts-rewrite
Ft ts rewrite
2 parents 0b1aec2 + 52dccb0 commit 5ef69d4

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ However the folowing charts can be made fully responsive, making them better to
650650
| VueUiSparkTrend | - |
651651
| VueUiSparkbar | - |
652652
| VueUiSparkgauge | - |
653-
| VueUiSparkline | - |
653+
| VueUiSparkline | |
654654
| VueUiStackbar ||
655655
| VueUiStripPlot ||
656656
| VueUiTableHeatmap | - |

TestingArena/ArenaVueUiSparkline.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,16 @@ const dataset = ref([
7777
])
7878
7979
const model = ref([
80+
{ key: 'responsive', def: false, type: 'checkbox'},
8081
{ key: 'type', def: 'line', type: 'select', options: ['line', 'bar']},
81-
{ key: 'style.chartWidth', def: 290, type: 'number', min: 100, max: 500},
82+
{ key: 'style.chartWidth', def: 400, type: 'number', min: 100, max: 500},
8283
{ key: 'style.animation.show', def: true, type: 'checkbox'},
8384
{ key: 'style.animation.animationFrames', def: 360, type: 'number', min: 0, max: 1000},
8485
{ key: 'style.backgroundColor', def: '#FFFFFF', type: 'color'},
8586
{ key: 'style.fontFamily', def: 'inherit', type: 'text'},
8687
{ key: 'style.line.color', def: '#3366CC', type: 'color'},
8788
{ key: 'style.line.strokeWidth', def: 3, type: 'number', min: 0, max: 20},
88-
{ key: 'style.line.smooth', def: false, type: 'checkbox'},
89+
{ key: 'style.line.smooth', def: true, type: 'checkbox'},
8990
{ key: 'style.bar.borderRadius', def: 3, type: 'number', min: 0, max: 12},
9091
{ key: 'style.bar.color', def: '#3366CC', type: 'color'},
9192
{ key: 'style.zeroLine.color', def: '#1A1A1A', type: 'color'},
@@ -160,6 +161,14 @@ const step = ref(0)
160161
<option v-for="opt in themeOptions">{{ opt }}</option>
161162
</select>
162163
</div>
164+
165+
<div style="width: 600px; height: 400px; resize: both; overflow: auto; background: white">
166+
<LocalVueUiSparkline :key="`responsive_${step}`" :dataset="dataset" :config="{
167+
...config,
168+
responsive: true
169+
}"/>
170+
</div>
171+
163172
<Box comp="VueUiSparkline" :dataset="dataset">
164173
<template #title>VueUiSparkline</template>
165174

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>

src/useConfig.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ export function useConfig() {
19291929

19301930
const vue_ui_sparkline = {
19311931
theme: '',
1932+
responsive: false,
19321933
type: SHAPE.LINE,
19331934
style: {
19341935
chartWidth: 290,

types/vue-data-ui.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,6 +3624,7 @@ declare module 'vue-data-ui' {
36243624
export type VueUiSparklineConfig = {
36253625
theme?: Theme;
36263626
type?: "line" | "bar";
3627+
responsive?: boolean;
36273628
style?: {
36283629
backgroundColor?: string;
36293630
fontFamily?: string;

0 commit comments

Comments
 (0)