All files / components/chart-examples-line chart-examples-line.component.ts

100% Statements 17/17
100% Branches 7/7
100% Functions 13/13
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                            1x 2x                         54x                 53x                 53x         2x   15x     2x   3x 3x   2x   6x                             84x 84x               84x 84x                                
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { first, map, switchMap, timer } from 'rxjs';
 
import { ILineChartOptions, TDateFormat, TLineChartData } from '../../interfaces/line-chart.interface';
 
/** Line chart example. */
@Component({
  selector: 'app-chart-examples-line',
  templateUrl: './chart-examples-line.component.html',
  styleUrls: ['./chart-examples-line.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class AppChartExamplesLineComponent {
  private readonly breakpointObserver = inject(BreakpointObserver);
 
  /** The chart data. */
  private get chartData() {
    return [
      [
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
      ].sort((a, b) => a.timestamp - b.timestamp),
      [
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
      ].sort((a, b) => a.timestamp - b.timestamp),
      [
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
        { timestamp: this.randomTimestamp(), value: this.randomValue() },
      ].sort((a, b) => a.timestamp - b.timestamp),
    ] as TLineChartData[];
  }
 
  /** The breakpoint observer stream. */
  private readonly breakpoint$ = this.breakpointObserver
    .observe([Breakpoints.XSmall, Breakpoints.Small, Breakpoints.Medium, Breakpoints.Large, Breakpoints.XLarge])
    .pipe(map(result => Object.keys(result.breakpoints).find(item => result.breakpoints[item]) ?? 'unknown'));
 
  /** The chart configuration stream. */
  public readonly chartConfig$ = this.breakpoint$.pipe(
    switchMap(() => {
      const timeout = 100;
      return timer(timeout).pipe(
        first(),
        map(() => ({
          data: this.chartData,
          datasetLabels: Array.from(new Array(this.chartData.length)).map((item, index) => `Dataset ${index}`),
          options: this.lineChartOptions(),
          optionsDateDdMmYy: this.lineChartOptions('dd/mm/yy'),
          optionsDateDdMmYyyy: this.lineChartOptions('dd/mm/yyyy'),
          optionsDateMmYyyy: this.lineChartOptions('mm/yyyy'),
        })),
      );
    }),
  );
 
  /**
   * Random value generator.
   * @param range value range
   */
  private randomValue(range?: number) {
    const defaultRange = 100;
    return Math.floor(Math.random() * (range ?? defaultRange) + 1);
  }
 
  /**
   * Random timestamp generator.
   * @param range value range
   */
  private randomTimestamp(range?: number) {
    const defaultRange = 100000000;
    return Math.floor(Math.random() * (range ?? defaultRange) + new Date().getTime());
  }
 
  /**
   * Example line chart options.
   * @param dateFormat date format
   */
  private lineChartOptions(dateFormat: TDateFormat = 'default') {
    return {
      chartTitle: `Example line chart, date format ${dateFormat}`,
      xAxisTitle: 'Date range',
      yAxisTitle: 'Value range',
      dateFormat,
    } as Partial<ILineChartOptions>;
  }
}