-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ctrl.js
More file actions
466 lines (435 loc) · 16.8 KB
/
Copy pathmain.ctrl.js
File metadata and controls
466 lines (435 loc) · 16.8 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import dc from 'dc';
import d3 from 'd3';
import crossfilter from 'crossfilter';
import moment from 'moment';
var toInjects = ['$scope', '$timeout', 'dataTypesConfig', 'geoShapeSrvc', 'dataSrvc', '$timeout', '$routeParams', '$location'];
var Promise = require("bluebird");
import numeral from 'numeral';
import {
agg,
aggByKeys,
GeoDataModel,
unpivot
} from './services/dataMapper.js';
export default class MainCtrl {
constructor(...args) {
Object.assign(this, _.zipObject(toInjects, args));
let vm = this;
let $scope = this.$scope;
let $location = this.$location;
let dataSrvc = this.dataSrvc;
let $timeout = this.$timeout;
let $routeParams = this.$routeParams;
vm.geoShapes = {};
vm.selectedGeoshape = '全香港';
function _initSelection() {
vm.selectedGeoCode = '';
vm.selectedGeoshape = '全香港';
}
_initSelection();
vm.dataTypesConfig = this.dataTypesConfig;
let removeGeoClickCb;
let _geoClickSelectFactory =
(onSelected, onReSelected, onClick) => (event, geoCode, geoCodeFormatted, isSelected) => {
if (isSelected) {
vm.selectedGeoCode = geoCode;
vm.selectedGeoshape = geoCodeFormatted;
onSelected(geoCode);
_doToggleCharts(true);
} else {
onReSelected(geoCode);
_initSelection();
}
onClick(event, geoCode, geoCodeFormatted);
};
// TODO refactor away jquery
$('.ui.dropdown')
.dropdown({
action: 'hide'
});
vm.boundary = 'gc';
vm.toggleBoundary = function(boundary) {
vm.boundary = boundary;
$location.path(['/', $routeParams.dataTypeAlias, '/', boundary].join(''), false);
};
this.geoShapeSrvc.getDC()
.then(function(data) {
vm.geoShapes.districts = data;
});
this.geoShapeSrvc.getCA()
.then(function(data) {
vm.geoShapes.areas = data;
});
this.geoShapeSrvc.getGC()
.then(function(data) {
vm.geoShapes.gc = data;
// vm.geojson = vm.geoShapes.gc;
});
// chroma.scale(['yellow', '008ae5']).mode('lch');
vm.year = 2016;
vm.slider = {
value: vm.year,
options: {
floor: 2005,
ceil: 2016,
showTicks: true,
hideLimitLabels: true,
translate: v => {
return '';
}
// use tick color more slick
// getLegend: v => {
// if (_.includes([2008, 2012, 2016], v)) {
// return '立';
// } else if (_.includes([2014], v)) {
// return '區';
// }
// }
}
};
// force refresh as wrong value onload
this.$timeout(function() {
$scope.$broadcast('rzSliderForceRender');
});
vm.drawCharts = function() {
dc.renderAll();
};
vm.toggleCharts = function() {
let chartOpenSearch = vm.isChartOpen ? null : 1;
// gave up keep in sync first as forced fresh for unknown reason
// $location.search('charts', chartOpenSearch);
_doToggleCharts();
};
function _doToggleCharts(keepOpen) {
console.log('toggleCharts');
if (keepOpen && vm.isChartOpen) {
return;
}
vm.isChartOpen = !vm.isChartOpen;
$('.drawer.sidebar')
.sidebar({
'transition': 'push',
context: $('.widget-container .pushable')
})
.sidebar('setting', 'closable', false)
.sidebar('setting', 'dimPage', false)
.sidebar('toggle');
vm.drawCharts();
// TODO CSS?
// TODO need lay after toggle
$timeout(function() {
let targetZoom = vm.isChartOpen ? 10 : 11;
$scope.$broadcast('redrawMap', true, targetZoom);
});
}
if (this.$routeParams.charts) {
// TODO delay post DOM ready
$timeout(function() {
_doToggleCharts();
});
}
vm.selectDataType = function(dataType) {
_updateDataType(dataType);
$location.path(['/', dataType.alias, '/', vm.boundary].join(''), false);
};
function _updateDataType(dataType) {
vm.selectedDataType = dataType;
// TODO fix this temp hack
if (vm.selectedDataType.key === 'vt_by_gc_ps_hour') {
vm.isByE = true;
} else {
vm.isByE = false;
}
if (vm.isChartOpen) {
_doToggleCharts();
}
// TODO multiple chart
vm.chartTitle = vm.selectedDataType.chartTitle;
_loadDataAndDraw(vm.selectedDataType.key);
}
let dataType = _.find(this.dataTypesConfig, {
alias: $routeParams.dataTypeAlias
});
if (dataType) {
_updateDataType(dataType);
} else {
this.$location.path('/population/gc/');
}
// TODO service
var ndx, all, chart;
var _createChart = function(createChartStrategy, onFilter) {
const elemSelector = "#chart-container";
removeGeoClickCb ? removeGeoClickCb() : null;
d3.select(elemSelector + " svg").remove();
return createChartStrategy(elemSelector)
.turnOnControls(true)
// .width('100%')
.on("postRender", function(chart) {
chart.select("svg")
.attr("preserveAspectRatio", "xMinYMax meet");
chart.redraw();
})
.height(300)
.margins({
left: 50,
top: 20,
right: 10,
bottom: 20
})
.on('filtered', function(chart, filterSelected) {
onFilter();
$scope.$digest();
});
};
function _loadDataAndDraw(dataType) {
// Visualize by Type
vm.dataPromise = dataSrvc.getData(dataType);
vm.dataPromise.then(data => {
vm.data = data;
console.log('draw');
const CATEGORIES = ['M', 'F'];
// diff x-filter data
// pivot
const ndx = crossfilter(data);
const all = ndx.groupAll();
const yearDimension = ndx.dimension(d => d.year);
yearDimension.filter(vm.year);
var onFilter;
var createChartStrategy;
var timeDimension;
var votersbyTimeGroup;
// clear
if (dataType === 'vt_by_gc_ps_hour') {
timeDimension = ndx.dimension(d => d.time);
let gcDimension = ndx.dimension(d => d.gc.toLowerCase());
let dcDimension = ndx.dimension(d => d.dc.toLowerCase());
votersbyTimeGroup = timeDimension.group().reduceSum(d => d.value);
createChartStrategy = function(elemSelector) {
console.log('line chart');
timeDimension.filterAll();
removeGeoClickCb = $scope.$on('feature.clicked', _geoClickSelectFactory(
geoCode => {
if (vm.boundary === 'gc') {
gcDimension.filter(geoCode);
} else if (vm.boundary === 'dc') {
dcDimension.filter(geoCode);
}
},
geoCode => {
gcDimension.filterAll();
dcDimension.filterAll();
}, () => {
// hacky flag to prevent choropleth update now
onFilter(null, true);
$scope.$digest();
// vm.geoData
}
));
// TODO stack electors
return dc.lineChart(elemSelector)
.renderArea(true)
.transitionDuration(1000)
.margins({top: 30, right: 50, bottom: 25, left: 40})
.x(d3.scale.ordinal())
.elasticY(true)
.elasticX(true)
// .x(d3.time.scale().domain([moment("1234", "hmm").toDate()]))
.xUnits(dc.units.ordinal)
.dimension(timeDimension)
.group(votersbyTimeGroup)
.renderVerticalGridLines(true)
.renderHorizontalGridLines(true)
.valueAccessor(function(d) {
return d.value;
})
.mouseZoomable(true);
};
// or custom handling for dcDimension to get directly, since gc also mapped
// TODO agg by .dc, using d3
// or reuse dimensiongroup
onFilter = function(filter, keepGeo) {
if (!keepGeo) {
let byDcEntries =
d3.nest().key(d => d.dc.toLowerCase())
.rollup(leaves => {
let voters = _.sumBy(leaves, l => l.value);
let electors = _.sumBy(leaves, l => l['electors']);
return {voters, electors};
})
.entries(timeDimension.filter('2230').top(Infinity));
timeDimension.filterAll();
// TODO one off filter?
let byDc = _.merge({}, ...byDcEntries.map(o => _.fromPairs([[o.key, o.values]])));
vm.geoData = new GeoDataModel(byDc, 'dc', function reducer(v1, v2) {
return {
voters: _.sum([(v1 || {}).voters, (v2 || {}).voters]),
electors: _.sum([(v1 || {}).electors, (v2 || {}).electors])
};
}, function accessor(v) {
return numeral(v.voters).divide(v.electors).value();
});
}
if (filter) {
chart.filter(filter);
}
if (chart.svg()) {
chart.redraw();
}
$location.search('dc', chart.filters()[0]);
};
vm.valueFormatter = function(v) {
return numeral(v).format('0.00%');
};
} else {
const ageDimension = ndx.dimension(d => d.age_group);
let valueAccessor = v => v.total;
let agePopulation = (ageGroup, population) => {
let [min, max] = ageGroup.split('-').map(v => numeral(v).value());
if (_.isUndefined(max) || max === 0) {
// 71 or above is roughly estimated from "70 or above" per 2015 year end data here,
// where 85+ is estimated as 87
// http://www.censtatd.gov.hk/hkstat/sub/sp20_tc.jsp?productCode=B1010002
// TODO request per-year data
max = 77;
}
return ((max + min) / 2) * population;
};
let calculateAgeAvg = function(valueAccessor) {
let total = ageDimension.groupAll().reduceSum(v => {
return agePopulation(v.age_group, valueAccessor(v));
}).value();
return numeral(total / ageDimension.groupAll().reduceSum(valueAccessor).value()).format('0');
};
let filteredCountChart = dc.numberDisplay('#selected-total');
let filteredPerentChart = dc.numberDisplay('#selected-percent');
let setupFilteredNumbers = ageDimensionGroup => {
filteredCountChart.group(all.reduceSum(valueAccessor))
.valueAccessor(d => d)
.formatNumber(d => numeral(d).format('0,0'));
let total = _.sumBy(ageDimensionGroup.top(Infinity), function(d) {
return _.sum(_.values(d.value));
});
// need both ageDimension, year filters
filteredPerentChart.group(all.reduceSum(valueAccessor))
.valueAccessor(d => {
// precision issue
return Math.min((d / total), 1.0);
})
.formatNumber(d3.format('0.0%'));
};
// TODO simplify reduceSumByKey
// grouped total Population by age group
let _createAgeDimensionGroup = (ageDimension, accessor) => {
if (!accessor) {
accessor = v => v.total;
}
return ageDimension.group()
.reduce((p, v) => {
p[v.category] = (p[v.category] || 0) + accessor(v);
return p;
}, (p, v) => {
p[v.category] = (p[v.category] || 0) - accessor(v);
return p;
}, () => {
return {};
});
};
let ageDimensionGroup = _createAgeDimensionGroup(ageDimension);
vm.valueFormatter = v => numeral(v).format('0,0') + ' 人';
onFilter = function(filters) {
var data = aggByKeys(ageDimension.top(ndx.size()));
vm.geoData = new GeoDataModel(data, 'dc');
// $scope.$digest();
vm.ageAvg = calculateAgeAvg(valueAccessor);
if (filters) {
chart.filter([filters]);
}
setupFilteredNumbers(ageDimensionGroup);
if (chart.svg()) {
chart.redraw();
filteredCountChart.redraw();
filteredPerentChart.redraw();
}
$location.search('ageGroup', chart.filters().join(','));
};
const getGroupValueByKey = category => d => d.value[category];
createChartStrategy = function(elemSelector) {
// TODO responsive chart
// TODO extract
$scope.$watch('$routeChangeSuccess', function() {
let ageGroupFilter = $routeParams.ageGroup;
if (ageGroupFilter) {
ageGroupFilter = ageGroupFilter.split(',');
}
// TODO validations
ageDimension.filterFunction(ageGroup => _.includes(ageGroupFilter, ageGroup));
if (ageDimension.top(Infinity).length === 0) {
ageDimension.filterAll();
} else {
onFilter(ageGroupFilter);
}
});
removeGeoClickCb = $scope.$on('feature.clicked', _geoClickSelectFactory(
geoCode => {
valueAccessor = v => {
// inefficient but work
let model = new GeoDataModel(v, 'dc');
return model.groupByBoundary(vm.boundary)[geoCode];
};
},
() => {
// reset valueAccessor
valueAccessor = v => v.total;
},
geoCode => {
// hack to manually select the value as data isn't proper in geo dimension
ageDimensionGroup = _createAgeDimensionGroup(ageDimension, valueAccessor);
chart.group(ageDimensionGroup, CATEGORIES[0], getGroupValueByKey(CATEGORIES[0]))
.stack(ageDimensionGroup, CATEGORIES[1], getGroupValueByKey(CATEGORIES[1]));
setupFilteredNumbers(ageDimensionGroup);
console.log('all ages');
// let total = ageDimension.groupAll().reduceSum(v => {
// return agePopulation(v.age_group, valueAccessor(v));
// }).value();
// vm.ageAvg = numeral(total / ageDimension.groupAll().reduceSum(valueAccessor).value()).format('0');
onFilter(null);
$scope.$digest();
}
));
return dc.barChart(elemSelector)
.elasticY(true)
.elasticX(true)
.x(d3.scale.ordinal())
.xUnits(dc.units.ordinal)
.legend(dc.legend().x(50).y(10).itemHeight(20).gap(5))
.dimension(ageDimension)
.group(ageDimensionGroup, CATEGORIES[0], getGroupValueByKey(CATEGORIES[0]))
.stack(ageDimensionGroup, CATEGORIES[1], getGroupValueByKey(CATEGORIES[1]))
.brushOn(false)
.clipPadding(20)
.renderHorizontalGridLines(true)
// .ordinalColors(['#0A2463', '#FFFFFF', '#D81C1C', '#3E92CC', '#1E1B18'])
.ordinalColors(['#99c0db', '#fb8072'])
.title(d => numeral(_.sum(_.values(d.value))).format('0,0'));
// .ordinalColors(['#9AC5E2', '#F7B8A1'])
// .renderLabel(true);
};
}
// manual hook as out of chart
$scope.$watch('vm.year', (newVal, oldVal) => {
yearDimension.filter(newVal);
onFilter();
if (vm.geoData.isEmpty() && vm.isChartOpen) {
vm.toggleCharts();
}
});
chart = _createChart(createChartStrategy, onFilter);
onFilter();
}).catch(err => console.error(err));
vm.drawCharts = function() {
dc.renderAll();
};
}
}
}
MainCtrl.$inject = toInjects;