Skip to content
Snippets Groups Projects
app.component.ts 1.61 KiB
Newer Older
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
import { AfterViewInit, Component, ElementRef, ViewChild } from '@angular/core';
import { nshmpHaz} from '@ghsc/nshmp-web-utils';
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',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
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();
    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({
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
      el: this.el.nativeElement,
Clayton, Brandon Scott's avatar
Clayton, Brandon Scott committed
      collection: this.disaggCollection,
      bounds: this.bounds(this.data),
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');
    return DisaggGraphView.calculateBounds(totalComponent.data);
  }

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

   this.disaggCollection.select(componentData);
  }