-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_functions3.js
More file actions
1124 lines (840 loc) · 28.7 KB
/
map_functions3.js
File metadata and controls
1124 lines (840 loc) · 28.7 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
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
// Tim's mapping functions
// Timothy C. Barmann
// tbarmann@projo.com
// 11/16/2010
// last revision 2/22/2011
/*
+ fields array now optional. It is created automatically if it doesn't exist. The label is set to the field's
name; if a field's value is numeric, it is automatically set to be mapable
*/
function isNumeric(n) {
var n2 = n;
n = parseFloat(n);
return (n!='NaN' && n2==n);
}
function getProperties(obj) {
var i, v;
var count = 0;
var props = [];
if (typeof(obj) === 'object') {
for (i in obj) {
v = obj[i];
if (v !== undefined && typeof(v) !== 'function') {
props[count] = i;
count++;
}
}
}
return props;
};
String.prototype.stripNonNumeric = function() {
var str = this;
str += '';
var rgx = /^\d|\.|-$/;
var out = '';
for( var i = 0; i < str.length; i++ ) {
if( rgx.test( str.charAt(i) ) ) {
if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) || ( str.charAt(i) == '-' && out.length != 0 ) ) ) {
out += str.charAt(i);
}
}
}
return out;
};
/**
* Formats the number according to the 'format' string; adherses to the american number standard where a comma is inserted after every 3 digits.
* note: there should be only 1 contiguous number in the format, where a number consists of digits, period, and commas
* any other characters can be wrapped around this number, including '$', '%', or text
* examples (123456.789):
* '0' - (123456) show only digits, no precision
* '0.00' - (123456.78) show only digits, 2 precision
* '0.0000' - (123456.7890) show only digits, 4 precision
* '0,000' - (123,456) show comma and digits, no precision
* '0,000.00' - (123,456.78) show comma and digits, 2 precision
* '0,0.00' - (123,456.78) shortcut method, show comma and digits, 2 precision
*
* @method format
* @param format {string} the way you would like to format this text
* @return {string} the formatted number
* @public
* from: http://mattsnider.com/javascript/numbers-and-number-format-function/
*/
Number.prototype.format = function(format) {
if ((typeof format != 'string') || (format.length <1)) {return this;} // sanity check
var hasComma = -1 < format.indexOf(','),
psplit = format.stripNonNumeric().split('.'),
that = this;
var hasSign = (-1 < format.indexOf('+'));
var isNotPositive = (parseFloat(this) <= 0);
// compute precision
if (1 < psplit.length) {
// fix number precision
that = that.toFixed(psplit[1].length);
}
// error: too many periods
else if (2 < psplit.length) {
throw('NumberFormatException: invalid format, formats should have no more than 1 period: ' + format);
}
// remove precision
else {
that = that.toFixed(0);
}
// get the string now that precision is correct
var fnum = that.toString();
// format has comma, then compute commas
if (hasComma) {
// remove precision for computation
psplit = fnum.split('.');
var cnum = psplit[0],
parr = [],
j = cnum.length,
m = Math.floor(j / 3),
n = cnum.length % 3 || 3; // n cannot be ZERO or causes infinite loop
// break the number into chunks of 3 digits; first chunk may be less than 3
for (var i = 0; i < j; i += n) {
if (i != 0) {n = 3;}
parr[parr.length] = cnum.substr(i, n);
m -= 1;
}
// put chunks back together, separated by comma
fnum = parr.join(',');
// add the precision back in
if (psplit[1]) {fnum += '.' + psplit[1];}
}
if ((hasSign) && (isNotPositive)) {
format = format.replace(/\+/,"");
}
// replace the number portion of the format with fnum
return format.replace(/[\d,?\.?]+/, fnum);
};
// IE doesn't have Array.map, so this adds it
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
res[i] = fun.call(thisp, t[i], i, t);
}
return res;
};
}
String.prototype.replaceSpacesLowerCase = function() {
var str = this.replace(/\s/g, '_');
str = str.toLowerCase();
return str;
};
String.prototype.restoreSpacesProperCase = function()
{
var str = this.replace(/_/g, ' ');
return str.toLowerCase().replace(/^(.)|\s(.)/g,
function($1) { return $1.toUpperCase(); });
}
Array.prototype.getUnique = function () {
var hash = new Object();
for (j = 0; j < this.length; j++) {hash[this[j]] = true}
var array = new Array();
for (value in hash) {array.push(value)};
return array;
}
/* extractPropertyArray(array, prop) ----------------------------------------- */
/* takes an array of objects and returns a single dimensional
array containing only the specified property:
var data = [{"fname":"bob", "lname":"smith"},
{"fname":"john", "lname":"jones"},
{"fname":"jimmy", "lname":"greenjeans"}
];
var lastnames = extractPropertyArray(data, "lname");
lastnames will = ["smith","jones","greenjeans"];
*/
function extractPropertyArray(array, prop) {
var values = array.map(function (el) {
return (el[prop]);
});
return values;
}
/* -----------------------------------------------------------------*/
function sortfunction(a, b){
return (a - b) //causes an array to be sorted numerically and ascending
}
function generateColor(ranges) {
if (!ranges) {
ranges = [
[150,256],
[50, 190],
[50, 256]
];
}
var g = function() {
//select random range and remove
var range = ranges.splice(Math.floor(Math.random()*ranges.length), 1)[0];
//pick a random number from within the range
return Math.floor(Math.random() * (range[1] - range[0])) + range[0];
}
var rgb = {};
rgb.r = g();
rgb.g = g();
rgb.b = g();
return '' + RGBToHex(rgb);
}
function initPalette() {
// globals: field object, map_init object
// field.steps defined in setStepBounds()
// if palette is defined already
// just return
if (field.hasOwnProperty('palette')) {
if (field.palette.length > 1) {
return;
}
}
// there is no palette defined, or it contains only 1 color
field.palette = [];
if (field.hasOwnProperty('type')) {
if (field.type == 'unique_value') {
for (var x=0;x<field.steps;x++) {
field.palette[x] = generateColor();
}
return;
}
}
else {
field.type = 'range';
}
if (!field.hasOwnProperty('start_color')) {
field.start_color = map_init.default_start_color;
}
if (!field.hasOwnProperty('end_color')) {
field.end_color = map_init.default_end_color;
}
field.palette = mixPalette(field.start_color,field.end_color,field.steps);
return;
}
function mixPalette(start_hex,end_hex,steps) {
// globals: field object
var step = {};
var start_rgb = hexToRGB(start_hex);
var end_rgb = hexToRGB(end_hex);
var this_palette = [];
var denominator = (steps<2) ? 1 : steps-1;
step.r = (end_rgb.r - start_rgb.r) / denominator;
step.g = (end_rgb.g - start_rgb.g) / denominator;
step.b = (end_rgb.b - start_rgb.b) / denominator;
for (var i = 0; i < steps; i++) {
var this_rgb = {};
this_rgb.r = start_rgb.r + (step.r * i);
this_rgb.g = start_rgb.g + (step.g * i);
this_rgb.b = start_rgb.b + (step.b * i);
this_palette.push(RGBToHex(this_rgb));
}
return this_palette;
}
function hexToRGB (color_str) {
color_str = color_str.toUpperCase();
color_str = color_str.replace(/[\#rgb\(]*/,'');
if (color_str.length == 3) {
var r = color_str.substr(0,1);
var g = color_str.substr(1,1);
var b = color_str.substr(2,1);
color_str = r + r + g + g + b + b;
}
var red_hex = color_str.substr(0,2);
var green_hex = color_str.substr(2,2);
var blue_hex = color_str.substr(4,2);
var this_color = {};
this_color.r = parseInt(red_hex,16);
this_color.g = parseInt(green_hex,16);
this_color.b = parseInt(blue_hex,16);
return this_color;
}
function RGBToHex (rgb) {
var r = (parseInt(rgb.r,10)).toString(16);
var g = (parseInt(rgb.g,10)).toString(16);
var b = (parseInt(rgb.b,10)).toString(16);
r= (r.length == 1) ? '0' + r : r;
g= (g.length == 1) ? '0' + g : g;
b= (b.length == 1) ? '0' + b : b;
return (r+g+b).toUpperCase();
}
function locDataLookup(loc,field) {
// globals: map_init
var join_field = map_init.join_field;
loc = loc.replaceSpacesLowerCase();
for (var x=0;x<map_init.datasource.length;x++) {
var new_loc_name = map_init.datasource[x][join_field].replaceSpacesLowerCase();
if (new_loc_name == loc)
return map_init.datasource[x][field.name];
}
return 0;
}
// Point object
function Point(x,y) {
this.x=x;
this.y=y;
}
// Contour object
function Contour(a) {
this.pts = []; // an array of Point objects defining the contour
}
// ...add points to the contour...
Contour.prototype.area = function() {
var area=0;
var pts = this.pts;
var nPts = pts.length;
var j=nPts-1;
var p1; var p2;
for (var i=0;i<nPts;j=i++) {
p1=pts[i]; p2=pts[j];
area+=p1.x*p2.y;
area-=p1.y*p2.x;
}
area/=2;
return area;
};
Contour.prototype.centroid = function() {
var pts = this. pts;
var nPts = pts.length;
var x=0; var y=0;
var f;
var j=nPts-1;
var p1; var p2;
for (var i=0;i<nPts;j=i++) {
p1=pts[i]; p2=pts[j];
f=p1.x*p2.y-p2.x*p1.y;
x+=(p1.x+p2.x)*f;
y+=(p1.y+p2.y)*f;
}
f=this.area()*6;
return new Point(parseInt(x/f),parseInt(y/f));
};
////////////// MAP RESIZING AND MOVING //////////////////////////////////////////////////////////////
function mapResizePct(pct) {
//global: map_init
// first time through, make a copy of all the coordinates so we have the original data
// when the map is resized
// stored in 2 dimensional array in global object map_init.coordArry
//if (!map_init.hasOwnProperty('coordArry')) {
map_init.coordArry = [];
$("area").each (function (index) {
// get the coordinate pairs for this polygon area and put them in an array
var coordStr = $(this).attr('coords');
var thisCoordArry = coordStr.split(',');
map_init.coordArry.push(thisCoordArry);
});
// }
// make a copy of the array of coordinates, stored in global object map_init
// necessary so we can modify the coordinates without changing the originals
var temp = {};
jQuery.extend(true,temp,map_init);
var coordArry = temp.coordArry.slice();
// console.log(map_init.coordArry[0]);
for (var x=0;x<coordArry.length;x++) {
for (y=0;y<coordArry[x].length;y++) {
coordArry[x][y] = Math.round(coordArry[x][y] * pct);
}
}
$("area").each (function (index) {
coordStr = coordArry[index].join();
$(this).attr('coords',coordStr);
});
}
// ------------------------------------------------------------------------------------
function mapResizeWidth(width) {
var maxXY = getMaxXY("area");
var pct = width/maxXY.x;
mapResizePct(pct);
}
// ------------------------------------------------------------------------------------
function moveArea(area_selector,x_offset,y_offset) {
$(area_selector).each(function() {
var xArray=[], yArray=[];
var coordStr = $(this).attr('coords');
var newCoordArry = [];
var coordArry = coordStr.split(',');
for (var j=0;j<coordArry.length-1;j+=2) {
newCoordArry[j] = (parseInt(coordArry[j])+x_offset);
newCoordArry[j+1] = (parseInt(coordArry[j+1])+y_offset);
}
coordStr = newCoordArry.join();
$(this).attr('coords',coordStr);
});
}
// ------------------------------------------------------------------------------------
function cornerMap(area_selector) {
// move the map so the top left corner is at 0,0
var top_left = getMinXY(area_selector);
moveArea(area_selector,-top_left.x, -top_left.y);
}
// ------------------------------------------------------------------------------------
function centerMapHere(area_selector, x,y) {
}
// ------------------------------------------------------------------------------------
function getCenterPoint(area_selector) {
var maxX=[], maxY=[], minX=[], minY=[], xArray=[], yArray=[];
$(area_selector).each(function() {
var coordStr = $(this).attr('coords');
var coordArry = coordStr.split(',');
for (var j=0;j<coordArry.length-1;j+=2) {
xArray.push(coordArry[j]);
yArray.push(coordArry[j+1]);
}
});
maxX= Math.max.apply(Math,xArray);
maxY= Math.max.apply(Math,yArray);
minX= Math.min.apply(Math,xArray);
minY= Math.min.apply(Math,yArray);
console.log('Center: ' + parseInt((maxX-minX)/2) + ',' + parseInt((maxY-minY)/2));
return new Point(parseInt((maxX-minX)/2),parseInt((maxY-minY)/2));
}
// ------------------------------------------------------------------------------------
function zoom_in() {
if (map_init.current_zoom_level < map_init.max_zoom_level) {
map_init.current_zoom_level++;
var pct = 1 + (map_init.current_zoom_level/10);
console.log(pct);
mapResizePct(pct);
labelMap();
colorMap();
buildLegend();
$('.map').maphilight();
// place legend div
var legend_width = parseInt($('.map_legend').css('width'));
var container_width = parseInt($('.map_container').css('width'))+legend_width_offset;
$('.map_legend').css('left',(container_width-legend_left_offset)+'px');
$('.map_legend').css('top',0 + legend_top_offset + 'px');
}
}
// ------------------------------------------------------------------------------------
function zoom_out() {
if (map_init.current_zoom_level > map_init.min_zoom_level) {
map_init.current_zoom_level=map_init.current_zoom_level-1;
var pct = 1 - (map_init.current_zoom_level/-10);
console.log(pct);
mapResizePct(pct);
labelMap();
colorMap();
buildLegend();
$('.map').maphilight();
// place legend div
var legend_width = parseInt($('.map_legend').css('width'));
var container_width = parseInt($('.map_container').css('width'))+legend_width_offset;
$('.map_legend').css('left',(container_width-legend_left_offset)+'px');
$('.map_legend').css('top',0 + legend_top_offset + 'px');
}
}
// ------------------------------------------------------------------------------------
function getMaxXY (selector) {
var xArry = [];
var yArry = [];
$(selector).each (function (i) {
// get the coordinate pairs for each polygon area and put them in an array
var coordStr = $(this).attr('coords');
var coordArry = coordStr.split(',');
// split off the x's in one array, the y's in another
for (x=0;x<coordArry.length;x+=2) {
xArry.push(parseInt(coordArry[x]));
yArry.push(parseInt(coordArry[x+1]));
}
}); // each
// get max,min, average for the x's and y's
// the average x,y will be the center of the square that bounds the polygon
var xmax = Math.max.apply(Math, xArry);
var ymax = Math.max.apply(Math, yArry);
return new Point(xmax,ymax);
}
// ------------------------------------------------------------------------------------
function getMinXY (selector) {
var xArry = [];
var yArry = [];
$(selector).each (function (i) {
// get the coordinate pairs for each polygon area and put them in an array
var coordStr = $(this).attr('coords');
var coordArry = coordStr.split(',');
// split off the x's in one array, the y's in another
for (x=0;x<coordArry.length;x+=2) {
xArry.push(parseInt(coordArry[x]));
yArry.push(parseInt(coordArry[x+1]));
}
}); // each
// get min for the x's and y's
var xmin = Math.min.apply(Math, xArry);
var ymin = Math.min.apply(Math, yArry);
return new Point(xmin,ymin);
}
// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------
function getMaxPropValueInArray(arr,prop) {
var unique_value = extractPropertyArray(arr,prop).sort(function(a,b){return a - b});
return unique_value[unique_value.length-1];
}
// ------------------------------------------------------------------------------------
function getMinPropValueInArray(arr,prop) {
var unique_value = extractPropertyArray(arr,prop).sort(function(a,b){return a - b});
return unique_value[0];
}
// ------------------------------------------------------------------------------------
Array.maxProp = function (array, prop) {
var values = array.map(function (el) {
return el[prop];
});
return Math.max.apply(Math, values);
};
// ------------------------------------------------------------------------------------
Array.minProp = function (array, prop) {
var values = array.map(function (el) {
return el[prop];
});
return Math.min.apply(Math, values);
};
// ------------------------------------------------------------------------------------
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
// ------------------------------------------------------------------------------------
function getLegendStep(target) {
// given a target value, searches through all datasource values of a particular field
// and figures out which step the target value belongs
// globals: datasource, field, steps, map_init
if (field.type==="unique_value") {
for (var x=0;x<field.values.length;x++) {
if (field.values[x]==target) {
return x;
}
}
alert ("Error target in unique values not found: " + target);
return null;
}
else {
for (var index=0; index<field.boundaries.length; index++) {
if ((target >= field.boundaries[index]['lower']) && (target < field.boundaries[index]['upper'])) {
return index;
}
}
alert ("Error target in boundaries array not found: " + target);
return null;
}
}
// ------------------------------------------------------------------------------------
function extractLoc(thisOnMouseOverStr) {
// Extract the parameter - this is the location name
var thisOnMouseOverArray = thisOnMouseOverStr.split("'");
var loc = thisOnMouseOverArray[1];
loc = loc.replaceSpacesLowerCase();
return loc;
}
// ------------------------------------------------------------------------------------
function isEven(x) { return (x%2)?false:true; }
function isOdd(x) { return (x%2)?true:false; }
// ------------------------------------------------------------------------------------
function mapOnMouseOver(loc){
var lower_loc = loc.replace(" ","_");
lower_loc = lower_loc.toLowerCase();
var this_field_index = parseInt($('.map_chooser').val());
var html = '<table class="tip_table">';
html +='<tr>';
html +='<td colspan="2">';
html += '<div class="label_loc">';
if (map_init.hasOwnProperty('join_field_label')) {
html += map_init.join_field_label + ': ';
}
html += loc + '</div>';
html += '</td></tr>';
var rows = fields.length;
for (var row = 0; row<rows; row++) {
html+='<tr';
if (isEven(row)) {
html += ' class="even';
}
else {
html += ' class="odd';
}
if (row === this_field_index) {
html += " field_highlight";
}
html+='">';
html +='<td>';
html +=fields[row].label;
html +='</td>';
html +='<td>';
var data = locDataLookup(loc,fields[row]);
if (fields[row].hasOwnProperty('format')===true) {
data = data.format((fields[row].format));
}
html += data;
html += '</td></tr>';
}
html += '</table>';
Tip(html);
}
// ------------------------------------------------------------------------------------
function buildLegend () {
// global objects: map_init, field
var html='<table class="map_legend">';
html += '<tr><td colspan="2"><div class="legend_heading">' + field.label + '</div></td></tr>';
for (var index=0; index<field.steps; index++) {
if (field.type==="unique_value") {
if (isNumeric(field.values[index])) {
var desc = parseFloat(field.values[index]);
desc = desc.format(field.format);
}
else {
desc = field.values[index].restoreSpacesProperCase();
}
}
else {
var lower_range = field.boundaries[index]['lower'].format(field.format);
var upper_range = field.boundaries[index]['upper'].format(field.format);
var desc = lower_range + ' to ' + upper_range;
}
mouseOverStr = 'onMouseOver="highlight_areas(\'step_' + index + '\');"';
mouseOverStr += ' onMouseOut="unhighlight_areas(\'step_' + index + '\');"';
html +='<tr>';
html +='<td><div class="color_box" ' + mouseOverStr + ' style="background:#' + getPaletteColor(index) + ';"> </div></td>';
html +='<td>' + desc + '</td>';
html +='</tr>';
}
html +='</table>';
$('.legend_div').html(html);
return;
}
// ------------------------------------------------------------------------------------
function initFields () {
// if the field does not have a label property, add one using the field's name
// global objects: map_init, fields
if (typeof(datasource) === 'undefined') {
alert ("Error: Data source has not been defined.");
return;
}
if (typeof(fields) === 'undefined') {
// create global array called fields
fields = new Array();
var props = getProperties(datasource[0]);
for (var index=0; index<props.length; index++) {
fields[index] = {"name": props[index]};
}
}
for (var index=0; index< fields.length; index++) {
var this_field = fields[index];
if (!this_field.hasOwnProperty('label')) {
this_field.label = this_field.name;
}
// multiplier property is used for percentages that need to be multiplied by 100
// if found, it goes through each object inside the datasource and multiplies
// the given field by the multiplier
if (this_field.hasOwnProperty('multiplier')) {
var multiplier = this_field.multiplier;
$.each(datasource, function(x, value) {
value[this_field.name] *= multiplier;
});
}
if (!this_field.hasOwnProperty('mapable')) {
this_field.mapable = isNumeric(datasource[0][this_field.name]) ;
}
}
}
// ------------------------------------------------------------------------------------
function setStepBounds () {
// global objects: map_init, field
// if the file type is unique_value, the number of steps = the number of unique values in the dataset
// if we haven't done so already, get all the unique values, sorted, and save them to be used to build
// the legend - buildLegend(), and then return. No need to determine boundaries.
if (field.hasOwnProperty('type')) {
if (field.type === 'unique_value') {
if (!field.hasOwnProperty('values')) {
field.values = extractPropertyArray(map_init.datasource,field.name).getUnique().sort(function(a,b){return a - b});
field.steps = field.values.length;
return;
}
}
}
field.type = 'range';
// Find out how many steps.
// if the boundaries have already been set because we've already been here
// or the user has defined the boundaries in the field definition, just set the steps
// and return. No need to classify.
if (field.hasOwnProperty('boundaries')) {
field.steps = field.boundaries.length;
return;
}
// no boundaries set yet
// if a palette has been set, set steps to palette length
if (field.hasOwnProperty('palette')){
field.steps = field.palette.length;
}
// if we haven't set steps by now, or the number of steps were not defined by user
// in field definition object, use the default steps value
if (!field.hasOwnProperty('steps')){
field.steps = map_init.default_steps;
}
// now classify
if (!field.hasOwnProperty('max_value')) {
field.max_value = Math.ceil((getMaxPropValueInArray(map_init.datasource, field.name))) + 1; // adding 1 ensures that target will be < max
}
if (!field.hasOwnProperty('min_value')) {
field.min_value = Math.floor((getMinPropValueInArray(map_init.datasource, field.name)));
}
var max = field.max_value;
var min = field.min_value;
var range = max-min;
if (field.hasOwnProperty('interval')) {
field.steps = Math.ceil(range/field.interval);
}
else {
field.interval = range/field.steps;
}
var steps = field.steps;
var interval = field.interval;
field.boundaries = [];
var lower_bound = min;
for (var index=0; index<steps; index++) {
field.boundaries[index]= {'lower':lower_bound, 'upper':lower_bound + interval};
lower_bound += interval;
}
return field;
}
// ------------------------------------------------------------------------------------
function highlight_areas(e) {
$('area').each (function(i) {
var this_class = $(this).attr('class');
this_class=this_class.replace("fillOpacity:1","fillOpacity:0.1");
$(this).attr('class', this_class);
});
$('area.' + e).each (function(i) {
var this_class = $(this).attr('class');
this_class=this_class.replace("fillOpacity:0.1","fillOpacity:1");
$(this).attr('class', this_class);
});
$('.map').maphilight();
}
function unhighlight_areas(e) {
$('area').each (function(i) {
var this_class = $(this).attr('class');
this_class=this_class.replace("fillOpacity:0.1","fillOpacity:1");
$(this).attr('class', this_class);
});
$('.map').maphilight();
}
// ------------------------------------------------------------------------------------
function lookupVal(location) {
var datasource = map_init.datasource;
var join_field = map_init.join_field;
if (field.hasOwnProperty('map_display_field')===true) {
var property = field.map_display_field;
}
else {
var property = field.name;
}
for (var x=0;x<datasource.length;x++) {
if (datasource[x][join_field] === location) {
return datasource[x][property];
}
}
return "?";
}