2023-02-13 20:40:31 -07:00
|
|
|
import { onUnmounted, onDeactivated, ref } from "vue";
|
2023-01-12 21:40:33 -07:00
|
|
|
import * as os from "@/os";
|
|
|
|
import MkChartTooltip from "@/components/MkChartTooltip.vue";
|
2022-06-25 03:05:35 -06:00
|
|
|
|
2023-02-13 20:40:31 -07:00
|
|
|
export function useChartTooltip(
|
|
|
|
opts: { position: "top" | "middle" } = { position: "top" },
|
|
|
|
) {
|
2022-06-25 03:05:35 -06:00
|
|
|
const tooltipShowing = ref(false);
|
|
|
|
const tooltipX = ref(0);
|
|
|
|
const tooltipY = ref(0);
|
|
|
|
const tooltipTitle = ref(null);
|
|
|
|
const tooltipSeries = ref(null);
|
|
|
|
let disposeTooltipComponent;
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
os.popup(
|
|
|
|
MkChartTooltip,
|
|
|
|
{
|
|
|
|
showing: tooltipShowing,
|
|
|
|
x: tooltipX,
|
|
|
|
y: tooltipY,
|
|
|
|
title: tooltipTitle,
|
|
|
|
series: tooltipSeries,
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
).then(({ dispose }) => {
|
2022-06-25 03:05:35 -06:00
|
|
|
disposeTooltipComponent = dispose;
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
if (disposeTooltipComponent) disposeTooltipComponent();
|
|
|
|
});
|
|
|
|
|
2023-02-13 20:40:31 -07:00
|
|
|
onDeactivated(() => {
|
|
|
|
tooltipShowing.value = false;
|
|
|
|
});
|
|
|
|
|
2022-06-25 03:05:35 -06:00
|
|
|
function handler(context) {
|
|
|
|
if (context.tooltip.opacity === 0) {
|
|
|
|
tooltipShowing.value = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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],
|
|
|
|
}));
|
|
|
|
|
|
|
|
const rect = context.chart.canvas.getBoundingClientRect();
|
|
|
|
|
|
|
|
tooltipShowing.value = true;
|
|
|
|
tooltipX.value = rect.left + window.pageXOffset + context.tooltip.caretX;
|
2023-02-13 20:40:31 -07:00
|
|
|
if (opts.position === "top") {
|
|
|
|
tooltipY.value = rect.top + window.pageYOffset;
|
|
|
|
} else if (opts.position === "middle") {
|
|
|
|
tooltipY.value = rect.top + window.pageYOffset + context.tooltip.caretY;
|
|
|
|
}
|
2022-06-25 03:05:35 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
handler,
|
|
|
|
};
|
|
|
|
}
|