Skip to content
Snippets Groups Projects
app.component.ts 1.64 KiB
Newer Older
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
import {AfterViewInit, Component, ElementRef, ViewChild} from '@angular/core';
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
import {nshmpHaz} from '@ghsc/nshmp-utils-ts';
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
import {
  DisaggGraphView,
  Disaggregation,
  DisaggResponse,
} from '@nshmp/disagg-d3/src/disagg';
import {Collection} from '@nshmp/disagg-d3/src/mvc';
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
import disagg from '../assets/disagg-response.json';

@Component({
  selector: 'app-root',
  styleUrls: ['./app.component.scss'],
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  templateUrl: './app.component.html',
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
export class AppComponent implements AfterViewInit {
  title = 'Disaggregation';
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  disaggCollection = new Collection<Disaggregation>([]);
  data = disagg.response as nshmpHaz.disaggService.DisaggResponseData;
  view: DisaggGraphView;

  @ViewChild('example')
  el: ElementRef<HTMLElement>;

Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  constructor() {}

  ngAfterViewInit(): void {
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    const response = new DisaggResponse(this.data);
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    const disaggs = response
      .get<Collection<Disaggregation>>('disaggregations')
      .data();
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    this.disaggCollection.addAll(disaggs);
    this.disaggCollection.select(this.disaggCollection.data()[0]);
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed

Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    this.view = new DisaggGraphView({
      bounds: this.bounds(this.data),
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
      collection: this.disaggCollection,
      el: this.el.nativeElement,
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  bounds(data: nshmpHaz.disaggService.DisaggResponseData): number[][] {
    const totalComponent = data.disaggs[0].data.find(
      d => d.component === 'Total'
    );
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    return DisaggGraphView.calculateBounds(totalComponent.data);
  }

  onComponentChange(component: string): void {
    const componentData = this.disaggCollection
      .data()
      .find(d => d.get('component') === component);

Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
    this.disaggCollection.select(componentData);
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
  }