Highcharts - 如何更新角度系列?

2024-02-18

我在柱形图中更新系列数据时遇到问题。一开始,当我的模型为空时,我设置一个空数组作为系列,然后在ngOnchanges方法我映射我的modelData到匹配的格式。不幸的是,图表仍然是空的。 这是我的组件代码:

        export class ColumnChartComponent implements OnInit, OnChanges {
        highcharts = Highcharts;
        chartConstructor: string;
        @Input() dataModel: MyChartModel[];

        ngOnInit(): void {
            this.initChart();
        }

        ngOnChanges(): void {
            this.chartOptions = {
                series: this.getData()
            };        
        }

        private initChart(): void {
            this.highcharts = Highcharts;
            this.chartConstructor = "chart";
            this.chartOptions = {
                chart: {
                    type: "column"
                }
                //...
                // others settings
                //...
                series: []
            };
        }



        getData(){
        // some methods to map from dataModel to expected array
        // finally return something like this:
        return [{
            data: [2134000, 3168818, 2569759],
            name: 2015,
            type: "column"`enter code here`
        },{
            data: [2497680, 3195299, 2480444],
            name: 2014,
            type: "column"
        },{
            data: [2872000, 3604271, 2925828],
            name: 2016,
            type: "column"
        }]
    }

最后图表是空的。有趣的是,当我设置series: [{}] in initChart()方法,图表显示一系列。如果我设置series: [{},{}]图表将显示两个系列等。但我不知道我的中有多少个系列dataModel,所以我无法使用数组中合适的对象数量来设置数组。

我尝试过使用this.chartOptions.series.push(myObject)但它也不起作用。


我为您制作了一个简单的在线示例,其中包含 Angular 应用程序中的 highcharts 更新。我用过highcharts-angular我向您推荐官方 Highcharts 包装器(可以在此处下载:https://github.com/highcharts/highcharts-angular https://github.com/highcharts/highcharts-angular)。检查下面发布的代码和演示:

应用程序模块.ts:

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";
import { HighchartsChartModule } from "highcharts-angular";
import { ChartComponent } from "./chart.component";

import { AppComponent } from "./app.component";

@NgModule({
  declarations: [AppComponent, ChartComponent],
  imports: [BrowserModule, HighchartsChartModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

图表.组件.html:

<div class="boxChart__container">
  <div>
    <highcharts-chart
      id="container"
      [Highcharts]="Highcharts"
      [constructorType]="chartConstructor"
      [options]="chartOptions"
      [callbackFunction]="chartCallback"
      [(update)]="updateFromInput"
      [oneToOne]="true"
      style="width: 100%; height: 400px; display: block;"
    >
    </highcharts-chart>

    <button (click)="onInitChart()">Init Chart</button>
  </div>
</div>

图表.组件.ts:

import { Component, OnInit } from "@angular/core";
import * as Highcharts from "highcharts";
import * as HighchartsMore from "highcharts/highcharts-more";
import * as HighchartsExporting from "highcharts/modules/exporting";

HighchartsMore(Highcharts);
HighchartsExporting(Highcharts);

@Component({
  selector: "app-chart",
  templateUrl: "./chart.component.html"
})
export class ChartComponent implements OnInit {
  title = "app";
  chart;
  updateFromInput = false;
  Highcharts = Highcharts;
  chartConstructor = "chart";
  chartCallback;
  chartOptions = {
    series: [],
    exporting: {
      enabled: true
    },
    yAxis: {
      allowDecimals: false,
      title: {
        text: "Data"
      }
    }
  };

  constructor() {
    const self = this;

    // saving chart reference using chart callback
    this.chartCallback = chart => {
      self.chart = chart;
    };
  }

  ngOnInit() {}

  onInitChart() {
    const self = this,
      chart = this.chart;

    chart.showLoading();
    setTimeout(() => {
      chart.hideLoading();

      self.chartOptions.series = [
        {
          data: [2134000, 3168818, 2569759],
          name: 2015,
          type: "column"
        },
        {
          data: [2497680, 3195299, 2480444],
          name: 2014,
          type: "column"
        },
        {
          data: [2872000, 3604271, 2925828],
          name: 2016,
          type: "column"
        }
      ];

      self.updateFromInput = true;
    }, 2000);
  }
}

Demo:
https://codesandbox.io/s/kw94l52z55 https://codesandbox.io/s/kw94l52z55

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Highcharts - 如何更新角度系列? 的相关文章

随机推荐