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 mulberry32 = (seed: any): any => {
          return function () {
            seed |= 0
            seed = (seed + 0x6d2b79f5) | 0
            var t = Math.imul(seed ^ (seed >>> 15), 1 | seed)
            t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
            return ((t ^ (t >>> 14)) >>> 0) / 4294967296
          }
        };

  private dailySeries = (seed: any, count: any, base: any, spread: any): any => {
          var rand = this.mulberry32(seed)
          var series = []
          var ts = new Date('01 Jun 2025').getTime()
          var val = base
          for (var i = 0; i < count; i++) {
            val += (rand() - 0.5) * spread
            series.push([ts, Math.round(val)])
            ts += 86400000
          }
          return series
        };

  public chartOptions: Partial<ChartOptions> = {
          series: [
            {
              name: 'Page Views',
              data: this.dailySeries(11, 20, 42, 18),
            },
          ],
          chart: {
            id: 'pageviews',
            group: 'website',
            type: 'line',
            height: 130,
          },
          title: {
            text: 'Page Views (k)',
          },
          colors: ['#008FFB'],
        };

  public chartOptions2: Partial<ChartOptions> = {
          series: [
            {
              name: 'Sessions',
              data: this.dailySeries(23, 20, 20, 8),
            },
          ],
          chart: {
            id: 'sessions',
            group: 'website',
            type: 'line',
            height: 130,
          },
          title: {
            text: 'Sessions (k)',
          },
          colors: ['#546E7A'],
        };

  public chartOptions3: Partial<ChartOptions> = {
          series: [
            {
              name: 'Sign-ups',
              data: this.dailySeries(35, 20, 38, 16),
            },
          ],
          chart: {
            id: 'signups',
            group: 'website',
            type: 'area',
            height: 130,
          },
          title: {
            text: 'Sign-ups',
          },
          colors: ['#00E396'],
        };
  ngAfterViewInit() {
    (window as any).ApexCharts.setLicense('APEX-eyJpc3N1ZURhdGUiOiIyMDI2LTA0LTE2IiwiZXhwaXJ5RGF0ZSI6IjIwNTItMDQtMTciLCJwbGFuIjoiZW50ZXJwcmlzZSIsImRvbWFpbnMiOlsiYXBleGNoYXJ0cy5jb20iLCIxMjcuMC4wLjEiXX0=');
    (window as any).Apex = {
            chart: {
              height: 130,
            },
            title: {
              style: {
                fontSize: '14px',
              },
            },
            dataLabels: {
              enabled: false,
            },
            stroke: {
              curve: 'straight',
            },
            toolbar: {
              tools: {
                selection: false,
              },
            },
            markers: {
              size: 6,
              hover: {
                size: 10,
              },
            },
            tooltip: {
              followCursor: false,
              theme: 'dark',
              x: {
                show: false,
              },
              marker: {
                show: false,
              },
              y: {
                title: {
                  formatter: () => {
                    return ''
                  },
                },
              },
            },
            grid: {
              clipMarkers: false,
            },
            yaxis: {
              tickAmount: 2,
            },
            xaxis: {
              type: 'datetime',
            },
          }
  }

  ngOnDestroy() {
    // no cleanup needed
  }
}
Syncing Charts - Angular Line Charts | ApexCharts.js | ApexCharts.js