Lines in Angular

Using ApexCharts with Angular

This Lines 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';

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 MONTHS: any = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug'];

  private STORES: any = ['Online store', 'Retail'];

  private ORDERS: any = [
        [420, 460, 510, 480, 560, 610, 590, 650],
        [300, 320, 310, 340, 360, 330, 380, 400],
      ];

  private CUSTOMERS: any = {
        returning: [
          [250, 270, 300, 290, 340, 370, 360, 400],
          [210, 220, 215, 240, 250, 230, 260, 280],
        ],
        first: [
          [170, 190, 210, 190, 220, 240, 230, 250],
          [90, 100, 95, 100, 110, 100, 120, 120],
        ],
      };

  private seriesFor = (customer: any): any => {
        return this.STORES.map(function (name, i) {
          var s = { name: name, data: this.ORDERS[i] }
          if (customer) s.highlightData = this.CUSTOMERS[customer][i]
          return s
        })
      };

  private pickCustomer = (chart: any, customer: any): any => {
        document.querySelectorAll('[data-customer]').forEach(function (b) {
          b.classList.toggle('active', b.getAttribute('data-customer') === customer)
        })
        return chart.updateSeries(this.seriesFor(customer))
      };

  private wireCustomers = (chart: any): any => {
        document.querySelectorAll('[data-customer]').forEach(function (b) {
          b.addEventListener('click', function () {
            this.pickCustomer(chart, b.getAttribute('data-customer'))
          })
        })
      };

  public chartOptions: Partial<ChartOptions> = {
          series: this.seriesFor('returning'),
          chart: {
            id: 'orders',
            type: 'line',
            height: 380,
            toolbar: { show: false },
          },
          colors: ['#0f766e', '#ea580c'],
          stroke: { curve: 'smooth', width: 3 },
          markers: { size: 4, strokeWidth: 0, hover: { size: 6 } },
          dataLabels: {
            enabled: true,
            style: { fontSize: '11px' },
            background: { enabled: true, borderWidth: 0, opacity: 0.85 },
          },
          xaxis: { categories: this.MONTHS },
          yaxis: { min: 0, title: { text: 'Orders' } },
          highlightFilter: { line: { dashArray: 5 } },
          legend: { position: 'bottom' },
          tooltip: { shared: true, intersect: false },
        };
  ngAfterViewInit() {
    (window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
    this.wireCustomers(this.chart)

    function pickCustomer(chart, customer) {
          document.querySelectorAll('[data-customer]').forEach(function (b) {
            b.classList.toggle('active', b.getAttribute('data-customer') === customer)
          })
          return this.chart.updateSeries(this.seriesFor(customer))
        }
    function wireCustomers(chart) {
          document.querySelectorAll('[data-customer]').forEach(function (b) {
            b.addEventListener('click', () => {
              this.pickCustomer(chart, b.getAttribute('data-customer'))
            })
          })
        }
  }

  ngOnDestroy() {
    // no cleanup needed
  }
}