【问题标题】:How to update a chart using VueJS and ChartJS如何使用 VueJS 和 ChartJS 更新图表
【发布时间】:2019-09-05 04:17:51
【问题描述】:

我正在尝试使用 VueJS 和 ChartJS 更新图表,到目前为止,我可以访问对象的每个属性,但是如果我尝试更改对象的属性,则会出现错误:

[Vue warn]: Error in mounted hook: "TypeError: _chart_data_js__WEBPACK_IMPORTED_MODULE_5__.planetChartData.update is not a function"

我去了 ChartJS 的教程部分和问题部分,但我找不到这个问题的任何线索。 我觉得奇怪的是“推送”功能运行良好。 到目前为止,我尝试的是:

.vue 文件

<template>
            <div id="app" style="position: relative; height:500px; width:500px">
                <canvas :width="300" :height="300" id="planet-chart"></canvas>
            </div>


 </template>

...

import { mapActions, mapState } from 'vuex'
import Chart from 'chart.js';
import {planetChartData,pie} from './chart-data.js';
// import { mapActions } from 'vuex'
// import { connectionsAlive } from '../../api/mkt-api.js'
export default {
    mounted() {
        var x=this.createChart('planet-chart', this.planetChartData)
        planetChartData.data.labels.push('Janvier', 'Février')
        planetChartData.update();
    },
    data () {
        return {
            planetChartData: planetChartData,
        }
    },
    methods: {
        createChart(chartId, chartData) {
            const ctx = document.getElementById(chartId);
            const myChart = new Chart(ctx, {
            type: chartData.type,
            data: chartData.data,
            options: chartData.options,
            });
        }
    }
}
</script>

和.js文件

export const planetChartData = {
    type: 'bar',
    data: {
      labels: ['Janvier', 'Février', 'Mars', 'Avril'],
      datasets: [
        { // one line graph
          label: 'Number of users',
          data: [3018, 3407, 3109,1060],
          backgroundColor: [
            'rgba(54,73,93,.5)', // Blue
            'rgba(54,73,93,.5)',
            'rgba(54,73,93,.5)',
            'rgba(54,73,93,.5)'
          ],
          borderColor: [
            '#36495d',
            '#36495d',
            '#36495d',
            '#36495d'
          ],
          borderWidth: 3
        },
      ]
    },
    options: {
      responsive: true,
      lineTension: 1,
      scales: {
        yAxes: [{
          ticks: {
            beginAtZero: true,
            padding: 40,
          }
        }]
      }
    }
  }

也许我使用了错误的语法,如果有人有想法请告诉我,谢谢。 问候。

【问题讨论】:

    标签: javascript node.js vue.js chart.js vue-chartjs


    【解决方案1】:

    在 vue 文件中,planetChartData 是对 js 文件中对象“planetChartData”的引用。它不是对您在 createChart() 中创建的图表的引用

    你想要的是返回创建的图表,所以你可以在上面调用update()

    createChart(chartId, chartData) {
      const ctx = document.getElementById(chartId);
      const myChart = new Chart(ctx, {
        type: chartData.type,
        data: chartData.data,
        options: chartData.options,
      });
      return myChart // <<< this returns the created chart
    }
    

    然后在mounted中你可以这样做:

    var chart = this.createChart('planet-chart', planetChartData)
    chart.update();
    

    【讨论】:

    • 非常感谢它以这种方式工作,我也会尝试 vue-chartjs 方式
    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多