首页 > 解决方案 > 如何在高角图表包装器中使用添加系列和更新方法?

问题描述

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

高图表包装

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

标签: highchartsangular5

解决方案


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

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

您必须更新整个组件,而不仅仅是图表属性。它可以通过更新 chartOptions 对象(添加新系列、点、标题等)和设置来实现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);
  }
}

演示:

文档参考:


推荐阅读