Skip to content
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

Chart.js is not reactive when data chagne #23

Open
ittus opened this issue May 19, 2018 · 3 comments
Open

Chart.js is not reactive when data chagne #23

ittus opened this issue May 19, 2018 · 3 comments

Comments

@ittus
Copy link

ittus commented May 19, 2018

Example: https://codesandbox.io/s/5mrzwp5q4p

In above example, when click on Change Data, data is changed but it's not updated in chart

<template>
  <div id="app">
    <button @click="changeData">Change Data</button>
    <chart :type="'pie'" :data="data" :options="options"></chart>
  </div>
</template>

<script>
import Chart from "./components/Chart.vue";

export default {
  name: "App",
  components: {
    Chart
  },
  data() {
    return {
      data: {
        labels: ["Sleeping", "Designing", "Coding", "Cycling"],
        datasets: [
          {
            data: [20, 40, 5, 35],
            backgroundColor: [
              "#1fc8db",
              "#fce473",
              "#42afe3",
              "#ed6c63",
              "#97cd76"
            ]
          }
        ]
      },
      options: {
        segmentShowStroke: false
      }
    };
  },
  methods: {
    changeData() {
      this.data = {
        labels: ["Sleeping", "Designing", "Coding", "Cycling"],
        datasets: [
          {
            data: [1, 2, 3, 4],
            backgroundColor: [
              "#1fc8db",
              "#fce473",
              "#42afe3",
              "#ed6c63",
              "#97cd76"
            ]
          }
        ]
      };
    }
  }
};
</script>

Especially, when data came from computed properties, chart will not be updated.
https://stackoverflow.com/q/50419627/2303610

@jakeYeager
Copy link

Re-render the chart with the new data: apertureless/vue-chartjs#353
Apertureless has some good docs for working with Vue/ChartJS and works in principal with this repo: http://vue-chartjs.org/#/home?id=reactive-data

@ittus
Copy link
Author

ittus commented Jun 19, 2018

@jakeYeager This repo doesn't use vue-chartjs. It consumes chartjs package directly.

@jakeYeager
Copy link

Right, if I understand what you are trying to do, you have to either bind the data as an input to your model, otherwise find a way to re-load the chart with the new data (i.e. v-if condition). apertureless offers some methods on how to achieve this. If I can find some time I'll try and codepen an example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants