Skip to content

Commit 5c3eaac

Browse files
committed
Improvement - VueUiXy - Avoid time tag overflow
1 parent b233b30 commit 5c3eaac

File tree

2 files changed

+81
-3
lines changed

2 files changed

+81
-3
lines changed

TestingArena/ArenaVueUiXy.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ const config = computed(() => {
11541154
},
11551155
timeTag: {
11561156
...c.chart.timeTag,
1157-
useDefaultFormat: true,
1157+
useDefaultFormat: false,
11581158
timeFormat: 'yyyy-MM-dd HH:mm:ss',
11591159
// customFormat: ({ absoluteIndex }) => {
11601160
// return String(absoluteIndex) + 'TEST'

src/components/vue-ui-xy.vue

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,8 +2575,85 @@ useTimeLabelCollision({
25752575
25762576
const useCustomFormatTimeTag = ref(false);
25772577
2578+
const timeTagEl = ref(null);
2579+
const timeTagInnerW = ref(200)
2580+
2581+
const activeIndex = computed(() =>
2582+
(selectedSerieIndex.value ?? selectedMinimapIndex.value ?? 0)
2583+
)
2584+
2585+
function timeTagMeasuredW() {
2586+
const w = Math.ceil(timeTagInnerW.value || 200)
2587+
return Math.min(Math.max(w, 1), 200)
2588+
}
2589+
2590+
function timeTagX() {
2591+
const w = timeTagMeasuredW();
2592+
const W_FO = 200;
2593+
const sectionsX = Math.max(1, maxSeries.value);
2594+
const sectionX = drawingArea.value.width / sectionsX;
2595+
const centerX = drawingArea.value.left + activeIndex.value * sectionX + sectionX / 2;
2596+
const desiredX = centerX - w / 2 - (W_FO - w) / 2;
2597+
const minX = drawingArea.value.left - (W_FO - w) / 2;
2598+
const maxX = drawingArea.value.right - (W_FO + w) / 2;
2599+
const clamped = Math.max(minX, Math.min(desiredX, maxX));
2600+
return checkNaN(clamped);
2601+
}
2602+
2603+
onMounted(() => {
2604+
let observedTimeTagEl = null;
2605+
let raf = null;
2606+
2607+
const setW = (w) => {
2608+
cancelAnimationFrame(raf)
2609+
raf = requestAnimationFrame(() => {
2610+
timeTagInnerW.value = Math.min(Math.max(Math.ceil(w || 0), 1), 200);
2611+
});
2612+
}
2613+
2614+
const ro = new ResizeObserver((entries) => {
2615+
let entry = entries.find(e => e.target === observedTimeTagEl) || entries[0];
2616+
if (!entry) return;
2617+
setW(entry.contentRect.width || 200)
2618+
});
2619+
2620+
const stop = watchEffect((onInvalidate) => {
2621+
const el = timeTagEl.value;
2622+
if (observedTimeTagEl && observedTimeTagEl !== el) {
2623+
ro.unobserve(observedTimeTagEl);
2624+
observedTimeTagEl = null;
2625+
}
2626+
2627+
if (el && el !== observedTimeTagEl) {
2628+
nextTick(() => {
2629+
if (el.offsetParent === null) return;
2630+
setW(el.offsetWidth || el.getBoundingClientRect().width || 200)
2631+
})
2632+
ro.observe(el);
2633+
observedTimeTagEl = el;
2634+
}
2635+
2636+
onInvalidate(() => {
2637+
if (observedTimeTagEl) {
2638+
ro.unobserve(observedTimeTagEl);
2639+
observedTimeTagEl = null;
2640+
}
2641+
});
2642+
});
2643+
2644+
onBeforeUnmount(() => {
2645+
try {
2646+
if (observedTimeTagEl) ro.unobserve(observedTimeTagEl);
2647+
ro.disconnect();
2648+
stop();
2649+
} catch {
2650+
// ignore
2651+
}
2652+
});
2653+
});
2654+
25782655
const timeTagContent = computed(() => {
2579-
if ([null, undefined].includes(selectedSerieIndex.value) && [null, undefined].includes(selectedMinimapIndex.value)) return ''
2656+
if ([null, undefined].includes(selectedSerieIndex.value) && [null, undefined].includes(selectedMinimapIndex.value)) return '';
25802657
25812658
const index = (selectedSerieIndex.value != null ? selectedSerieIndex.value : 0) || (selectedMinimapIndex.value != null ? selectedMinimapIndex.value : 0);
25822659
@@ -3698,9 +3775,10 @@ defineExpose({
36983775
<g v-if="FINAL_CONFIG.chart.timeTag.show && (![null, undefined].includes(selectedSerieIndex) || ![null, undefined].includes(selectedMinimapIndex))"
36993776
style="pointer-events:none">
37003777
<foreignObject
3701-
:x="drawingArea.left + (drawingArea.width / maxSeries) * ((selectedSerieIndex !== null ? selectedSerieIndex : 0) || (selectedMinimapIndex !== null ? selectedMinimapIndex : 0)) - 100 + (drawingArea.width / maxSeries / 2)"
3778+
:x="timeTagX()"
37023779
:y="drawingArea.bottom" width="200" height="40" style="overflow: visible !important;">
37033780
<div
3781+
ref="timeTagEl"
37043782
data-cy="time-tag"
37053783
class="vue-ui-xy-time-tag"
37063784
:style="`width: fit-content;margin: 0 auto;text-align:center;padding:3px 12px;background:${FINAL_CONFIG.chart.timeTag.backgroundColor};color:${FINAL_CONFIG.chart.timeTag.color};font-size:${FINAL_CONFIG.chart.timeTag.fontSize}px`"

0 commit comments

Comments
 (0)