【问题标题】:How to use Google Charts with Vue.js library?如何在 Vue.js 库中使用 Google Charts?
【发布时间】:2020-10-15 08:24:03
【问题描述】:

我正在尝试使用 Vue.js 库使用 Google Charts 制作图表,但我不知道如何添加到 div。

这是我尝试做的,这是如何使用香草 javascript (Here the code example of the documentation) 添加图表,我尝试适应 vue,但没有发生任何事情:

google.charts.load('current', {'packages': ['corechart']});

Vue.component('line-char', {
    data: function(){
        // Create the data table.
        var data = google.visualization.arrayToDataTable([
            ['Tiempo', 'Temperatura'],
            [1,  1000],
            [2,  1170],
            [3,  660],
            [4,  1030]
        ]);

        // Set chart options
        var options = {
            'title': 'Data Line',
            'width': '100%',
            'height': 250,
            legend: { position: 'bottom' }
        };

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    },
    template: '<div v-model="chart_div"></div>'
});

html:

<div id="component">
    <line-chart></line-chart>
</div>

【问题讨论】:

    标签: javascript html vue.js


    【解决方案1】:

    您要做的是为您的&lt;div&gt; 使用ref,然后注册回调以在组件的mounted 挂钩中绘制图表。

    // Load library
    google.charts.load('current', {'packages':['corechart']})
    
    const lineChartOptions = {
      title: 'Data Line',
      width: '100%',
      height: 250,
      legend: { position: 'bottom' }
    }
    
    Vue.component('LineChart', {
      template: `<div ref="chart"></div>`, // ? set ref here
      data: () => ({
        headings: ['Tiempo', 'Temperatura'],
        chartData: [
          [1,  1000],
          [2,  1170],
          [3,  660],
          [4,  1030]
        ]
      }),
      methods: {
        drawChart () {
          const dataTable = google.visualization.arrayToDataTable([
            this.headings,
            ...this.chartData
          ], false) // ? don't forget "false" here to indicate the first row as labels
    
          const chart = new google.visualization.LineChart(this.$refs.chart) // ? use ref here
          chart.draw(dataTable, lineChartOptions)
        }
      }
      mounted () {
        // set the library loaded callback here
        google.charts.setOnLoadCallback(() => this.drawChart())    
      }
    })
    

    正如Matt 所提到的,如果您的组件的模板真的是空的,除了单个&lt;div&gt;,您可以使用$el 属性来挂载图表而无需打扰参考

    Vue.component('LineChart', {
      template: `<div></div>`,
      // ...
      methods: {
        drawChart () {
          // ...
          const chart = new google.visualization.LineChart(this.$el)
          chart.draw(this.dataTable, options)
        }
      }
    })
    

    【讨论】:

    • +upvote 虽然在这种情况下我可能会选择this.$el 而不是参考。
    • @Matt 当然,如果模板中没有其他内容,那也可以。
    • @Phil 谢谢,但我得到另一个错误:Cannot read property 'LineChart' of undefined,显然,google.charts 定义不正确。你是怎么定义google.charts.load('current', {'packages': ['corechart']});.的?
    • @DrakoPD 我以为您已经解决了所有问题。我会更新我的答案
    • @Phil,谢谢现在正在工作。我有一些东西,但显然,我的方法没有奏效。你过去的代码有什么问题?
    【解决方案2】:

    根据更新,此解决方案不再可取

    更简单的用法是使用vue-google-charts plugin

    示例代码:

      <GChart
        type="ColumnChart"
        :data="chartData"
        :options="chartOptions"
      />
    
     chartData: [
                ['Year', 'Sales', 'Expenses', 'Profit'],
                ['2014', 1000, 400, 200],
                ['2015', 1170, 460, 250],
                ['2016', 660, 1120, 300],
                ['2017', 1030, 540, 350],
            ],
            chartOptions: {
                chart: {
                    title: 'Company Performance',
                    subtitle: 'Sales, Expenses, and Profit: 2014-2017',
                },
            },
    

    【讨论】:

    • vue-google-charts 自 2020 年以来似乎已被废弃。我在响应功能方面遇到了重大生产问题,但没有解决方案有效。我必须摆脱这个并找到替代方案
    • 感谢您让我知道@MahanHazratiSagharchi。我在生产中使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-16
    • 2021-10-27
    • 2021-09-29
    • 2015-07-10
    相关资源
    最近更新 更多