-
-
Notifications
You must be signed in to change notification settings - Fork 837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Can't get chart instance with composition api #1012
Comments
I'm seeing the same thing when using Vue 2.7 and |
It seems that
|
I'm seeing the same behavior when running in EDIT: I found a way seconds after posting. So the <template>
<div class="relative ">
<PieChart
ref="chart"
:chart-data="chartData"
:options="chartOptions"
:height="height"
:css-classes="cssClasses"
/>
<div class="absolute top-0 right-0 px-2 py-1 space-x-2">
<OutlineButton size="xs" class="py-1.5 !px-1.5 !text-base">
<Fa :icon="['far','file-csv']" fixed-width />
</OutlineButton>
<OutlineButton
size="xs"
class="py-1.5 !px-1.5 !text-base"
@click="onExportImageClick"
>
<Fa :icon="['far','file-image']" fixed-width />
</OutlineButton>
</div>
</div>
</template>
<script setup>
import { Chart, registerables } from 'chart.js'
import { computed, nextTick, ref } from 'vue'
import { PieChart } from 'vue-chart-3'
import { emptyChartPlugin } from '@/Modules/Charts/ChartHelpers'
import OutlineButton from '../OutlineButton.vue'
Chart.defaults.font.family = 'Inter'
Chart.register(...registerables, emptyChartPlugin)
const props = defineProps({
options: {
type: Object,
default: null,
},
data: {
type: Object,
default: null,
},
height: {
type: Number,
default: 400,
},
cssClasses: {
type: String,
default: '',
},
})
const chart = ref(null)
const chartData = computed(() => {
return props.data
})
const chartOptions = computed(() => {
return props.options
})
const onExportImageClick = () => {
//Correctly logs Chart instance for interaction
console.log(chart.value.chartInstance)
}
</script> |
Isn't the property name |
No, for some reason it isn't reflected like that when using Composition API. |
@dnkmdg You are using I just started with migrating. from Something like:
|
I'm seeing the same situation in the options api.
And when trying to access the chart property, that property (at least during I'm actually happy for any hints as to how to listen to clicks on the chart by other means. |
This is working for 'onHover' event, using vue-chartjs / vue3 / compostion API:
It's giving a typescript error for `chart.value.chart' because I cannot find the correct type definition for the chart isntance, but the code runs OK. |
import { ChartComponentRef } from "vue-chartjs";
...
const chart = ref<ChartComponentRef | null>(null) or if you need more strict typing based on the chart type: const chart = ref<ChartComponentRef<"line"> | null>(null)
For me the following works using @palsingh's workaround: <script setup lang="ts">
import { nextTick, onMounted, ref } from "vue";
import { Line } from 'vue-chartjs'
import { ChartComponentRef } from "vue-chartjs";
const chart = ref<ChartComponentRef | null>(null)
onMounted(async () => {
await nextTick()
console.log(chart.value?.chart)
})
</script>
<template>
<Line ref="chart" :data="<data>" :options="<options>" />
</template> |
@jeroenpelgrims
|
@jeroenpelgrims Just tried the solution but it doesn't seem to work anymore😢 Because I am getting the folowing runtime error Only modified it a little by adding some data and options.
I am using the latest packages
Does anyone has a working solution for nuxt with the composition api? |
Would you like to work on a fix?
Current and expected behavior
Can't get chart instance by ref. Chart ref value always return null
This is my code
Reproduction
https://stackblitz.com/edit/github-62t8lr?file=src/App.vue
chart.js version
^4.0.0
vue-chartjs version
^5.0.0
Possible solution
No response
The text was updated successfully, but these errors were encountered: