Areas in Angular

Using ApexCharts with Angular

This Areas 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
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 DAYS: any = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];

  private SECTIONS: any = ['Docs', 'Blog'];

  private VISITS: any = [
        [62, 70, 74, 68, 60, 34, 30],
        [28, 34, 40, 37, 42, 48, 44],
      ];

  private SOURCES: any = {
        search: [
          [40, 44, 47, 42, 37, 20, 18],
          [12, 15, 18, 16, 18, 20, 19],
        ],
        social: [
          [6, 8, 9, 8, 7, 6, 5],
          [10, 12, 14, 14, 16, 21, 19],
        ],
      };

  private seriesFor = (source: any): any => {
        return this.SECTIONS.map(function (name, i) {
          var s = { name: name, data: this.VISITS[i] }
          if (source) s.highlightData = this.SOURCES[source][i]
          return s
        })
      };

  private pickSource = (source: any): any => {
        document.querySelectorAll('[data-source]').forEach(function (b) {
          b.classList.toggle('active', b.getAttribute('data-source') === source)
        })
        return Promise.all(
          ['visits', 'stacked-visits'].map(function (id) {
            return ApexCharts.getChartByID(id).updateSeries(this.seriesFor(source))
          }),
        )
      };

  private wireSources = (): any => {
        document.querySelectorAll('[data-source]').forEach(function (b) {
          b.addEventListener('click', function () {
            this.pickSource(b.getAttribute('data-source'))
          })
        })
      };

  public chartOptions: Partial<ChartOptions> = {
          series: this.seriesFor('search'),
          chart: {
            id: 'visits',
            type: 'area',
            height: 320,
            toolbar: { show: false },
          },
          colors: ['#0284c7', '#16a34a'],
          stroke: { curve: 'smooth', width: 2 },
          dataLabels: { enabled: false },
          xaxis: { categories: this.DAYS },
          yaxis: { title: { text: 'Visits (thousands)' } },
          legend: { position: 'bottom' },
          tooltip: { shared: true, intersect: false },
        };

  public chartOptions2: Partial<ChartOptions> = {
          series: this.seriesFor('search'),
          chart: {
            id: 'stacked-visits',
            type: 'area',
            height: 320,
            stacked: true,
            toolbar: { show: false },
          },
          colors: ['#0284c7', '#16a34a'],
          stroke: { curve: 'straight', width: 2 },
          fill: { type: 'solid', opacity: 0.85 },
          dataLabels: { enabled: false },
          xaxis: { categories: this.DAYS },
          yaxis: { title: { text: 'Visits (thousands)' } },
          legend: { position: 'bottom' },
          tooltip: { shared: true, intersect: false },
        };
  ngAfterViewInit() {
    (window as any).ApexCharts.setLicense('APEX-eyJleHBpcnlEYXRlIjoiMjEyNi0wNy0wNCIsImlzc3VlRGF0ZSI6IjIwMjYtMDctMjgiLCJwbGFuIjoicHJlbWl1bSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiLCJsb2NhbGhvc3QiXSwic2lnIjoieVBmb1VCc0Z3TU9ZdUEyaEZkR0I2Y1FtZ0JITUtXcVdJSjB2NVRESXRZbFR3eDJMUmh6R2x0RUc3VXJ4X0s3b25ZMWRZb2Z2VGItN01ydFYyNDVyOWcifQ==');
    this.wireSources()

    function pickSource(source) {
          document.querySelectorAll('[data-source]').forEach(function (b) {
            b.classList.toggle('active', b.getAttribute('data-source') === source)
          })
          return Promise.all(
            ['visits', 'stacked-visits'].map(function (id) {
              return ApexCharts.getChartByID(id).updateSeries(this.seriesFor(source))
            }),
          )
        }
    function wireSources() {
          document.querySelectorAll('[data-source]').forEach(function (b) {
            b.addEventListener('click', () => {
              this.pickSource(b.getAttribute('data-source'))
            })
          })
        }
  }

  ngOnDestroy() {
    // no cleanup needed
  }
}