High Charts无法动态添加系列

问题描述 投票:4回答:2

我是第一次使用HighCharts,事实证明很难找到帮助我解决这个问题的来源。

我试图显示具有不同数量的系列的多个图表。我没有设置多个选项模板,而只想使用一个,并且每次都使用一个函数来自定义选项对象。在这里,我提供了一些模拟数据,用于表示从API获得的图表。在应用程序中,我计划从HTML中提供它。

由于我对HighCharts缺乏了解,我一直遇到各种各样的问题,现在我只是绕着圈子解决一个问题只针对过去的问题再次弹出我希望有人能指出我正确的方向。这是我的班级:

export class CompletenessTabComponent implements OnInit, AfterViewInit {
  @ViewChild('chartTarget') chartTarget: ElementRef;

  chart: Highcharts.ChartObject;

  dataObj = {"id":0,
    "tableName":"d1 vs d2",
    "data":{"d1Count":[50,45,55,60,70],
      "d2Count":[40,32,55,58,33],
      "Errors":[2,3,4,1,0]},
    "dates":[20180927,20180928,20181001,20181002,20181003]
  };

  constructor() { }

  setHighChartOptions(data, seriesNames, chartTitle ): any {
    count = 1;
    highChartOptions.title.text = chartTitle;
    highChartOptions.xAxis.data = data.dates;
    this.chart.series[0].setData(this.dataObj.data[0]);
    Object.keys(data.data).forEach((key) =>
      this.chart.addSeries({
        name: seriesNames[count],
        data: data.data[key],
        type: 'line'
      }) count ++
    );
    return highChartOptions;
  }
  getHighChartOptions(){
    return highChartOptions;
  }
  ngOnInit() {
    this.chart = chart(this.chartTarget.nativeElement, this.setHighChartOptions(this.dataObj, ['d1', 'd2', 'error'], this.dataObj.tableName));
    Object.keys(this.dataObj.data).forEach(key =>
    console.log(key, this.dataObj.data[key]))
  }
  ngAfterViewInit() {
  }

}

const highChartOptions = {
  colors:
    ['#058DC7', '#50B432', '#ED561B', '#DDDF00', '#24CBE5', '#64E572', '#FF9655', '#FFF263', '#6AF9C4'],

  credits: {
    enabled: false
  },
  chart: {
    backgroundColor: null,
  },
  legend: {
    layout: 'vertical',
    align: 'right',
    verticalAlign: 'top',
    itemStyle: {
      fontSize: '10px',
      fontWeight: 'normal'
    }
  },
  tooltip: {
    valuePrefix: '$'
  },
  title: {
    text: ''
  },
  xAxis: {
    type: 'date',
    data: [],
    title: 'date'
  },
  yAxis: {
    title: {
      align: 'middle',
      rotation: 0,
      text: ''
    }
  },
  plotOptions: {
    series: {
      marker: {
        enabled: false
      },
      shadow: false
    }
  },
  series: [{
    type: 'line',
    name: '',
    data: [null]
  }]
};

我一直收到的一些错误是:

在setHighChartOptions()中:无法读取undefined(at,this.chart.series [0])的属性'series',并且无法读取未定义的'keys'(在ForEach循环的开头)。

我认为最大的问题是图表对象是未定义的,因为我已经在类的顶部实例化了它然后onInit我设置了图表选项。

我希望你能发现我的错误并帮助我。谢谢。

javascript angular typescript highcharts
2个回答
1
投票

很可能,这是因为你试图引用尚未定义的系列。实际上,如果你是第一次调用setHighchartsOptions,你的图表还没有定义。

为了使它工作,请尝试首先初始化图表(甚至可能是空图表,没有任何数据),然后在Chart对象上调用另一个方法,如addSeries。这样的东西,但我没有看到你的整个应用程序,所以很难编写完全调整的解决方案。

ngOnInit() {
    this.chart = chart(this.chartTarget.nativeElement, {});
    this.chart.update(this.setHighChartOptions(this.dataObj, ['d1', 'd2', 'error'], this.dataObj.tableName))
    Object.keys(this.dataObj.data).forEach(key =>
    console.log(key, this.dataObj.data[key]))
  }

1
投票

首先,您必须小心在Angular项目中正确使用导入所有js包:

import { chart } from 'highcharts';
import * as Highcharts from 'highcharts';

此外,您必须使用“npm”检查您的包装是否正确安装?

毕竟,作为我的观点,这个例子可以帮助您更好地理解如何在项目中使用“highcharts”包,其他配置取决于您使用它的“highcharts”版本。

import { Component, ElementRef, ViewChild } from '@angular/core';
import { chart } from 'highcharts';
import * as Highcharts from 'highcharts';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Highcharts Sample';

  @ViewChild('chartTarget') chartTarget: ElementRef;

  chart: Highcharts.ChartObject;

  ngAfterViewInit() {
    const options: Highcharts.Options = {
      chart: {
        type: 'bar'
      },
      title: {
        text: 'Sample Title'
      },
      xAxis: {
        categories: ['Top Test', 'Test 2', 'Test3']
      },
      yAxis: {
        title: {
          text: 'Fox Rex'
        }
      },
      series: [{
        name: 'Tomi',
        data: [1, 0, 4]
      }, {
        name: 'Alex',
        data: [5, 7, 3]
      }]
    };

    this.chart = chart(this.chartTarget.nativeElement, options);
  }

  addSeries(){
    this.chart.addSeries({
      name:'Test',
      data:[2,3,7]
    })    
  }
}

和样本的HTML模板:

<div #chartTarget>
  chart target sample
</div>

此外,您必须将数据提供给highcharts,因为格式是标准格式,请在highcharts网站的示例中检查JSON样本数据格式。

© www.soinside.com 2019 - 2024. All rights reserved.