refactor: update tooltips

This commit is contained in:
Fine 2023-01-05 15:24:16 +08:00
parent 08239622ee
commit 313abd3146
2 changed files with 25 additions and 67 deletions

View File

@ -22,6 +22,7 @@ limitations under the License. -->
top: menuPos.y + 'px', top: menuPos.y + 'px',
left: menuPos.x + 'px', left: menuPos.x + 'px',
}" }"
@mouseenter="hideTooltips"
> >
<div class="tools" @click="associateMetrics" v-if="associate.length"> <div class="tools" @click="associateMetrics" v-if="associate.length">
{{ t("associateMetrics") }} {{ t("associateMetrics") }}
@ -30,16 +31,6 @@ limitations under the License. -->
{{ t("viewTrace") }} {{ t("viewTrace") }}
</div> </div>
</div> </div>
<div
class="menus"
v-show="showTooltip"
:style="{
top: tooltipPos.y + 'px',
left: tooltipPos.x + 'px',
}"
>
<div v-for="(item, index) in tipValues" :key="index">{{ item.name }}: {{ item.value }}</div>
</div>
<el-drawer <el-drawer
v-model="showTrace" v-model="showTrace"
size="100%" size="100%"
@ -76,8 +67,6 @@ limitations under the License. -->
}); });
const menuPos = reactive<{ x: number; y: number }>({ x: NaN, y: NaN }); const menuPos = reactive<{ x: number; y: number }>({ x: NaN, y: NaN });
const showTooltip = ref<boolean>(false); const showTooltip = ref<boolean>(false);
const tooltipPos = reactive<{ x: number; y: number }>({ x: NaN, y: NaN });
const tipValues = ref<{ name: string; value: number }[]>([]);
const props = defineProps({ const props = defineProps({
height: { type: String, default: "100%" }, height: { type: String, default: "100%" },
width: { type: String, default: "100%" }, width: { type: String, default: "100%" },
@ -124,6 +113,9 @@ limitations under the License. -->
if (!chartRef.value) { if (!chartRef.value) {
return; return;
} }
instance.dispatchAction({
type: "hideTip",
});
visMenus.value = true; visMenus.value = true;
const w = chartRef.value.getBoundingClientRect().width || 0; const w = chartRef.value.getBoundingClientRect().width || 0;
const h = chartRef.value.getBoundingClientRect().height || 0; const h = chartRef.value.getBoundingClientRect().height || 0;
@ -135,17 +127,20 @@ limitations under the License. -->
if (h - params.event.offsetY < 50) { if (h - params.event.offsetY < 50) {
menuPos.y = params.event.offsetY - 40; menuPos.y = params.event.offsetY - 40;
} else { } else {
menuPos.y = params.event.offsetY + 2; menuPos.y = params.event.offsetY;
} }
}); });
if (props.option.series.type === "sankey") { if (props.option.series.type === "sankey") {
return; return;
} }
instance.on("mouseover", (params: EventParams) => { instance.on("mouseover", () => {
setTooltips(params); visMenus.value = false;
}); });
instance.on("mouseout", () => { instance.on("mouseout", () => {
showTooltip.value = false; showTooltip.value = false;
instance.dispatchAction({
type: "hideTip",
});
}); });
document.addEventListener( document.addEventListener(
"click", "click",
@ -156,8 +151,7 @@ limitations under the License. -->
visMenus.value = false; visMenus.value = false;
showTooltip.value = false; showTooltip.value = false;
instance.dispatchAction({ instance.dispatchAction({
type: "updateAxisPointer", type: "hideTip",
currTrigger: "leave",
}); });
}, },
true, true,
@ -165,33 +159,6 @@ limitations under the License. -->
}, 1000); }, 1000);
} }
function setTooltips(params: EventParams | undefined) {
if (!chartRef.value || !params) {
return;
}
tipValues.value = [];
showTooltip.value = true;
visMenus.value = false;
const w = chartRef.value.getBoundingClientRect().width || 0;
const h = chartRef.value.getBoundingClientRect().height || 0;
if (w - params.event.offsetX > 50) {
tooltipPos.x = params.event.offsetX;
} else {
tooltipPos.x = params.event.offsetX - 30;
}
if (h - params.event.offsetY < 50) {
tooltipPos.y = params.event.offsetY - 20;
} else {
tooltipPos.y = params.event.offsetY + 2;
}
for (const item of props.option.series) {
tipValues.value.push({
name: item.name,
value: item.data[params.dataIndex] ? item.data[params.dataIndex][1] : null,
});
}
}
function associateMetrics() { function associateMetrics() {
emits("select", currentParams.value); emits("select", currentParams.value);
updateOptions(currentParams.value || undefined); updateOptions(currentParams.value || undefined);
@ -211,16 +178,10 @@ limitations under the License. -->
setOptions(options || props.option); setOptions(options || props.option);
} else { } else {
instance.dispatchAction({ instance.dispatchAction({
type: "updateAxisPointer", type: "showTip",
dataIndex: params ? params.dataIndex : props.filters.dataIndex, dataIndex: params ? params.dataIndex : props.filters.dataIndex,
seriesIndex: params ? params.seriesIndex : 0, seriesIndex: params ? params.seriesIndex : 0,
}); });
const ids = props.option.series.map((_: unknown, index: number) => index);
instance.dispatchAction({
type: "highlight",
dataIndex: params ? params.dataIndex : props.filters.dataIndex,
seriesIndex: ids,
});
} }
} }
@ -234,6 +195,13 @@ limitations under the License. -->
visMenus.value = true; visMenus.value = true;
} }
function hideTooltips() {
const instance = getInstance();
instance.dispatchAction({
type: "hideTip",
});
}
watch( watch(
() => props.option, () => props.option,
(newVal, oldVal) => { (newVal, oldVal) => {

View File

@ -91,17 +91,13 @@ limitations under the License. -->
}); });
const color: string[] = chartColors(keys); const color: string[] = chartColors(keys);
const tooltip = { const tooltip = {
trigger: "none", trigger: "axis",
axisPointer: { textStyle: {
type: "cross", fontSize: 12,
color: "#333",
}, },
// trigger: "axis", enterable: true,
// textStyle: { confine: true,
// fontSize: 12,
// color: "#333",
// },
// enterable: true,
// confine: true,
extraCssText: "max-height: 300px; overflow: auto; border: none;", extraCssText: "max-height: 300px; overflow: auto; border: none;",
}; };
const tips = { const tips = {
@ -131,12 +127,6 @@ limitations under the License. -->
color: props.theme === "dark" ? "#fff" : "#333", color: props.theme === "dark" ? "#fff" : "#333",
}, },
}, },
axisPointer: {
label: {
color: "#333",
backgroundColor: "rgba(255, 255, 255, 0.8)",
},
},
grid: { grid: {
top: showEchartsLegend(keys) ? 35 : 10, top: showEchartsLegend(keys) ? 35 : 10,
left: 0, left: 0,