如何在 angular 的高图表包装器中使用添加系列和更新方法?

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

我在下面 link 的帮助下在我的 angular5 应用程序中使用高图表包装器。

high chart wrapper

但是如何使用 addSeries() 将系列添加到现有图表中以及如何更新现有图表的属性。

这里有一个非常有用的答案,可以帮助您学习如何更新图表。

https://www.highcharts.com/demo/chart-update

说明方法chart.update

 chart.update({
    chart: {
      inverted: false,
      polar: false
    },
    subtitle: {
      text: 'Plain'
    }
  });

添加系列使用以下方法

chart.addSerie(serie,true);

flag 'true'这里相当于chart.redraw();

var chart = new Highcharts.Chart(options);
chart.addSeries({                        
    name: array.name,
    data: array.value
});

如果您要添加多个系列,您应该将重绘标志设置为 false,然后手动调用重绘,这样会快得多。

var chart = new Highcharts.Chart(options);
chart.addSeries({                        
    name: 'Bill',
    data: [1,2,4,6]
}, false);
chart.addSeries({                        
    name: 'John',
    data: [4,6,4,6]
}, false);
chart.redraw();

有关更多信息和方法,您可以访问官方 Highcharts API 页面:

https://api.highcharts.com/class-reference/Highcharts.Chart

当使用 angular-highcharts 包装器作为 从 'angular-highcharts';

导入 { 图表}

创建如下图表

  chart = new Chart({
    chart: {
      type: 'line'
    },
    title: {
      text: 'Linechart'
    },
    credits: {
      enabled: false
    },
    series: [
      {
        name: 'Line 1',
        data: [1, 2, 3]
      }
    ]
  });

现在您可以在此

上调用所有 API 方法

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

使用 highcharts-angular 包装器时,不建议直接在图表参考上使用 addSeries()update() 等图表方法。

您必须更新整个组件,而不仅仅是图表属性。它可以通过更新 chartOptions object(添加新系列、点、标题等)和设置 updateFlag = true 来实现。检查下面发布的代码和演示。

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:

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

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);
  }
}

演示:

文档参考: