【问题标题】:Highcharts in Angular - accessing the APIAngular 中的 Highcharts - 访问 API
【发布时间】:2020-11-06 14:47:09
【问题描述】:

我想访问 highcharts API,特别是我想动态添加或删除一个系列。基本上我无法访问任何方法,或者我不确定如何访问。

我的代码如下:

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-widget-area',
  templateUrl: './area.component.html',
  styleUrls: ['./area.component.scss']
})
export class AreaComponent implements OnInit {

  chartOptions: {};

  Highcharts = Highcharts;

  constructor() { }

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

 myChart(){
  this.chartOptions = {
    chart: {
        type: 'line'
    },
    title: {
        text: 'Historic and Estimated Worldwide Population Growth by Region'
    },
    subtitle: {
        text: 'Source: Wikipedia.org'
    },
    xAxis: {
        categories: ['1750', '1800', '1850', '1900', '1950', '1999', '2050'],
        tickmarkPlacement: 'on',
        title: {
            enabled: false
        }
    },
    yAxis: {
        title: {
            text: 'Billions'
        },
        labels: {
            formatter () {
                return this.value / 1000;
            }
        }
    },
    tooltip: {
        split: true,
        valueSuffix: ' millions'
    },
    plotOptions: {
      line: {
          dataLabels: {
              enabled: true
          },
          enableMouseTracking: false
      }
  },
    series: [{
        name: 'Asia',
        data: [502, 635, 809, 947, 1402, 3634, 5268]
    }, {
        name: 'Africa',
        data: [106, 107, 111, 133, 221, 767, 1766]
    }, {
        name: 'Europe',
        data: [163, 203, 276, 408, 547, 729, 628]
    }, {
        name: 'America',
        data: [18, 31, 54, 156, 339, 818, 1201]
    }, {
        name: 'Oceania',
        data: [2, 2, 2, 6, 13, 30, 46]
    }]
};

  setTimeout(() => {
  window.dispatchEvent(
    new Event('resize')
  );
},300);
 }
}

HTML 是这样的:

<highcharts-chart
  [Highcharts]="Highcharts"
  [options]="chartOptions"
  style="width: 100%; height: 400px; display: block;">
</highcharts-chart>

如何重写这段代码,使其可以访问addSeries等方法,从而动态显示新数据?

我看过这两个帖子,它们看起来很有帮助:

Highcharts add series dynamically

Adding new HighChart Series

问题是我不能像他们那样访问这些方法。

当我尝试实现 chart = new Highcharts.Chart(options);和他们一样,我只会出错。

先谢谢了。

【问题讨论】:

    标签: javascript angular typescript highcharts angular-material


    【解决方案1】:

    需要分两步完成。 首先,您更改 chartOption 对象内的值。 其次,您更新图表。

    例如,在您的组件中添加这样的函数:

    update(){
      this.myOptions.series = [{
        name: 'Oceania',
        data: [2, 2, 2, 6, 13, 30, 46]
      }]
      this.updateFromInput = true;
    }
    

    在你的 html 中:

    <highcharts-chart 
      [Highcharts]="Highcharts"
      [options]="myOptions"
      [(update)]="updateFromInput">
    </highcharts-chart>
    <button (click)="update()" class="btn">Add</button>
    

    一个基本的例子here

    【讨论】:

    • 非常感谢。这对我帮助很大!还有一个问题。添加新数据后,系列数组会发生变化,但图表上仍然可以看到旧数据。如何删除这些旧信息?
    • 我认为这是因为您忘记使用布尔值“updateFromInput”更新图表。
    • 我确实将它包含在 TS 和 HTML 文件中,如示例中所示。我认为问题可能是图表没有重绘?
    • 我想是这样,但没有你的代码,我很难提供帮助。也许你可以更新你的帖子?
    • 这篇文章的重点是向系列中添加数据,您已经回答了我的问题。现在的问题是如何有效地更新图表。我已经为这个问题创建了一个新帖子。我应该更新这篇文章还是保持原样?这是链接:stackoverflow.com/questions/64726763/…
    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 2021-12-25
    • 2019-01-04
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多