/
ko.mapChart.js
349 lines (322 loc) · 11.3 KB
/
ko.mapChart.js
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
// Licensed to Cloudera, Inc. under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. Cloudera, Inc. licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import $ from 'jquery';
import * as ko from 'knockout';
import nv from 'ext/nv.d3.1.1.15b.custom';
import Datamap from './datamap';
import huePubSub from 'utils/huePubSub';
import HueColors from 'utils/hueColors';
import HueGeo from 'utils/hueGeo';
ko.bindingHandlers.mapChart = {
render: function(element, valueAccessor) {
const _options = valueAccessor();
const $element = $(element);
window.clearTimeout($element.data('drawThrottle'));
const timeout = window.setTimeout(() => {
$element.empty();
$element.css('position', 'relative');
$element.css('marginLeft', 'auto');
$element.css('marginRight', 'auto');
if (typeof _options.maxWidth != 'undefined') {
const _max = _options.maxWidth * 1;
$element.width(Math.min($element.parent().width(), _max));
} else {
$element.width($element.parent().width() - 10);
}
$element.height($element.width() / 2.23);
const scopeObservable = _options.data.scope || _options.data.chartScope;
const _scope = scopeObservable ? String(ko.unwrap(scopeObservable)) : 'world';
const _data = _options.transformer(_options.data);
let _is2d = false;
const _pivotCategories = [];
let _maxWeight = 0;
function comparePivotValues(a, b) {
if (a.count < b.count) {
return 1;
}
if (a.count > b.count) {
return -1;
}
return 0;
}
$(_data).each((cnt, item) => {
if (item.value > _maxWeight) {
_maxWeight = item.value;
}
if (item.obj.is2d) {
_is2d = true;
}
if (item.obj.pivot && item.obj.pivot.length > 0) {
item.obj.pivot.forEach(piv => {
let _category = null;
_pivotCategories.forEach(category => {
if (category.value === piv.value) {
_category = category;
if (piv.count > _category.count) {
_category.count = piv.count;
}
}
});
if (_category == null) {
_category = {
value: piv.value,
count: -1
};
_pivotCategories.push(_category);
}
});
}
});
_pivotCategories.sort(comparePivotValues);
const _chunk = _maxWeight / _data.length;
const _mapdata = {};
const _maphovers = {};
const _fills = {};
const _legend = [];
const _noncountries = [];
if (_options.isScale) {
_fills['defaultFill'] = HueColors.WHITE;
const _colors = _is2d
? HueColors.d3Scale()
: HueColors.scale(HueColors.LIGHT_BLUE, HueColors.DARK_BLUE, _data.length);
$(_colors).each((cnt, item) => {
_fills['fill_' + cnt] = item;
});
const getHighestCategoryValue = (cnt, item) => {
let _cat = '';
let _max = -1;
if (item.obj.pivot && item.obj.pivot.length > 0) {
item.obj.pivot.forEach(piv => {
if (piv.count > _max) {
_max = piv.count;
_cat = piv.value;
}
});
}
let _found = cnt;
if (_cat !== '') {
_pivotCategories.forEach((cat, i) => {
if (cat.value === _cat) {
_found = i;
}
});
}
return {
idx: _found,
cat: _cat
};
};
const addToLegend = category => {
let _found = false;
_legend.forEach(lg => {
if (lg.cat === category.cat) {
_found = true;
}
});
if (!_found) {
_legend.push(category);
}
};
$(_data).each((cnt, item) => {
addToLegend(getHighestCategoryValue(cnt, item));
let _place = typeof item.label == 'string' ? item.label.toUpperCase() : item.label;
if (_place != null) {
if (
_scope !== 'world' &&
_scope !== 'usa' &&
_scope !== 'europe' &&
_place.indexOf('.') === -1
) {
_place = HueGeo.getISOAlpha2(_scope) + '.' + _place;
}
if ((_scope === 'world' || _scope === 'europe') && _place.length === 2) {
_place = HueGeo.getISOAlpha3(_place);
}
_mapdata[_place] = {
fillKey:
'fill_' +
(_is2d
? getHighestCategoryValue(cnt, item).idx
: Math.ceil(item.value / _chunk) - 1),
id: _place,
cat: item.obj.cat,
value: item.obj.values ? item.obj.values : item.obj.value,
pivot: _is2d ? item.obj.pivot : [],
selected: item.obj.selected,
fields: item.obj.fields ? item.obj.fields : null
};
_maphovers[_place] = item.value;
} else {
_noncountries.push(item);
}
});
} else {
_fills['defaultFill'] = HueColors.LIGHT_BLUE;
_fills['selected'] = HueColors.DARK_BLUE;
$(_data).each((cnt, item) => {
let _place = item.label.toUpperCase();
if (_place != null) {
if (
_scope !== 'world' &&
_scope !== 'usa' &&
_scope !== 'europe' &&
_place.indexOf('.') === -1
) {
_place = HueGeo.getISOAlpha2(_scope) + '.' + _place;
}
if ((_scope === 'world' || _scope === 'europe') && _place.length === 2) {
_place = HueGeo.getISOAlpha3(_place);
}
_mapdata[_place] = {
fillKey: 'selected',
id: _place,
cat: item.obj.cat,
value: item.obj.values ? item.obj.values : item.obj.value,
pivot: [],
selected: item.obj.selected,
fields: item.obj.fields ? item.obj.fields : null
};
_maphovers[_place] = item.value;
} else {
_noncountries.push(item);
}
});
}
let _map = null;
function createDatamap(element, options, fills, mapData, nonCountries, mapHovers) {
_map = new Datamap({
element: element,
fills: fills,
scope: _scope,
data: mapData,
legendData: _legend,
onClick: function(data) {
if (typeof options.onClick != 'undefined') {
huePubSub.publish('charts.state', { updating: true });
options.onClick(data);
}
},
done: function() {
const _bubbles = [];
if (options.enableGeocoding) {
$(nonCountries).each((cnt, item) => {
HueGeo.getCityCoordinates(item.label, (lat, lng) => {
_bubbles.push({
fillKey: 'selected',
label: item.label,
value: item.value,
radius: 4,
latitude: lat,
longitude: lng
});
_map.bubbles(_bubbles, {
popupTemplate: function(geo, data) {
return (
'<div class="hoverinfo" style="text-align: center"><strong>' +
data.label +
'</strong><br/>' +
item.value +
'</div>'
);
}
});
});
});
}
},
geographyConfig: {
hideAntarctica: true,
borderWidth: 1,
borderColor: HueColors.DARK_BLUE,
highlightOnHover: true,
highlightFillColor: HueColors.DARK_BLUE,
highlightBorderColor: HueColors.BLUE,
selectedFillColor: HueColors.DARKER_BLUE,
selectedBorderColor: HueColors.DARKER_BLUE,
popupTemplate: function(geography, data) {
let _hover = '';
if (data != null) {
_hover = '<br/>';
if (data.pivot && data.pivot.length > 0) {
data.pivot.sort(comparePivotValues);
data.pivot.forEach((piv, cnt) => {
_hover +=
(cnt === 0 ? '<strong>' : '') +
piv.value +
': ' +
piv.count +
(cnt === 0 ? '</strong>' : '') +
'<br/>';
});
} else {
_hover += mapHovers[data.id];
}
}
return (
'<div class="hoverinfo" style="text-align: center"><strong>' +
geography.properties.name +
'</strong>' +
_hover +
'</div>'
);
}
}
});
if (options.onComplete != null) {
options.onComplete();
}
if (_is2d) {
_map.legend();
}
}
createDatamap(element, _options, _fills, _mapdata, _noncountries, _maphovers);
const _parentSelector =
typeof _options.parentSelector != 'undefined' ? _options.parentSelector : '.card-widget';
$element.parents(_parentSelector).one('resize', () => {
ko.bindingHandlers.mapChart.render(element, valueAccessor);
});
let _resizeTimeout = -1;
nv.utils.windowResize(() => {
window.clearTimeout(_resizeTimeout);
_resizeTimeout = window.setTimeout(() => {
ko.bindingHandlers.mapChart.render(element, valueAccessor);
}, 200);
});
huePubSub.publish('charts.state');
}, 50);
$element.data('drawThrottle', timeout);
},
init: function(element, valueAccessor) {
ko.bindingHandlers.mapChart.render(element, valueAccessor);
},
update: function(element, valueAccessor, allBindingsAccessor) {
if (typeof allBindingsAccessor().mapChart.visible != 'undefined') {
if (
(typeof allBindingsAccessor().mapChart.visible == 'boolean' &&
allBindingsAccessor().mapChart.visible) ||
(typeof allBindingsAccessor().mapChart.visible == 'function' &&
allBindingsAccessor().mapChart.visible())
) {
$(element).show();
ko.bindingHandlers.mapChart.render(element, valueAccessor);
} else {
$(element).hide();
}
} else {
ko.bindingHandlers.mapChart.render(element, valueAccessor);
}
}
};