Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

vue.js - How to make chartist update on Vuejs

First of all a happy new year to everyone.

I would like to call update at chartist-js.

main.js

import Chartist from "chartist";

Vue.prototype.$Chartist = Chartist;

Component.vue

<chart-card
                    :chart-data="performanceUser.data"
                    :chart-options="performanceUser.options"
                    chart-type="Line"
                    data-background-color="green">
            </chart-card>

Component.vue -> methods

getStatsUser(){
      UsersAPI.getUserPerformance(this.users.filters.user.active).then(r => {
        this.performanceUser.data.labels = r.data.labels;
        this.performanceUser.data.series = r.data.series;

        this.$Chartist.update();

      });
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple of things you need to do. First, you don't need to patch Vue prototype object with Chartist instance. Just import Chartist package wherever you need it. Prototype patching is required when you need singleton or stateful construct.

Second, I assume all your chart rendering logic will be inside your chart-card component. It will roughly look like:

<template>
    <!-- Use vue.js ref to get DOM Node reference -->
    <div class="chart-container" ref="chartNode"></div>
</template>
<script>
import Chartist from 'chartist';

export default {

    // data is an object containing Chart X and Y axes data
    // Options is your Chartist chart customization options
    props: ['data', 'options'],

    // Use of mounted is important.
    // Otherwise $refs will not work
    mounted() {

        if (this.data && this.options) {
            // Reference to DOM Node where you will render chart using Chartist
            const divNode = this.$refs.chartNode;

            // Example of drawing Line chart
            this.chartInstance = new Chartist.Line(divNode, this.data, this.options);
        }

    },

    // IMPORTANT: Vue.js is Reactive framework.
    // Hence watch for prop changes here
    watch: {
        data(newData, oldDate) {
            this.chartInstance.update(newData, this.options);
        },

        options(newOpts) {
            this.chartInstance.update(this.data, newOpts);
        }
    }
}
</script>

Finally, in your calling component, you will have:

getStatsUser() {
    UsersAPI.getUserPerformance(this.users.filters.user.active).then(r => {
        // Since component is watching props,
        // changes to `this.performanceUser.data`
        // should automatically update component
        this.performanceUser.data = {
            labels: r.data.labels,
            series: r.data.series
        };
    });
}

I hope this gives you an idea of how to build Vue wrapper for Chartist graphs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...