Paul Brink
Paul Brink

Reputation: 464

Highcharts in Angular - accessing the API

I want to access the highcharts API, specifically I want to add or remove a series dynamically. Essentially I cant access any methods, or I am unsure on how.

The code I have is as follows:

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

and the HTML is like this:

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

How can I rewrite this piece of code to make it possible to access the methods such as addSeries and so on, so that I can dynamically show new data?

Ive seen theses two posts, and they look like they can be very helpful:

Highcharts add series dynamically

Adding new HighChart Series

The problem is that I cant access the methods like they do.

And when I try to implement chart = new Highcharts.Chart(options); like they do, I onyl get errors.

Thanks is advance.

Upvotes: 0

Views: 563

Answers (1)

Xtof
Xtof

Reputation: 425

It needs to be done in 2 steps. First you change the value inside the chartOption Object. Second you update the chart.

in your component you add a function like this for exemple:

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

And in your html:

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

an basic exemple here

Upvotes: 0

Related Questions