How to use add series and update methods in the high chart wrapper for angular?

后端 未结 2 893
抹茶落季
抹茶落季 2020-12-21 07:34

I am using high chart wrapper in my angular5 app with the help of below link.

high chart wrapper

but how can I use addSeries() to add series into the existing

2条回答
  •  执笔经年
    2020-12-21 08:10

    how can I use addSeries() to add series into the existing chart and how can I update the properties of existing chart.

    When using highcharts-angular wrapper it is not recommended to use chart methods like addSeries() or update() directly on chart reference.

    You have to update a whole component, not only chart properties. It can be achieved by updating chartOptions object (add new series, point, title etc) and setting updateFlag = true. Check the code and demo posted below.

    app.module.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 {}
    

    chart.component.html:

    chart.component.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;
      updateFlag = false;
      Highcharts = Highcharts;
      chartConstructor = "chart";
      chartCallback;
      chartOptions = {
        series: [
          {
            data: [1, 2, 3, 6, 9]
          }
        ],
        exporting: {
          enabled: true
        },
        yAxis: {
          allowDecimals: false,
          title: {
            text: "Data"
          }
        }
      };
    
      constructor() {
        const self = this;
    
        this.chartCallback = chart => {
          // saving chart reference
          self.chart = chart;
        };
      }
    
      ngOnInit() {}
    
      updateChart() {
        const self = this,
          chart = this.chart;
    
        chart.showLoading();
        setTimeout(() => {
          chart.hideLoading();
    
          self.chartOptions.series = [
            {
              data: [10, 25, 15]
            },
            {
              data: [12, 15, 10]
            }
          ];
    
          self.chartOptions.title = {
            text: "Updated title!"
          };
    
          self.updateFlag = true;
        }, 2000);
      }
    }
    

    Demo:

    • https://codesandbox.io/s/oomo7424pz

    Docs reference:

    • updateFlag - https://github.com/highcharts/highcharts-angular#options-details

提交回复
热议问题