Vue-chartjs: Use Reactive Data with <script> version of vue-chartjs

Created on 14 May 2017  ·  11Comments  ·  Source: apertureless/vue-chartjs

Hello,

So I want to use the reactive data feature on my chart. But the problem is, I'm building the Vue app via the <script> method (intentionally). No webpack/babel/ES6 involved.

The example in the docs only demonstrates how to implement with ES6. How do I implement Reactive Data in an environment without ES6 and imports? Is it possible?

❓ question

Most helpful comment

Hey @apertureless

So I asked the question above because I was writing an article on vue-chartjs. I'm done with it and you can read here

All 11 comments

Hello @yomete

yeah it is possible. You simply use it, without the imports :D
If you're using the browser version, everything is bundled into it. So if you for example console.log(VueChartJs) you will see the base chart types and also the mixin. Because VueChartJs is the object which holds all the modules and is in the global scope.

The only thing you need to do is to add the mixin to your chart component

...
mixins: [VueChartJs.mixins.reactiveProp]`
...

Awesome! Thanks @apertureless

Although, I still couldn't get the chart to work. I tried using the Reactive Data example in the docs.
I created a JSFiddle, if you could help look at it and see what's wrong, I'd be grateful!

JSFiddle Here

Vue mixins has to be passed as an array.

I think chartData is necessary in the props of the vue-component, how about it?
And I implemented it using computed.

JSFiddle

Nah, the mixin will create the chartData prop. But it needs to be passed as an array. Otherwise it will not extend the components props and methods.

Vue.component('line-chart', {
  extends: VueChartJs.Line,
  mixins: [VueChartJs.mixins.reactiveProp],
  props: ['chartData', 'options'],
  mounted () {
    console.log(this.chartData)
    this.renderChart(this.chartData, this.options)
  }
})

@euledge you are also not passing the mixin as an array :D So if you would have a button to randomize the data, it would also not work ;) Your example is working because the computed property is generating the data before the chart gets rendered. So its likeley the same as defining the random data on the data model.
;)

@apertureless Thank you for correcting errors in recognition.
But is wrong datacollection: null to collect datacollection: {} right?

JSFiddle

@apertureless @euledge Awesome! Got it to work!

Thanks guys 😄

@apertureless Thank you for correcting errors in recognition.
But is wrong datacollection: null to collect datacollection: {} right?

Both are right. You can initiate datacollection with null or {}

Hey @apertureless

So I asked the question above because I was writing an article on vue-chartjs. I'm done with it and you can read here

Nah, the mixin will create the chartData prop. But it needs to be passed as an array. Otherwise it will not extend the components props and methods.

Vue.component('line-chart', {
  extends: VueChartJs.Line,
  mixins: [VueChartJs.mixins.reactiveProp],
  props: ['chartData', 'options'],
  mounted () {
    console.log(this.chartData)
    this.renderChart(this.chartData, this.options)
  }
})

@euledge you are also not passing the mixin as an array :D So if you would have a button to randomize the data, it would also not work ;) Your example is working because the computed property is generating the data before the chart gets rendered. So its likeley the same as defining the random data on the data model.
;)

This should be added to the documentation.

@CyrusTheVirusG What exactly?

It is mentioned in the docs, that the mixin is creating the props itself: https://vue-chartjs.org/guide/#updating-charts

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rzb picture rzb  ·  4Comments

bdeanindy picture bdeanindy  ·  3Comments

Scalpel78 picture Scalpel78  ·  4Comments

jcalonso picture jcalonso  ·  4Comments

aido179 picture aido179  ·  3Comments