2023-07-27 05:31:52 +00:00
|
|
|
/*
|
2024-02-13 15:50:11 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
2023-07-27 05:31:52 +00:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-12-28 04:15:31 +00:00
|
|
|
import { onUnmounted, onDeactivated, ref } from 'vue';
|
2023-09-19 07:37:43 +00:00
|
|
|
import * as os from '@/os.js';
|
2022-08-30 15:24:33 +00:00
|
|
|
import MkChartTooltip from '@/components/MkChartTooltip.vue';
|
2022-06-25 09:05:35 +00:00
|
|
|
|
2022-12-26 04:26:21 +00:00
|
|
|
export function useChartTooltip(opts: { position: 'top' | 'middle' } = { position: 'top' }) {
|
2022-06-25 09:05:35 +00:00
|
|
|
const tooltipShowing = ref(false);
|
|
|
|
const tooltipX = ref(0);
|
|
|
|
const tooltipY = ref(0);
|
2023-12-26 05:19:35 +00:00
|
|
|
const tooltipTitle = ref<string | null>(null);
|
|
|
|
const tooltipSeries = ref<{
|
|
|
|
backgroundColor: string;
|
|
|
|
borderColor: string;
|
|
|
|
text: string;
|
|
|
|
}[] | null>(null);
|
2022-06-25 09:05:35 +00:00
|
|
|
let disposeTooltipComponent;
|
|
|
|
|
|
|
|
os.popup(MkChartTooltip, {
|
|
|
|
showing: tooltipShowing,
|
|
|
|
x: tooltipX,
|
|
|
|
y: tooltipY,
|
|
|
|
title: tooltipTitle,
|
|
|
|
series: tooltipSeries,
|
|
|
|
}, {}).then(({ dispose }) => {
|
|
|
|
disposeTooltipComponent = dispose;
|
|
|
|
});
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
|
|
|
if (disposeTooltipComponent) disposeTooltipComponent();
|
|
|
|
});
|
|
|
|
|
2022-12-28 04:15:31 +00:00
|
|
|
onDeactivated(() => {
|
|
|
|
tooltipShowing.value = false;
|
|
|
|
});
|
|
|
|
|
2022-06-25 09:05:35 +00: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;
|
2022-12-26 04:26:21 +00: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 09:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
handler,
|
|
|
|
};
|
|
|
|
}
|