2023-07-07 19:22:30 +00:00
|
|
|
import { onUnmounted, onDeactivated, ref } from "vue";
|
|
|
|
import * as os from "@/os";
|
|
|
|
import MkChartTooltip from "@/components/MkChartTooltip.vue";
|
|
|
|
|
|
|
|
export function useChartTooltip(
|
2023-07-23 13:31:28 +00:00
|
|
|
opts: { position: "top" | "middle" } = { position: "top" }
|
2023-07-07 19:22:30 +00:00
|
|
|
) {
|
2023-07-23 13:31:28 +00:00
|
|
|
const tooltipShowing = ref(false);
|
|
|
|
const tooltipX = ref(0);
|
|
|
|
const tooltipY = ref(0);
|
|
|
|
const tooltipTitle = ref(null);
|
|
|
|
const tooltipSeries = ref(null);
|
|
|
|
let disposeTooltipComponent;
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
os.popup(
|
|
|
|
MkChartTooltip,
|
|
|
|
{
|
|
|
|
showing: tooltipShowing,
|
|
|
|
x: tooltipX,
|
|
|
|
y: tooltipY,
|
|
|
|
title: tooltipTitle,
|
|
|
|
series: tooltipSeries,
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
).then(({ dispose }) => {
|
|
|
|
disposeTooltipComponent = dispose;
|
|
|
|
});
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
onUnmounted(() => {
|
|
|
|
if (disposeTooltipComponent) disposeTooltipComponent();
|
|
|
|
});
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
onDeactivated(() => {
|
|
|
|
tooltipShowing.value = false;
|
|
|
|
});
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
function handler(context) {
|
|
|
|
if (context.tooltip.opacity === 0) {
|
|
|
|
tooltipShowing.value = false;
|
|
|
|
return;
|
|
|
|
}
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
tooltipTitle.value = context.tooltip.title[0];
|
|
|
|
tooltipSeries.value = context.tooltip.body.map((b, i) => ({
|
|
|
|
backgroundColor: context.tooltip.labelColors[i].backgroundColor,
|
|
|
|
borderColor: context.tooltip.labelColors[i].borderColor,
|
|
|
|
text: b.lines[0],
|
|
|
|
}));
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
const rect = context.chart.canvas.getBoundingClientRect();
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
tooltipShowing.value = true;
|
|
|
|
tooltipX.value =
|
|
|
|
rect.left + window.pageXOffset + context.tooltip.caretX;
|
|
|
|
if (opts.position === "top") {
|
|
|
|
tooltipY.value = rect.top + window.pageYOffset;
|
|
|
|
} else if (opts.position === "middle") {
|
|
|
|
tooltipY.value =
|
|
|
|
rect.top + window.pageYOffset + context.tooltip.caretY;
|
|
|
|
}
|
|
|
|
}
|
2023-07-07 19:22:30 +00:00
|
|
|
|
2023-07-23 13:31:28 +00:00
|
|
|
return {
|
|
|
|
handler,
|
|
|
|
};
|
2023-07-07 19:22:30 +00:00
|
|
|
}
|