comparison src/app/visualisations/regions/regions.component.ts @ 480:0fbba61603b3

Begin to add regions component
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 20 Jul 2017 16:15:40 +0100
parents
children 7253d73ac7c9
comparison
equal deleted inserted replaced
479:a14ea0f9bbd1 480:0fbba61603b3
1 /**
2 * Created by lucast on 31/05/2017.
3 */
4 import {
5 InspectableVerticallyBoundedComponent,
6 VerticallyBounded,
7 VerticalScaleRenderer,
8 VerticalValueInspectorRenderer,
9 WavesComponent
10 } from '../waves-base.component';
11 import {
12 ChangeDetectionStrategy,
13 Component,
14 Input,
15 } from '@angular/core';
16 import {Region} from '../FeatureUtilities';
17 import Waves from 'waves-ui-piper';
18
19 @Component({
20 selector: 'ugly-regions',
21 templateUrl: '../waves-template.html',
22 styleUrls: ['../waves-template.css'],
23 changeDetection: ChangeDetectionStrategy.OnPush,
24 providers: [
25 { provide: VerticallyBounded, useExisting: RegionsComponent },
26 { provide: VerticalScaleRenderer, useExisting: RegionsComponent },
27 {provide: VerticalValueInspectorRenderer, useExisting: RegionsComponent },
28 {provide: WavesComponent, useExisting: RegionsComponent}
29 ]
30 })
31 export class RegionsComponent extends InspectableVerticallyBoundedComponent<Region[]> {
32 private currentVerticalRange: [number, number];
33
34 get range(): [number, number] {
35 return this.currentVerticalRange;
36 }
37
38 @Input() set regions(regions: Region[]) {
39 this.feature = regions;
40 }
41
42 protected get featureLayers(): Layer[] {
43 this.currentVerticalRange = findVerticalRange(this.feature);
44 return [
45 new Waves.helpers.PianoRollLayer(
46 this.feature,
47 {
48 height: this.height,
49 color: this.colour,
50 yDomain: this.currentVerticalRange
51 }
52 )
53 ];
54 }
55 }
56
57 // TODO there might be scope to create a generic utility function like this
58 function findVerticalRange(regions: Region[]): [number, number] {
59 let [min, max] = regions.reduce((acc, region) => {
60 const [min, max] = acc;
61 return [Math.min (min, region.value), Math.max (max, region.value)];
62 }, [Infinity, -Infinity]);
63 if (min === Infinity) {
64 min = 0;
65 max = 1;
66 }
67 return [ min, max ];
68 }