Skip to content

Commit 946c4d9

Browse files
committed
Improvement - VueUiSparkline - Add optional mini tooltip
1 parent 2523879 commit 946c4d9

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/components/vue-ui-sparkline.vue

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { useResponsive } from "../useResponsive";
2626
import { throttle } from "../canvas-lib";
2727
import PackageVersion from "../atoms/PackageVersion.vue";
2828
import { useChartAccessibility } from "../useChartAccessibility";
29+
import SparkTooltip from "../atoms/SparkTooltip.vue"
2930
3031
const { vue_ui_sparkline: DEFAULT_CONFIG } = useConfig();
3132
@@ -317,14 +318,19 @@ const area = computed(() => {
317318
318319
319320
const selectedPlot = ref(undefined);
321+
const previousSelectedPlot = ref(undefined);
320322
321323
function selectPlot(plot, index) {
322324
selectedPlot.value = plot;
325+
if (!previousSelectedPlot.value) {
326+
previousSelectedPlot.value = plot;
327+
}
323328
emits('hoverIndex', {index})
324329
}
325330
326331
function unselectPlot() {
327-
selectedPlot.value = undefined
332+
previousSelectedPlot.value = selectedPlot.value;
333+
selectedPlot.value = undefined;
328334
emits('hoverIndex', {index:undefined})
329335
}
330336
@@ -409,6 +415,7 @@ function selectDatapoint(datapoint, index) {
409415
v-if="isDataset"
410416
data-cy="sparkline-svg"
411417
:viewBox="`0 0 ${svg.width} ${svg.height}`" :style="`background:${FINAL_CONFIG.style.backgroundColor};overflow:visible`"
418+
@mouseleave="previousSelectedPlot = undefined"
412419
>
413420
<PackageVersion />
414421

@@ -557,6 +564,39 @@ function selectDatapoint(datapoint, index) {
557564
<slot name="svg" :svg="svg"/>
558565
</svg>
559566

567+
<SparkTooltip
568+
v-if="selectedPlot && FINAL_CONFIG.style.tooltip.show"
569+
:x="selectedPlot.x"
570+
:y="selectedPlot.y"
571+
:prevX="previousSelectedPlot.x"
572+
:prevY="previousSelectedPlot.y"
573+
:offsetY="FINAL_CONFIG.style.plot.radius * 3 + FINAL_CONFIG.style.tooltip.offsetY"
574+
:svgRef="svgRef"
575+
:background="FINAL_CONFIG.style.tooltip.backgroundColor"
576+
:color="FINAL_CONFIG.style.tooltip.color"
577+
:fontSize="FINAL_CONFIG.style.tooltip.fontSize"
578+
:borderWidth="FINAL_CONFIG.style.tooltip.borderWidth"
579+
:borderColor="FINAL_CONFIG.style.tooltip.borderColor"
580+
:borderRadius="FINAL_CONFIG.style.tooltip.borderRadius"
581+
:backgroundOpacity="FINAL_CONFIG.style.tooltip.backgroundOpacity"
582+
>
583+
<slot name="tootlip" v-bind="{ ...selectedPlot }">
584+
{{ selectedPlot.period }}: {{
585+
applyDataLabel(
586+
FINAL_CONFIG.style.dataLabel.formatter,
587+
selectedPlot.absoluteValue,
588+
dl({
589+
p: FINAL_CONFIG.style.dataLabel.prefix,
590+
v: selectedPlot.absoluteValue,
591+
s: FINAL_CONFIG.style.dataLabel.suffix,
592+
r: FINAL_CONFIG.style.dataLabel.roundingValue
593+
}),
594+
{ datapoint: selectedPlot }
595+
)
596+
}}
597+
</slot>
598+
</SparkTooltip>
599+
560600
<div v-if="$slots.source" ref="source" dir="auto">
561601
<slot name="source" />
562602
</div>

0 commit comments

Comments
 (0)