1

I have a component with two charts which I show on the button click. I use the same component of a chart but I give different chartData to it through two different getters. The chart looks like this:

<script>
import { Bar, mixins } from 'vue-chartjs';

export default {
extends: Bar,
mixins: [mixins.reactiveProp],
props: ['chartData', 'options'],
mounted() {
   this.renderChart(this.chartData, this.options);
  },
};
</script>

I know that reactiveProp renders a new chart if data has changed (and initialises nice animation). But currently two getters give the same data for this.chartData so when I try to switch the chart, it stays the same. Is there any way to update or destroy data so the chart will be re-rendered, even if it gets the same data from another getter? For some reasons, methods like update(), destroy(), clear() didn't work out but maybe I used them in the wrong place.

Here's my page with the chart:

<template>
 <div>
  <button @click="showAll = false">
          Show old users
  </button>
  <button @click="showAll = true">
          Show all users
  </button>
  <chart v-if="isChartData === true && showAll === false" :options="options" :chart-data="setDataOld" />
  <chart v-if="isChartData === true && showAll === true" :options="options" :chart-data="setData" />
 <div>
<template>
3
  • 1
    Use a key. Commented May 22, 2018 at 15:10
  • Thanks. I used this and now my chart is re-rendered every time I click the button but the console says: Avoid using non-primitive value as key, use string/number value instead. I added key like this: <chart v-if="isChartData === true && showAll === false" :options="options" :chart-data="setDataOld" :key="setDataOld" /> Commented May 22, 2018 at 15:21
  • I think you want :key="'setDataOld'". If you want to use an explicit string you need to use single quotes inside double quotes (or vice versa). Basically, you don't want your key to be an object, you want it to be some primitive value, and it doesn't have to be any specific value, it just needs to be different for chart one and chart two. Really, you could just use :key="1" and :key="2" in your case. Commented May 22, 2018 at 15:22

1 Answer 1

1

This should work. Note the keys.

<chart :key="1" v-if="isChartData === true && showAll === false" :options="options" :chart-data="setDataOld" />
<chart :key="2" v-if="isChartData === true && showAll === true" :options="options" :chart-data="setData" />

Essentially you want to be able to give Vue a hint about what differentiates the two charts so it knows explicitly which one to render instead of making use of its default component re-use strategy.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This solution is so simple, yet I would never think to use key apart from the case when you need to male a list with v-for. Now everything works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.