All files / components/chart-examples-force-directed chart-examples-force-directed.component.ts

100% Statements 31/31
84.61% Branches 11/13
100% Functions 19/19
100% Lines 25/25

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                                    1x 2x     2x                               2x 6x 16x           140x 128x     16x 2x   16x 42x 20x 20x     16x 20x     16x   160x         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 {
  IForceDirectedChartData,
  IForceDirectedChartOptions,
  IForceDirectedGraphEntity,
} from '../../interfaces/force-directed-chart.interface';
 
/** Force directed chart example. */
@Component({
  selector: 'app-chart-examples-force-directed',
  templateUrl: './chart-examples-force-directed.component.html',
  styleUrls: ['./chart-examples-force-directed.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class AppChartExamplesForceDirectedComponent {
  private readonly breakpointObserver = inject(BreakpointObserver);
 
  /** Sample chart data. */
  private readonly value = Object.freeze({
    domains: ['first', 'second', 'third'],
    entities: [
      { name: 'one', domains: ['first'], img: '' },
      { name: 'two', domains: ['second'], img: '' },
      { name: 'three', domains: ['third'], img: '' },
      { name: 'four', domains: ['first', 'second'], img: '' },
      { name: 'five', domains: ['second'], img: '' },
      { name: 'six', domains: ['third', 'second'], img: '' },
      { name: 'seven', domains: ['second'], img: '' },
      { name: 'eight', domains: ['third'], img: '' },
    ],
  });
 
  /** The chart data. */
  private get chartData() {
    const input = { ...this.value };
    const domains: IForceDirectedChartData['domains'] = input.domains.map((name, index) => ({ index, name, value: 1 }));
    const entities: IForceDirectedChartData['entities'] = input.entities.map((app, index) => ({
      index: index,
      name: app.name,
      domains: [...app.domains],
      img: app.img,
      linksCount: input.entities.reduce((accumulator, entity) => {
        const counter = entity.name === app.name ? 0 : entity.domains.filter(domain => app.domains.includes(domain)).length;
        return accumulator + counter;
      }, 0),
    }));
    const nodes: IForceDirectedGraphEntity[] = [...entities.map(item => ({ ...item, value: 1 }))];
    const links: IForceDirectedChartData['links'] = entities
      .map(entity => {
        return entity.domains.map(domain => {
          const source = domains.find(value => domain === value.name)?.index ?? -1;
          const target = entity.index;
          return { source, target };
        });
      })
      .reduce((accumulator, item) => (Array.isArray(item) ? [...accumulator, ...item] : [...accumulator, item]), [])
      .filter(link => link.source !== -1 && link.target !== -1 && typeof link.target !== 'undefined');
    const chartData: IForceDirectedChartData = {
      domains,
      entities: entities.map(item => ({
        ...item,
        linksCount: links.reduce((acc, link) => (link.target === item.index ? acc + 1 : acc), 0),
      })),
      links,
      nodes,
    };
    return chartData;
  }
 
  /** The chart options. */
  private get chartOptions() {
    return {
      chartTitle: 'Example force directed chart',
    } as Partial<IForceDirectedChartOptions>;
  }
 
  /** 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 })),
      );
    }),
  );
}