ecomfe / vue-echarts

Vue.js component for Apache ECharts™.
https://vue-echarts.dev
MIT License
9.43k stars 1.48k forks source link

Could you please provide an example of getting the chart instance in Vue composition API #732

Closed dvago closed 11 months ago

dvago commented 11 months ago

Confirmation

Details

Hi there,

I'm reaching out as I would like to use the getWidth and getHeight methods to draw some graphic within a chart instance but I can't find a way to refer to the chart instance when initialized as per your documented demo:

<template>
  <v-chart class="chart" :option="option" />
</template>

see document, Vue 3 demo.

Could you please provide a sample and maybe update the README file?

Thanks in advance.

Justineo commented 11 months ago

All methods are exposed on the component instance and you can just use a template ref to access them.

<script setup>
import { ref, onMounted } from 'vue'
import VChart from 'vue-echarts'

const option = ref({ /* option */ })
const chart = ref(null)

onMounted(() => {
  console.log(chart.value.getWidth())
})
</script>

<template>
  <v-chart :option="option" ref="chart" />
</template>
kouts commented 6 months ago

For TypeScript that would be:

const chart = ref<InstanceType<typeof VChart> | null>(null);