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

Is it possible to use with axios? #103

Closed
altcointrading opened this issue May 6, 2017 · 3 comments
Closed

Is it possible to use with axios? #103

altcointrading opened this issue May 6, 2017 · 3 comments

Comments

@altcointrading
Copy link

Expected Behavior

Chart to be rendered with data coming via axios get request

Actual Behavior

Chart is rendered before data arrives, in spite of importing the reactive mixin chart never gets updated.

I am importing vue-chartjs in a component like so

<script>
import { Line, mixins } from 'vue-chartjs'
const { reactiveProp } = mixins
export default Line.extend({
  mixins: [reactiveProp],
  props: ['data', 'options'],
  mounted () {
    this.renderChart(this.data, this.options)
  }
})
</script>

This component then is imported into another component that gets the actual data from an API. In dev mode the chart does get drawn, however when I build the app for static hosting the problem appears.

Environment

  • vue.js version: 2
  • vue-chart.js version: latest
  • npm version: latest
@apertureless
Copy link
Owner

Hey @altcointrading

yes, it is working with axios and apis.

I guess it is working on your local dev environment, because the fetch of your json is fast enough to finish, before the chart get's drawn.

However, the mixin will never trigger! Because if you take a look in the docs the mixin will create a prop named chartData and add a vue watcher to it. You can see it in the source code . But you are passing your data to your own prop data so chartData where the watcher is attached to, is always empty.

Try to change it to chartData instead.

I don't know that you're planning, but you don't nessasary need the reactive prop. It is only usefull, if you plan to change the data, while the chart is rendered. Like updating the data. If you only want to fetch some data and then render the chart, you can simply use a v-if

Make a new data model isLoaded which is set to false and in your axios promise, after you transformed your data you set it to true.

And then just add the v-if=isLoaded to your chart componente template. This way the chart gets only rendered if all data is there. Thats how I did it for http://npm-stats.org

@altcointrading
Copy link
Author

altcointrading commented May 10, 2017

Hi @apertureless - thanks a lot. It seems the charting I need is a bit too much anyway so I'll be looking for another way but for the meantime with small amount of data points i got it working with

methods: {
    isLoaded () {
      return false
    }
  },

which switsches to true when data is processed, and

<template>
  <section class="container">
    <single-line v-if="isLoaded==true" :data="this.points" :options="{responsive: true, maintainAspectRatio: true}"/>
    <p>{{this.points.labels}}</p>
  </section>
</template>

Cheers!

@jiangdi0924
Copy link

useful

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

No branches or pull requests

3 participants