-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.ts
76 lines (63 loc) · 1.43 KB
/
app.ts
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
/// <reference path="typings/tsd.d.ts" />
import * as angular from 'angular2/angular2';
import {Component, Directive, View, Attribute, onChange, ElementRef} from 'angular2/angular2';
import {Inject} from 'angular2/di';
import * as d3 from 'd3';
@Directive({
selector: 'bar-graph',
lifecycle: [ onChange ],
properties: [ 'data' ]
})
class BarGraph {
data: Array<number>;
divs: any;
constructor(
@Inject(ElementRef) elementRef: ElementRef,
@Attribute('width') width: string,
@Attribute('height') height: string) {
var el:any = elementRef.domElement;
var graph:any = d3.select(el);
this.divs = graph.
append('div').
attr({
'class': 'chart'
}).
style({
'width': width + 'px',
'height': height + 'px',
}).
selectAll('div');
}
render(newValue) {
if (!newValue) return;
this.divs.data(newValue).enter().append('div')
.transition().ease('elastic')
.style('width', d => d + '%')
.text(d => d + '%');
}
onChange() {
this.render(this.data);
}
}
@Component({
selector: 'app'
})
@View({
directives: [ BarGraph ],
template: `
<h1 class="title">Angular 2 + d3</h1>
<bar-graph
bind-data="graphData"
width="500"
height="130"
>
</bar-graph>
`
})
class App {
graphData: Array<number>;
constructor() {
this.graphData = [10,20,30,40,60];
}
}
angular.bootstrap(App, [/* AppInjector */]);