Parliament in Angular

Using ApexCharts with Angular

This Parliament example wires ApexCharts into Angular through the ng-apexcharts component.

Install it with npm install ng-apexcharts apexcharts, then mount the chart with <apx-chart [series]="series" [chart]="chart"></apx-chart>.

Angular charts guide

This demo uses imperative chart updates. The generated code is a faithful Angular translation: open it in CodeSandbox to run and tweak.

import { Component, AfterViewInit, OnDestroy, ViewChild } from '@angular/core';
import {
  ChartComponent,
  ApexAxisChartSeries,
  ApexNonAxisChartSeries,
  ApexChart,
  ApexXAxis,
  ApexYAxis,
  ApexTitleSubtitle,
  ApexDataLabels,
  ApexStroke,
  ApexFill,
  ApexLegend,
  ApexTooltip,
  ApexMarkers,
  ApexPlotOptions,
  ApexResponsive,
  ApexGrid,
  ApexAnnotations,
  ApexStates,
  ApexTheme,
  NgApexchartsModule,
} from 'ng-apexcharts';

import 'apexcharts/unit';

export type ChartOptions = {
  series?: ApexAxisChartSeries | ApexNonAxisChartSeries;
  chart?: ApexChart;
  xaxis?: ApexXAxis;
  yaxis?: ApexYAxis | ApexYAxis[];
  title?: ApexTitleSubtitle;
  subtitle?: ApexTitleSubtitle;
  dataLabels?: ApexDataLabels;
  stroke?: ApexStroke;
  fill?: ApexFill;
  legend?: ApexLegend;
  tooltip?: ApexTooltip;
  markers?: ApexMarkers;
  plotOptions?: ApexPlotOptions;
  responsive?: ApexResponsive[];
  grid?: ApexGrid;
  annotations?: ApexAnnotations;
  states?: ApexStates;
  theme?: ApexTheme;
  colors?: string[];
  labels?: any;
};

@Component({
  selector: 'app-chart',
  standalone: true,
  imports: [NgApexchartsModule],
  templateUrl: './chart.component.html',
})
export class AppChart implements AfterViewInit, OnDestroy {
  @ViewChild('chart') chart!: ChartComponent;
  private PARTIES: any = [
        'Progressives',
        'Conservatives',
        'Greens',
        'Liberals',
        'Centre',
        'Independents',
      ];

  private MAJORITY: any = 158;

  private RESULTS: any = {
        2019: [82, 78, 45, 40, 38, 32], // fragmented: no single majority
        2023: [60, 162, 30, 28, 20, 15], // the Conservatives sweep to a majority
      };

  private readoutFor = (year: any): any => {
        var seats = this.RESULTS[year]
        var topIdx = 0
        for (var i = 1; i < seats.length; i++) {
          if (seats[i] > seats[topIdx]) topIdx = i
        }
        var top = seats[topIdx]
        var verdict =
          top >= this.MAJORITY
            ? '<b>' +
              this.PARTIES[topIdx] +
              '</b> hold an outright majority (' +
              top +
              ' of 315).'
            : 'No majority: <b>' +
              this.PARTIES[topIdx] +
              '</b> lead with ' +
              top +
              ' of 315, short of ' +
              this.MAJORITY +
              '.'
        return year + ': ' + verdict
      };

  private readout: any = document.querySelector('#readout');

  private btn2019: any = document.querySelector('#btn2019');

  private btn2023: any = document.querySelector('#btn2023');

  private show = (year: any, btn: any): any => {
          this.btn2019.classList.toggle('active', btn === this.btn2019)
          this.btn2023.classList.toggle('active', btn === this.btn2023)
          this.readout.innerHTML = this.readoutFor(year)
          this.chart.updateSeries(this.RESULTS[year])
        };

  public chartOptions: Partial<ChartOptions> = {
          series: [82, 78, 45, 40, 38, 32],
          chart: {
            id: 'parliament',
            type: 'unit',
            height: 440,
            animations: {
              enabled: true,
              speed: 800,
            },
          },
          labels: [
            'Progressives',
            'Conservatives',
            'Greens',
            'Liberals',
            'Centre',
            'Independents',
          ],
          colors: ['#E3000F', '#1E6FD9', '#3DA35D', '#F0A202', '#17A2B8', '#6C757D'],
          legend: {
            position: 'bottom',
          },
          plotOptions: {
            unit: {
              layout: 'arc',
              arc: {
                innerRadiusRatio: 0.35,
              },
              tooltip: {
                // Party name + its seat total for the hovered seat.
                formatter: (o) => {
                  return '<b>' + o.seriesName + '</b><br/>' + o.count + ' seats'
                },
              },
            },
          },
        };
  ngAfterViewInit() {
    (window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');

    function show(year, btn) {
            this.btn2019.classList.toggle('active', btn === this.btn2019)
            this.btn2023.classList.toggle('active', btn === this.btn2023)
            this.readout.innerHTML = this.readoutFor(year)
            this.chart.updateSeries(this.RESULTS[year])
          }
    this.btn2019.addEventListener('click', () => {
            this.show(2019, this.btn2019)
          })
    this.btn2023.addEventListener('click', () => {
            this.show(2023, this.btn2023)
          })
  }

  ngOnDestroy() {
    // no cleanup needed
  }
}