Skip to content

Commit e0f4610

Browse files
committed
use ResizeObserver to get element size, fix plot layout in transform: scale elements
1 parent 0fa901f commit e0f4610

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/components/Plot.vue

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup>
22
import { computed, watch, Fragment, useAttrs, useSlots, useTemplateRef, onMounted, reactive, provide } from 'vue'
3-
import { reactiveComputed, useElementSize } from '@vueuse/core'
3+
import { reactiveComputed, useResizeObserver } from '@vueuse/core'
44
import { baseParse } from '@vue/compiler-core'
55
import { theme_base, theme_default, themeBuild, themeMerge, themePreprocess } from '../js/theme'
66
import { str_c, serializeSVG } from '../js/utils'
@@ -479,12 +479,14 @@ onMounted(() => {
479479
wrapperRef.value.style.height = str_c(v, 'px') ?? null
480480
}, { immediate: true })
481481
})
482-
const { width: w, height: h } = useElementSize(plotRef)
483-
watch([w, h], ([w, h], [ow, oh]) => {
482+
let oldSize = { width: 0, height: 0 }
483+
useResizeObserver(plotRef, (e) => {
484+
let { width: w, height: h } = e[0].contentRect
484485
if (wrapperRef.value.style.width) width.value = w
485486
if (wrapperRef.value.style.height) height.value = h
486-
if ((w > 0 || h > 0) && (ow > 0 || oh > 0))
487-
emit('resize', { width: w, height: h }, { width: ow, height: oh })
487+
if ((w > 0 || h > 0) && (oldSize.width > 0 || oldSize.height > 0))
488+
emit('resize', { width: w, height: h }, oldSize)
489+
oldSize = { width: w, height: h }
488490
})
489491
const panelStyle = computed(() => {
490492
return {
@@ -520,7 +522,8 @@ defineExpose({
520522
:coord-levels="coordLevels" :levels="levels" :scales="$scales" :axes="axes" :theme="theme"
521523
:selections="selections" v-model:transition="transition" v-bind="vBind.plot"
522524
v-model:selection-preview="selectionPreview" v-model:selection-preview-theme="selectionPreviewTheme"
523-
:action="action" :clip="clip" :render="render" :legendTeleport="legendTeleport" @select="(d, e) => emit('select', d, e)" />
525+
:action="action" :clip="clip" :render="render" :legendTeleport="legendTeleport"
526+
@select="(d, e) => emit('select', d, e)" />
524527
<div class="vvplot-panel-container" :style="panelStyle">
525528
<div class="vvplot-panel" v-if="vnodes.dom.panel?.length">
526529
<component v-for="c in vnodes.dom.panel" :is="c" />

src/core/CorePlot.vue

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<script setup>
22
defineOptions({ inheritAttrs: false })
3-
import { computed, watch, inject, useTemplateRef, useId } from 'vue'
3+
import { ref, computed, watch, inject, useTemplateRef, useId } from 'vue'
44
import { GPlot, GAxis } from '#base/js/plot'
55
import { unique, oob_squish_any, oob_squish_infinite, dropNull, emitEvent, plus } from '#base/js/utils'
6-
import { reactiveComputed, useElementSize } from '@vueuse/core'
6+
import { reactiveComputed, useResizeObserver } from '@vueuse/core'
77
import CoreAxis from './axis/CoreAxis.vue'
88
import CoreGridH from './grid/CoreGridH.vue'
99
import CoreGridV from './grid/CoreGridV.vue'
@@ -52,7 +52,13 @@ const transition = defineModel('transition')
5252
5353
const svgRef = useTemplateRef('svg')
5454
const layers = useTemplateRef('layers')
55-
const { width, height } = useElementSize(svgRef)
55+
const width = ref(0), height = ref(0)
56+
57+
useResizeObserver(svgRef, entries => {
58+
let { width: w, height: h } = entries[0].contentRect
59+
width.value = w
60+
height.value = h
61+
})
5662
5763
const gplot = computed(() => new GPlot(schema, props.layers))
5864
const vplot = computed(() => {

0 commit comments

Comments
 (0)