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

100% Statements 10/10
100% Branches 2/2
100% Functions 7/7
100% Lines 9/9

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                            1x 2x                                                       2x       2x   15x     2x   3x 3x   2x          
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { first, map, switchMap, timer } from 'rxjs';
 
import { IPieChartDataNode, IPieChartOptions } from '../../interfaces/pie-chart.interface';
 
/** Pie chart example. */
@Component({
  selector: 'app-chart-examples-pie',
  templateUrl: './chart-examples-pie.component.html',
  styleUrls: ['./chart-examples-pie.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class AppChartExamplesPieComponent {
  private readonly breakpointObserver = inject(BreakpointObserver);
 
  /** The chart data. */
  private get chartData() {
    return [
      { key: 'one', y: 1 },
      { key: 'two', y: 2 },
      { key: 'three', y: 3 },
      { key: 'four', y: 4 },
      { key: 'five', y: 5 },
      { key: 'six', y: 6 },
    ] as IPieChartDataNode[];
  }
 
  /** The chart options. */
  private get chartOptions() {
    const options: {
      first: Partial<IPieChartOptions>;
      second: Partial<IPieChartOptions>;
    } = {
      first: {
        chartTitle: 'Example pie chart 1',
      } as Partial<IPieChartOptions>,
      second: {
        chartTitle: 'Example pie chart 2',
        innerRadius: 75,
      } as Partial<IPieChartOptions>,
    };
    return options;
  }
 
  /** 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, options: this.chartOptions })),
      );
    }),
  );
}