-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.component.ts
153 lines (135 loc) · 3.74 KB
/
app.component.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* Angular 2 decorators and services
*/
import {
Component,
OnInit,
ViewEncapsulation
} from '@angular/core';
import { Http } from '@angular/http';
import { AppState } from './app.service';
import * as vegaliteChart from './chart/vegalite.chart';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import * as _ from 'lodash';
interface Dataset {
name: string;
files: string[];
}
interface Chart {
name: string;
id: string;
spec: any;
selected?: boolean;
view?: any;
}
/**
* App Component
* Top Level Component
*/
@Component({
selector: 'app',
encapsulation: ViewEncapsulation.None,
styleUrls: ['./app.component.css'],
templateUrl: 'app.component.html'
})
export class AppComponent implements OnInit {
public selectedDataset: Dataset;
public datasets: Dataset[];
public sourceCharts: Chart[];
public resultCharts: Chart[];
private chartId: number = 0;
constructor(
public appState: AppState,
private http: Http
) {}
public ngOnInit() {
console.log('Initial App State', this.appState.state);
this.selectedDataset = null;
this.http.get('assets/charts/datasets.json')
.map(res => res.json())
.subscribe(res => this.datasets = res);
}
public loadDataset(event: Dataset) {
this.sourceCharts = [];
this.resultCharts = [];
event.files.forEach(d => {
this.http.get(`assets/charts/${d}`)
.map(res => res.json())
.subscribe(res => {
res.width = 180;
res.height = 180;
if (_.isUndefined(res.name)) {
res.name = _(d).split(".").dropRight().join(' ');
}
this.sourceCharts.push({
name: res.name,
id: `chart-${this.chartId}`,
spec: res,
selected: false,
});
this.chartId++;
});
});
}
public selectChart(event: any) {
let charts = _(this.sourceCharts)
.filter(d => d.selected)
.map(d => ({
spec: d.spec,
values: d.view.data('source_0'),
}))
.value();
if (charts.length < 2) {
return;
}
let results = [];
if (this.selectedDataset.name === "1D + 1D = ?") {
let data = {
values: this.sourceCharts[0].view._runtime.data.source_0.input.value,
};
let width = 480;
let height = 200;
results = [{
$schema: "https://vega.github.io/schema/vega-lite/v2.json",
width,
height,
data,
mark: "circle",
encoding: {
y: charts[0].spec.encoding.x,
x: charts[1].spec.encoding.x,
size: charts[0].spec.encoding.y
}
}, {
$schema: "https://vega.github.io/schema/vega-lite/v2.json",
width,
height,
data,
mark: "rect",
encoding: {
y: charts[0].spec.encoding.x,
x: charts[1].spec.encoding.x,
color: _.assign({}, charts[0].spec.encoding.y, {"scale": { "scheme": "greenblue"}})
}
}];
} else {
results = vegaliteChart.mergeCharts(charts);
}
this.resultCharts = _.map(results, (d, i) => ({
name: '',
id: `chart-result-${i}`,
spec: d,
}));
}
public onChartRendered(chart: Chart, view: any) {
chart.view = view;
}
}
/**
* Please review the https://github.com/AngularClass/angular2-examples/ repo for
* more angular app examples that you may copy/paste
* (The examples may not be updated as quickly. Please open an issue on github for us to update it)
* For help or questions please contact us at @AngularClass on twitter
* or our chat on Slack at https://AngularClass.com/slack-join
*/