且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Highcharts-如何更新角度系列?

更新时间:2023-11-21 23:03:10

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

I've made a simple online example for you with highcharts update in Angular app. I've used highcharts-angular official Highcharts wrapper which I recommend you (it can be downloaded here: https://github.com/highcharts/highcharts-angular). 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:

<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>

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

演示:
https://codesandbox.io/s/kw94l52z55