-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd3by5-map.js
1202 lines (1009 loc) · 43.1 KB
/
d3by5-map.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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* jshint laxcomma: true, quotmark: single */
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['lodash', 'd3', 'd3-geo-projection', 'topojson', 'textures', 'iso-3166-1'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('lodash'), require('d3'), require('d3-geo-projection'), require('topojson'), require('textures'), require('iso-3166-1'));
} else {
root.Map = factory(root._, root.d3, root.geoProjection, root.topojson, root.textures, root.iso3166);
}
}(this, function (_, d3, geoProjection, topojson, textures, iso3166) {
'use:strict';
function Map () {
var map = {
/**
* Default option values
*/
options : {
debug: false,
data : {values : null}, // data to visualize. TODO: remove values
geoData : {}, // topoJson (only for now)
width : 960,
height : 500,
aspectRatio: 500 / 960,
margin: 20, // for now single value
offSetX: 0, // shifts the entire map horisointally in pixels
offSetY: 0, // shifts the entire map veritcaly in pixels
zoomMin: 1, // min zoom level
zoomMax: 20, // max zoom level
zoomGestures: true, // "Free zoom" (mouse scroll wheel, touch gestures)
zoomControls: false, // toggle zoomButtons
zoomResetOnOceanClick: false, // reset zoom on background area click
baseColor: '#F8EBCB',
backgroundgColor: 'none', // 'none' for transparent or #HEX
accentColor: '#FF6B6B',
borderColor: '#9DBFB1',
// fillColorMap: {}, // for simple mapping of fillColors
projection: 'geoMercator', // projection
scale: 0.85, // not quiter sure about this one
responsive: false,
graticule: false, // graticules: a uniform grid of meridians and parallels for showing projection distortion
projectionFit: true, // tries to fit the map inside container. Todo: rename ??
countryIsoCodeMap : 'properties.countryCode', // If the topojson does not store the ISO code in "geometries.id", use this to map the correct attribute (uses dot notation, eg "properties.countryCode")
geometries: 'countries', // TODO: Document. References the topojson geometries object
color: null,
texture: null,
colorKey: null,
textureKey: null,
showLabels: 0, // Bboolean or integer] false/0 = off, true/1 = on, 2 - 20 = zoomLevelTreshold
higlightOnHover: false, // higlight country (or group of countries) on hover
showToolTipOn: 'hover', // [click, hover, none]
toolTipTemplate: function(d, data) {
return '<h3 class="d3x5_tooltip__header">' + d.properties.name + '</h3>';
},
assetsUrl: '/', // base path to asset files
classPrefix: 'd3x5_',
// this should maybe be implented as a plugin or similar
texturesConfig: {
lines: {
size: 20,
strokeWidth: 7,
stroke: '#2DAE3B',
background: '#F8EBCB'
},
circles: {
radius: 8,
size: 30,
strokeWidth: 4,
stroke: 'blue',
fill: '#2DAE3B',
background: '#F8EBCB'
},
paths: {
d: 'crosses', // hexagons, caps, woven, waves, nylon, squares
strokeWidth: 2,
stroke: '#2DAE3B',
background: '#F8EBCB'
},
},
},
/**
* init map
* @param {object} selection d3 selction
* @return {object} the map intance
*/
init: function (selection) {
if (arguments.length) {
// pass along the selection
map.selection = selection;
// use selection to set a unique identifier (TODO: but what if selection is based on class ??)
map.id = selection.node().id;
// Add required position: relative to element (for position of tooltips)
selection.style('position', 'relative');
// hook up listener to resize if responsive
d3.select(window).on('resize', _.bind(map.resize, map));
// draw the map
map.draw();
}
return this;
},
/**
* [draw description]
* @return {[type]} [description]
*/
draw: function () {
var that = this
, opt = this.options // just to shorten...
, data = opt.data.values // will probaby need a parser here at some point. TODO: Drop Values. title, schema etc be seperate options
, scale = opt.scale
, width = opt.width
, height = opt.height
, zoom
, aspectRatio = opt.aspectRatio
, projection
, color
, path
, dom
, svg
, rect
, g
, countries
, neighbors
, graticule
, fillTextures = null
, multipleColors = false // when looping colorKeys, this get flipped to true of we find a country with moe than one colorkey defined
, active = d3.select(null) // selected country
;
this.selection.each(function() {
// calculate responsive dimensions: We override width, height and scale.
if(opt.responsive) {
// get the canvas dimensions
canvasWidth = this.getBoundingClientRect().width;
canvasHeight = window.innerHeight;
// if aspectRatio is not set explicitly, re-calculate
if(aspectRatio === opt.height / opt.width) {
aspectRatio = canvasHeight / canvasWidth;
}
width = canvasWidth;
height = width * aspectRatio;
scale = Math.max(1, width / opt.width); // not really thought this through yet...
}
// Projection
projection = that.setProjection(width, height, scale);
// Path (TODO: maybe move into setProjection() ??!?)
path = d3.geoPath().projection(projection);
// zoom
zoom = d3.zoom()
.scaleExtent([opt.zoomMin, opt.zoomMax]) // min max zoom levels
.on('zoom', zoomed)
.on('start', zoomedStart)
.on('end', zoomedEnd);
// geoJson
countries = topojson.feature(opt.geoData, opt.geoData.objects[opt.geometries]).features;
neighbors = topojson.neighbors(opt.geoData.objects[opt.geometries].geometries);
// mapping iso country code in geoData (topojson).
if(opt.countryIsoCodeMap) {
var countryIsoCodeMap = opt.countryIsoCodeMap.split('.');
countries.forEach( function (d) {
var id = null;
for (var i=0; i<countryIsoCodeMap.length; i++){
id = id ? id[countryIsoCodeMap[i]] : d[countryIsoCodeMap[i]];
}
if (id) {
if (iso3166) {
d.id = iso3166.whereAlpha2(id).alpha2 || iso3166.whereAlpha3(id).alpha2 || iso3166.whereNumeric(id).alpha2 || d.id;
} else {
d.id = id
}
}
// console.log(d.id);
});
// Parse data that comes in other shapes and forms (move this somwhere probably)
// Need to define a set schema.
if (data && (data.columns || Array.isArray(data))) {
var key = null;
var notFound = [];
var keys = [_.last(countryIsoCodeMap, 1), 'id', 'countrycode', 'countryCode', 'country', 'Country', 'countryName', 'countryname'];
var parseDataByKey = function (key) {
data.countries = {};
data.forEach( function (d) {
var id = d[key];
var country = null;
if (iso3166) {
if (id) {
country = iso3166.whereAlpha2(id) || iso3166.whereAlpha3(id) || iso3166.whereNumeric(d3.format('03')(id)) || iso3166.whereCountry(id);
}
if (country) {
data.countries[country.alpha3] = d;
} else {
notFound.push(d.country);
}
} else {
data.countries[id] = d;
}
});
}
// 1) look for a countryCode or countryName
if (data.columns) { //d3.csv() exposes a columns property
key = keys.find(function(d) {
return data.columns.includes(d);
});
// 2) if data is an array of countries
} else if (Array.isArray(data)) {
key = keys.find(function(d) {
return Object.keys(data[0]).includes(d);
});
}
if ( key ) {
// console.log(key);
parseDataByKey(key);
}
if (that.debug && notFound.length) {
console.log('Warning! Not found: ' + notFound.join(', '));
}
}
}
// extend countries properties with custom data
// TODO. Ad hoc for now, currently just assumes that the data has a country alph3 id as key...
countries.forEach( function (c) {
if (iso3166) {
var country = iso3166.whereAlpha2(c.id.toString());
if (country) {
var id = country.alpha3; // tmp ad hoc alpha3 conversion
if(_.has(data.countries, id)) {
_.assign(c.properties, data.countries[id]);
}
}
} else {
if(_.has(data.countries, c.id)) {
_.assign(c.properties, data.countries[c.id]);
}
}
});
// Colors
// Sett scale defaults. Ordinal scales must be explicit and determined
if(opt.color && typeof opt.color.unknown === 'function') {
opt.color.unknown(opt.baseColor);
}
// here we loop countries and check for any keys that map to colors. Defaults to country ID.
// The keys are stored in a country colorKey property (array) and used as domain input in the defined color scale
// We also test for multiple colors, so that we can use textures if needed.
if(opt.color && opt.colorKey) {
var colorKey = opt.colorKey.split('.');
countries.forEach( function (d) {
var key = null;
for (var i = 0; i < colorKey.length; i++){
key = key ? key[colorKey[i]] : d[colorKey[i]];
}
d.colorKey = key || d.id; // Defaults to country ID
// typecast to array
d.colorKey = Array.isArray(d.colorKey) ? d.colorKey : [d.colorKey];
// more than one color key ?
if(d.colorKey.length > 1) {
multipleColors = true;
}
});
}
// more or less same as above.
// TODO: dry out this if possible...
if(opt.textureKey) {
var textureKey = opt.textureKey.split('.');
countries.forEach( function (d) {
var id = null;
for (var i=0; i<textureKey.length; i++){
id = id ? id[textureKey[i]] : d[textureKey[i]];
}
d.textureKey = id || null;
});
}
// start wrting to the dom
dom = d3.select(this);
// dom.style('background', opt.backgroundgColor);
// cleanup if on redraw. TODO: not very elegant...
if (dom.select('svg')) {
dom.select('svg').remove();
}
if (dom.select('.'+opt.classPrefix+'tooltip')) {
dom.select('.'+opt.classPrefix+'tooltip').remove();
}
// svg element
svg = dom.append('svg')
.attr('width', width)
.attr('height', height)
.attr('display', 'block');
if(opt.offSetX || opt.offSetY) {
svg.style('transform', function(){
return 'translateX(' + opt.offSetX + 'px) translateY(' + opt.offSetY + 'px)';
});
}
// Free Zoom (mouseweel, etc)
if(opt.zoomGestures) {
svg.call(zoom);
}
// Init Textures
if(opt.texture || multipleColors) {
// Create texture which combines two colors. TODO: Possible to do more than two ??!?
if(multipleColors) {
opt.texture = d3.scaleOrdinal()
.domain(opt.texture ? _.concat(opt.texture.domain(), opt.color.domain()) : opt.color.domain())
.range(opt.texture ? _.concat(opt.texture.range(), opt.color.range()) : opt.color.range());
}
// Sett scale defaults. Ordinal scales must be explicit and determined
opt.texture.unknown(null);
fillTextures = that.setTextures();
// create all possible textures we might need to use later
if(fillTextures) {
_.forEach(fillTextures, function(t) {
_.forEach(t, function(tc) {
if(_.isFunction(tc)) {
svg.call(tc); // pattern textures
} else {
_.forEach(tc, function(cc) {
if(_.isFunction(cc)) {
svg.call(cc); //color textures
}
});
}
});
});
}
}
rect = svg.append('rect')
.attr('width', width)
.attr('height', height)
.style('fill-opacity', function(){
return opt.backgroundgColor === 'none' ? 0 : 1;
});
if (opt.backgroundgColor !== 'none') {
rect.style('fill', opt.backgroundgColor);
}
if (opt.zoomResetOnOceanClick) {
rect.on('click', resetZoom);
}
g = svg.append('g').style('font-size', '0.5em');
// lat/lng lines
if(opt.graticule) {
graticule = d3.geoGraticule();
g.append('path')
.datum(graticule)
.classed(opt.classPrefix + 'graticule', true)
.attr('d', path);
}
/* alternativ method of using multiple layers and a base map... */
// basemap = svg.append("g");
// basemap.selectAll('.'+opt.classPrefix+'basecountry')
// .data(countries)
// .enter()
// .append("path")
// .attr("d", path)
// .style("fill", opt.baseColor);
var features = g.selectAll('.'+opt.classPrefix+'country')
.data(countries)
.enter()
.append('path')
.attr('d', path)
.attr('id', function(d) {
return d.id;
})
.attr('class', function(d) {
return opt.classPrefix + 'country ' + _.join(d.properties.groups, ' ');
})
.style('fill', function(d, i) { // TODO move all this to a setFill()-function ??!?
// default fill with base color
var id
var fill = opt.baseColor;
// get country
if (iso3166) {
var country = iso3166.whereAlpha2(d.id.toString()); // probably best to fork this and make sure it can:
// typecast strings and
// return undefined
// look up more fuzzy (st/Saints, &/and, short forms)
id = country ? country.alpha3 : null; // tmp ad hoc alpha3 conversion
} else {
id = d.id;
}
if(opt.color && data.countries[id]) { // ad hoc force alpha3. TOOD: Test for this
fill = opt.color(d.colorKey[0]);
// multiple colors, use textures
if(d.colorKey.length > 1) {
fill2 = opt.color(d.colorKey[1]);
if(fillTextures.colors[fill] && fillTextures.colors[fill][fill2]) { // defensive programing FTW
fill = fillTextures.colors[fill][fill2].url();
// Not a texture as we are displaying only one color for countries which belongs to multiple color groups
} else if(fill2 !== opt.baseColor) {
fill = fill2;
}
}
else if(fillTextures && opt.texture(d.textureKey)) {
fill = fillTextures[opt.texture(d.textureKey)][fill].url();
}
}
return fill;
// This colors countries by neigbors. Implement as an option
// return color(d.color = d3.max(neighbors[i], function(n) {
// return countries[n].color;
// }) + 1 | 0);ble
})
.classed(opt.classPrefix + 'clickable', true)
.on('click', clicked)
.on('mouseover', that.mouseOver)
.on('mouseout', that.mouseOut)
.transition().duration(0) // 250 TODO: Make thia an optionsparameter (animateDraw ??!?)
;
// add border-lines between countries
// TODO Toggle as option
g.append('path')
.datum(topojson.mesh(opt.geoData, opt.geoData.objects[opt.geometries], function(a, b) { return a !== b; }))
.classed(opt.classPrefix + 'boundary', true)
.attr('d', path)
.style('fill', 'none')
.style('stroke', opt.borderColor);
// optinal method of applying fill to entire groups
// _.forEach(data.groups, function(d, key) {
// if(d.countries.length) {
// var groupSelector = '#' + _.join(_.map(d.countries, 'id'), ', #');
// var elements = d3.selectAll(groupSelector);
// var fill;
// var color = opt.baseColor;
// var texture;
// if(_.has(opt.fillColorMap, d.name)) {
// color = opt.fillColorMap[d.name];
// }
// if(_.has(opt.texturesMap, d.name)) {
// texture = fillTextures[opt.texturesMap[d.name]][color].url();
// }
// fill = texture ? texture : color;
// elements.attr('fill', fill);
// }
// });
// add tooltip container element to dom
if( opt.showToolTipOn === 'click' || opt.showToolTipOn === 'hover' ) {
map.initToolTip();
}
/******************************************
Zoom methods
TODO: events (start, end) seems to fire twice...
/*************************************** */
function zoomed() {
var transform = d3.event.transform; // same as d3.zoomTransform(this);
g.style('stroke-width', d3.min([1.5 / transform.k, 0.5]) + 'px'); // tweak stroke width according to zoom
g.style('font-size', d3.min([1 / transform.k, 0.5]) + 'em'); // tweak font-size according to zoom
g.attr('transform', transform); // apply transform
}
function zoomedStart() {
// console.log(d3.event.type + ' zoom');
}
function zoomedEnd() {
var opt = map.options
, svg = d3.select(this)
, zoomLevel = Math.round(d3.event.transform.k)
, isZoomed = (zoomLevel > opt.zoomMin)
, isMaxZoom = (zoomLevel === opt.zoomMax)
, zoomIn
, zoomOut
;
// toggle classes on zoom UI
// rect.classed(opt.classPrefix + 'clickable', isZoomed);
if (opt.zoomControls) {
zoomIn = svg.select('.'+opt.classPrefix+'zoom-control--in');
zoomOut = svg.select('.'+opt.classPrefix+'zoom-control--out');
zoomOut.classed(opt.classPrefix + 'clickable',isZoomed);
zoomIn.classed(opt.classPrefix + 'clickable', !isMaxZoom);
}
// indicate that the ocean is clickable
if (opt.zoomResetOnOceanClick) {
rect.classed(opt.classPrefix + 'clickable', isZoomed);
}
// close tool tip when zoomed all the way out
if(!isZoomed && (!d3.event.sourceEvent || d3.event.sourceEvent.srcElement.id !== 'RUS')) { // TODO: fails on russia
map.selection.select('.'+opt.classPrefix + 'tooltip').classed(opt.classPrefix + 'hidden', true);
}
// if showLabels is a threshold value
if (opt.showLabels && opt.showLabels > 1) {
if (zoomLevel > opt.showLabels) {
showLabels();
} else {
hideLabels();
}
}
// console.log(d3.event.type + ' zoom');
// console.log('Current zoom level: ' + zoomLevel);
}
function resetZoom() {
active.classed(opt.classPrefix + 'active', false);
active = d3.select(null);
svg.transition().duration(750).call(zoom.transform, d3.zoomIdentity);
}
/******************************************
Labels
/*************************************** */
// Display labels if ture and not set to a zoom threshold
if(opt.showLabels && opt.showLabels < 2) {
showLabels();
}
//TODO: Option: showLabel('name') // d.properties
function showLabels() {
var labels
, opt = map.options;
labels = g.selectAll('.'+opt.classPrefix+'label')
.data(countries)
.enter()
.append('text')
.classed(opt.classPrefix + 'label', true)
.attr('text-anchor','middle')
.attr('dy', '.35em')
.style('fill', '#777') // option: LabelTextColor
.style('opacity', 0)
.attr('transform', function(d) {
if(!isNaN(path.centroid(d)[0])) {
var latlng = path.centroid(d);
// ad hoc position fix
if (d.id === 'KNA') {
latlng = [latlng[0],latlng[1] + 2];
} else if (d.id === 'BRB') {
latlng = [latlng[0],latlng[1] + 2];
}
else if (d.id === 'KIR') {
return 'translate(' + [117.38496987005313, 694.1484886048762] + ')';
}
return 'translate(' + latlng + ')';
}
})
.text(function(d,i) {
// ad hoc just skip Saint Martin (French part)
if (d.id === 'SXM' || d.id === 'MAF') {
return '';
}
return (d.properties.name);
})
.transition().duration(350).style('opacity', 1) // TODO: Make this an optionsparameter (animateDraw ??!?)
;
}
function hideLabels() {
g.selectAll('.'+opt.classPrefix+'label')
.transition().duration(500).style('opacity', 0).remove();
// console.log('hideLabels');
}
/******************************************
Map Controls (zoom-button)
/*************************************** */
if(opt.zoomControls) {
var controls = svg.append('g').classed(opt.classPrefix + 'zoom-controls', true);
var controlColor = opt.accentColor;
var controlItems = controls.selectAll('.'+opt.classPrefix+'zoom-control')
.data(['zoom_in', 'zoom_out'])
.enter()
.append('g')
.attr('class', function(d, i) {
return opt.classPrefix + 'zoom-control ' + (i ? opt.classPrefix + 'zoom-control--out' : opt.classPrefix + 'zoom-control--in d3x5_clickable');
})
.style('transform', function(d, i){
return 'translateX(' + (30 + 60*i) + 'px) translateY(' + 30 + 'px)';
})
.style('font-size', '3em')
.style('font-weight', 'bold')
;
controlItems.append('text')
.style('fill', controlColor)
.attr('y', 23)
.attr('x', 25)
.attr('text-anchor','middle')
.attr('dy', '.35em')
.text(function(d,i){ return i ? '-' : '+';});
controlItems.append('rect')
.attr('id', function(d){return d;})
.attr('width', 50)
.attr('height', 50)
.style('fill', controlColor)
.style('fill-opacity', 0)
.style('stroke', controlColor)
.style('stroke-width', '2px')
.on('click', function(d) {
var factor = (d === 'zoom_in') ? 2 : 1/2
, selection = map.selection.select('svg').transition().duration(350);
zoom.scaleBy(selection, factor);
});
}
/******************************************
Events
/*************************************** */
/**
* [clicked description]
* @param {[type]} d [description]
* @return {[type]} [description]
*/
function clicked(d) {
// var opt = map.options;
// don't collide with d3 zoom/pan -> bail out
if (d3.event.defaultPrevented) {
return;
}
// what to do if country already is selected ("active")
if (active.node() === this) {
// zoom out ?
// return resetZoom();
// toggle toolTip?
// if(opt.showToolTipOn == 'click') {
// d3.select('.'+opt.classPrefix+'tooltip').classed(opt.classPrefix + 'hidden', true);
// }
}
// pan and zoom to active county
active.classed(opt.classPrefix + 'active', false);
active = d3.select(this).classed(opt.classPrefix + 'active', true);
var bounds = path.bounds(d)
, dx = bounds[1][0] - bounds[0][0]
, dy = bounds[1][1] - bounds[0][1]
, x = (bounds[0][0] + bounds[1][0]) / 2
, y = (bounds[0][1] + bounds[1][1]) / 2
, scale
, translate
;
scale = Math.max(opt.zoomMin, Math.min(opt.zoomMax, opt.zoomMin / Math.max(dx / width, dy / height)));
// since Russia is so huge, we increase the zoom and shift the panning a bit to get a better effect.
if(d.id === 'RUS') {
scale = scale * 2;
x = x * 1.3;
}
translate = [width / 2 - scale * x, height / 2 - scale * y];
svg.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity.translate(translate[0], translate[1]).scale(scale));
// show tooltip if bound to click
if(map.options.showToolTipOn == 'click') {
map.showToolTip(d, this, 'fixed');
}
}
});
}, // end draw function
/**
* first version of simple method to redraw on resize
* @return {[type]} [description]
*/
resize: function() {
//clean up
this.selection.each(function() {
this.childNodes[0].remove();
});
// redraw
this.draw();
},
/**
* mouseOver: Highlight all relevant groups
* @param {[type]} d [description]
* @return {[type]} [description]
*/
mouseOver: function(d) {
var collection
, classList
, match
, opt = map.options
;
// Show tooltip on hover if set
if(opt.showToolTipOn == 'hover') {
map.showToolTip(d, this);
}
// Higlight group on hover
if(opt.higlightOnHover) {
collection = d.properties.groups;
classList = this.classList;
match = _.find(collection, function( c ) {
return classList.contains( c );
});
}
// TODO: Higlight group on hover
if (opt.higlightOnHover == 'country') {
console.log('higlight country');
}
if(collection && match) {
// console.log(collection);
// console.log(opt.color(collection));
// console.log(opt.texture(collection));
// console.log(classList);
// console.log(collection.join(', .'));
map.selection.selectAll('.'+opt.classPrefix+'country')
.classed(opt.classPrefix + 'active', false)
.transition().duration(250).style('opacity', 0.25);
map.selection.selectAll('.'+collection.join(', .'))
.classed(opt.classPrefix + 'active', true)
.transition().duration(250).style('opacity', 1);
// map.selection.selectAll('.'+opt.classPrefix+'country:not(.d3x5_active)').style('fill', opt.baseColor);
// map.selection.selectAll('.'+opt.classPrefix+'country').transition().duration(250).style('opacity', 0.25);
// map.selection.selectAll('.'+collection.join(', .')).transition().duration(250).style('opacity', 1);
}
},
/**
* [mouseOut description]
* @param {[type]} d [description]
* @return {[type]} [description]
*/
mouseOut: function(d) {
var opt = map.options;
map.selection.selectAll('.'+opt.classPrefix+'country').transition().duration(250).style('opacity', 1);
// hide tooltip
if(opt.showToolTipOn === 'hover') {
map.selection.select('.'+opt.classPrefix+'tooltip').classed(opt.classPrefix + 'hidden', true);
}
},
/******************************************
Tooltip
/*************************************** */
/**
* init Tooltip
*/
initToolTip: function() {
var opt = map.options;
// var container = d3.select(this.selection.node().parentNode);
var toolTip = this.selection
.append('div')
.classed(opt.classPrefix + 'tooltip d3x5_hidden', true);
// if on click, add close button
if( this.options.showToolTipOn === 'click') {
toolTip.html('<a class="d3x5_tooltip__close"><img src="' + opt.assetsUrl +'images/close.svg"></a>');
toolTip.select('.'+opt.classPrefix+'tooltip__close').on('click', map.closeToolTip);
}
toolTip.append('div').classed(opt.classPrefix + 'tooltip__content', true);
},
/**
* close Tooltip
*/
closeToolTip: function() {
var opt = map.options
, toolTip = map.selection.select('.'+opt.classPrefix+'tooltip')
, toolTipContent = toolTip.select('.'+opt.classPrefix+'tooltip__content')
;
toolTipContent.html('');
toolTip.classed(opt.classPrefix + 'hidden', true);
},
/**
* show ToolTip
* @param {object} d [description]
* @param {dom} elm [description]
* @param {object} objects [description]
*/
showToolTip: function(d, elm, pos){
var that = this
, opt = map.options
, svg = map.selection.select('svg')
// , toolTip = d3.select(map.selection.node().parentNode).select('.'+opt.classPrefix+'tooltip')
, toolTip = map.selection.select('.'+opt.classPrefix+'tooltip')
, toolTipContent = toolTip.select('.'+opt.classPrefix+'tooltip__content')
, tooltipHtml = map.toolTipContent(d)
, position = pos || 'cursor'
, offset = [0,0]
, cursor
;
// display tooltip
toolTip.classed(opt.classPrefix + 'hidden', false);
// write html content.
toolTipContent.html(tooltipHtml);
// position: fixed
if(position == 'fixed') {
// since we use the centroid, we correct this for some countries for better positioning
var latlng = d3.geoPath().centroid(d);
var xy = projection(latlng);
// console.log(d.id);
switch(d.id) {
case 'CHN':
offset[0] = -400;
break;
case 'PHL':
case 'KOR':
case 'HKG':
case 'AUS':
offset[0] = -200;
break;
case 'ZAF':
offset[1] = -300;
break;
}
this.toolTipPosition(toolTip, xy, offset);
}
// position: move with cursor
else if(position == 'cursor') {
offset = [15, 15];
d3.select(elm).on('mousemove', function() {
var div = d3.select(this);
// get cursor position
cursor = d3.mouse(div.node()).map(function(d) { // svg.node()
return parseInt(d);
});
that.toolTipPosition(toolTip, cursor, offset);
});
}
},
/**
* [toolTipPosition description]
* @param {[type]} tooltip [description]
* @param {[type]} pos [description]
* @return {[type]} [description]
*/
toolTipPosition: function (toolTip, pos, offset) {
toolTip.style('top', (pos[1] + offset[1]) + 'px')
.style('left', (pos[0] + offset[0]) + 'px');
},
/**
* [toolTipContent description]
* @param {[type]} d [description]
* @return {[type]} [description]
*/
toolTipContent: function(d) {
var opt = map.options;
return opt.toolTipTemplate(d, opt.data);
},
/**
* projection
* @param {number} width map width
* @param {number} height map height
* @param {number} scale map scale
*/
setProjection: function(width, height, scale) {
// get either basic projection bundled in d3 or extended from d3-geo-projection
projection = d3[this.options.projection] || geoProjection[this.options.projection];
// init projection
projection = projection();
// set parallel if projection is gall-peters
if(this.options.projection === 'geoCylindricalEqualArea') {
projection.parallel(45);
}
if(this.options.projectionFit) {
var object = topojson.feature(this.options.geoData, {type: 'GeometryCollection', geometries: this.options.geoData.objects[this.options.geometries].geometries });
projection.fitExtent([[this.options.margin, this.options.margin], [width-this.options.margin, height-this.options.margin]], object);
// projection.fitSize([width, height], object);
} else {
projection
.scale(150 * scale)