Highcharts-Vue等待数据然后更新系列

use*_*612 5 highcharts vue.js

似乎有一些例子说明了如何做类似的事情,但都与我的情况略有不同。我正在从 API(在 JS 文件中)加载一些股票数据,然后在我的 VUE 中使用它。我想使用从 API 数据编译的新数组来更新我的图表系列,但它不起作用,而且我没有收到任何错误。

我的 Vue 看起来像这样:

<template>
    <div>
        <highcharts :options="chartOptions" :updateArgs="[true, false]" ref="highcharts"></highcharts>
    </div>
</template>

<script>
    import appService from '../stock_prices'
    import {Chart} from 'highcharts-vue'

    export default {
      name: 'stocks',
      props: {
        msg: String
      },
      data () {
        return {
          chartOptions: {
              mySeries: [],
              info: {},
              updateArgs: [true, true, true],
              series: [{
                  data: [1,2,3,4,5,6,7]
                }],
             }
          }, 
        }
      }, //data

      components: {
        highcharts: Chart 
      },

      methods: {
        updateSeries() {

          for (var i = 0; i < this.info.stock_prices.length; i++) {
          this.mySeries.push([this.info.stock_prices[i].volume]);
          i++
          } 


          data: this.mySeries

        }
      }, //methods

      async created () {
        this.info = await appService.getPosts();
        this.updateSeries()
      }, //async created

} //export default
Run Code Online (Sandbox Code Playgroud)

显然,我想等待 API(在 appService 组件中)加载所有数据,然后使用它来创建更新的系列,但我不确定这实际上是发生的情况。

也许有一个重要的说明:如果我data: this.mySeries用类似的方法替换我的方法,data: [10,10,10,10,10,10]它仍然不成功 - 没有错误,并且该系列没有更新。

谢谢!

小智 4

请注意,您的数据不包含图表选项。另外,updateSeries()您正在更新没有任何内容的数据。它应该类似于下面的示例:

<template>
    <div>
        <highcharts :options="chartOptions" :updateArgs="[true, false]" ref="highcharts"></highcharts>
    </div>
</template>

<script>
    import appService from '../stock_prices'
    import {Chart} from 'highcharts-vue'

    export default {
      name: 'stocks',
      props: {
        msg: String
      },
      data () {
        return {
          mySeries: [],
          info: {},
          updateArgs: [true, true, true],
          chartOptions: {
            series: [{
                data: [1,2,3,4,5,6,7]
            }]
          }
        }
      }, //data

      components: {
        highcharts: Chart 
      },

      methods: {
        updateSeries() {
          for (var i = 0; i < this.info.stock_prices.length; i++) {
            this.mySeries.push([this.info.stock_prices[i].volume]);
          } 

          this.chartOptions.series[0].data: this.mySeries;
        }
      }, //methods

      async created () {
        this.info = await appService.getPosts();
        this.updateSeries()
      }, //async created

} //export default
Run Code Online (Sandbox Code Playgroud)

检查这个例子: