【问题标题】:Highchart in js does not work in Vue.jsjs 中的 Highchart 在 Vue.js 中不起作用
【发布时间】:2018-12-29 00:06:52
【问题描述】:

我在 html 文件中有一个可以正常工作的 highchart。当我将其移至 vue.js 项目时,它不起作用。 html文件中的代码如下

<html>
    <head>
        <title>
            Chart
        </title>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script src="https://code.highcharts.com/highcharts.js"></script>
        <script src="https://code.highcharts.com/modules/exporting.js"></script>
        <script src="https://code.highcharts.com/modules/export-data.js"></script>
    </head>
    <body>
        <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
        <script>
            Highcharts.setOptions({
                global: {
                    useUTC: false
                }
            });            
            const chart = new Highcharts.chart('container', {
                chart: {
                    type: 'spline',
                    animation: Highcharts.svg, 
                    marginRight: 10,
                },
                title: {
                    text: 'Live random data'
                },
                xAxis: {
                    type: 'datetime',
                    tickPixelInterval: null,
                    maxZoom: 10 * 1000
        },
                yAxis: {
                    title: {
                        text: 'Value'
                    },
                    plotLines: [{
                        value: 0,
                        width: 1,
                        color: '#808080'
                    }]
                },
                tooltip: {
                    formatter: function () {
                        return '<b>' + this.series.name + '</b><br/>' +
                            Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                            Highcharts.numberFormat(this.y, 2);
                    }
                },
                legend: {
                    enabled: false
                },
                exporting: {
                    enabled: false
                },
                series: []             
            });
            document.addEventListener('DOMContentLoaded', function() {                
                var i = 0;  
                setInterval(function () {
                if (chart.series.length > 0) {
                    var series = chart.series[0];
                        var x = (new Date()).getTime(),
                            y = Math.random();
                            if (series.data.length < 20)
                                series.addPoint([x, y], true, false);
                            else
                                series.addPoint([x, y], true, true);
                        console.log(1)
                    }
                else {
                    a = { name: 'aaa', data: [{x: (new Date()).getTime(), y:Math.random()}] };
                    chart.addSeries(a); 
                    console.log(chart.series[0].name)                       
                }                          
            }, 1000);
            });
        </script>
    </body>
</html>

vue中的代码如下

<template>
    <div class="container">
        <b-row>
            <b-col md="12" sm="12">
            <b-card header="Line Chart">
                <div class="chart-wrapper">
                <vue-highcharts :options="options" :Highcharts="Highcharts" ref="lineCharts"></vue-highcharts>
                </div>
            </b-card>
            </b-col>
        </b-row>
    </div>
</template>

<script>
import VueHighcharts from 'vue2-highcharts'
import Highcharts from 'highcharts'

export default {
  name: 'chartSample',
  components: {
    VueHighcharts
  },
  data () {
    return {
      Highcharts: Highcharts,
      options: {
        chart: {
          type: 'spline',
          animation: Highcharts.svg, // don't animate in old IE
          marginRight: 10
        },
        title: {
          text: 'Live random data'
        },
        xAxis: {
          type: 'datetime',
          tickPixelInterval: null,
          maxZoom: 10 * 1000
        },
        yAxis: {
          title: {
            text: 'Value'
          },
          plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
          }]
        },
        tooltip: {
          formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' +
              Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
              Highcharts.numberFormat(this.y, 2)
          }
        },
        legend: {
          enabled: false
        },
        exporting: {
          enabled: false
        },
        series: []
      }
    }
  },
  mounted () {
    let chart = this.$refs.lineCharts
    setInterval(function () {
      if (chart.series != null) {
        var series = chart.series[0]
        var x = (new Date()).getTime()
        var y = Math.random()
        if (series.data.length < 20) {
          series.addPoint([x, y], true, false)
        } else {
          series.addPoint([x, y], true, true)
        }
      } else {
        var a = { name: 'aaa', data: [{x: (new Date()).getTime(), y: Math.random()}] }
        chart.addSeries(a)        
      }
    }, 1000)
  }
}
</script>

我不知道是什么原因,但它不起作用。 似乎大多数部分未定义或页面中的图表对象有问题。但它在 html 文件中工作。

【问题讨论】:

    标签: javascript vue.js highcharts vuejs2 vue2-highcharts


    【解决方案1】:

    您不能直接访问 Highcharts 的 Vue 包装器的 series 数组。访问内部 Highcharts 对象调用getChart 方法:

    new Vue({
      el: "#app",
      name: 'chartSample',
      components: {
        VueHighcharts: VueHighcharts.default
      },
      data () {
        return {
          Highcharts: Highcharts,
          options: {
            chart: {
              type: 'spline',
              animation: Highcharts.svg, // don't animate in old IE
              marginRight: 10
            },
            title: {
              text: 'Live random data'
            },
            xAxis: {
              type: 'datetime',
              tickPixelInterval: null,
              maxZoom: 10 * 1000
            },
            yAxis: {
              title: {
                text: 'Value'
              },
              plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
              }]
            },
            tooltip: {
              formatter: function () {
                return '<b>' + this.series.name + '</b><br/>' +
                  Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' +
                  Highcharts.numberFormat(this.y, 2)
              }
            },
            legend: {
              enabled: false
            },
            exporting: {
              enabled: false
            },
            series: []
          }
        }
      },
      mounted () {
        var chart = this.$refs.lineCharts;
        setInterval(function () {
          var series = chart.getChart().series[0];
          if (series != null) {
            var x = (new Date()).getTime()
            var y = Math.random()
            if (series.data.length < 20) {
              series.addPoint([x, y], true, false)
            } else {
              series.addPoint([x, y], true, true)
            }
          } else {
            var a = { name: 'aaa', data: [{x: (new Date()).getTime(), y: Math.random()}] }
            chart.addSeries(a)        
          }
        }, 1000)
      }
    })
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue2-highcharts@1.2.4/dist/vue-highcharts.min.js"></script>
    <script src="https://code.highcharts.com/highcharts.js"></script>
    <div id="app">
      <div class="chart-wrapper">
        <vue-highcharts :options="options" :Highcharts="Highcharts" ref="lineCharts"></vue-highcharts>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 2019-04-02
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 2018-06-30
      相关资源
      最近更新 更多