This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
choropleth.js
193 lines (182 loc) · 6.87 KB
/
choropleth.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
(function($) {
Drupal.behaviors.choropleth = {
attach: function(context) {
var choroDataColumn = [Drupal.settings.choropleth.choropleth_data_column] || [];
var choroUnitsOfMeasureColumn = [Drupal.settings.choropleth.choropleth_unit_of_measure][0] || [];
var choropleth = Drupal.settings.choropleth.choropleth || '';
var colorScale = Drupal.settings.choropleth.choropleth_color_scale || ['#FFEDA0', '#FEB24C', '#E31A1C', '#800026'];
var breakpoints = Drupal.settings.choropleth.choropleth_breakpoints || [];
var map_type = false;
var map_label = '';
var polygons;
var location_default = [];
location_default.lat = 0;
location_default.lon = 0;
location_default.zoom = 1;
// Stop if choropleth isn't selected as a view to display.
if (!choropleth.length) {
return;
}
if (breakpoints.length) {
var breakpoints = breakpoints.split(', ');
var item = {};
var items = [];
for (var i = 0; i < breakpoints.length; i++) {
items[i] = breakpoints[i];
}
breakpoints = items;
}
/**
* Sets the maptype for a map
* @param {string} type
* The type of the map attempting to be set.
* @param {Boolean} force
* A flag to allow a map type to override an existing type.
* @returns {string|Drupal.behaviors.choropleth.attach.map_type}
* The type of map established.
*/
function set_map_type(type, force) {
var force = force || false;
var type = type || false;
// Determine if type has already been set or if force changed.
if (!map_type || force) {
map_type = type;
}
return map_type;
}
/**
* Sets the map label for a map
* @param {string} label
* The label of the map attempting to be set.
* @param {Boolean} force
* A flag to allow a map label to override an existing label.
* @returns {string|Drupal.behaviors.choropleth.attach.map_label}
* The type of map established.
*/
function set_map_label(label, force) {
var force = force || false;
var label = label || false;
// Determine if label has already been set or if force changed.
if (!map_label || force) {
map_label = label;
}
return map_label;
}
// This event is trigged when the recline module creates
// a new dataExplorer object.
$(document).bind('createDataExplorer', function(event) {
// Check to see if choropleth is enabled.
if (typeof(Drupal.settings.choropleth.enable) != "undefined" && Drupal.settings.choropleth.enable) {
var explorer = window.dataExplorer;
var model = explorer.model;
var fields = model.toTemplateJSON().fields;
$.each(fields, function(i) {
var field_id = fields[i].id.toLowerCase();
// Process special case fields. First one set, wins.
switch (field_id) {
case 'state':
case 'states':
// Process map as states.
set_map_type('states');
set_map_label('State');
polygons = Drupal.settings.choropleth.statesData;
// Set to contigous USA.
location_default.lat = 51.25;
location_default.lon = -110;
location_default.zoom = 3;
break;
case 'country':
// Process map as World.
// Functionality not built yet. Needs country polygons.
set_map_type('world');
set_map_label('Country');
break;
case 'county':
// Process map as county.
// Functionality not built yet. Needs county polygons and default location fitbounds method.
set_map_type('county');
set_map_label('County');
break;
case 'city':
// Process map as a state - functionality not built yet.
// Functionality not built yet. Needs city polygons and default location fitbounds method.
set_map_type('state');
set_map_label('City');
break;
case 'region':
// Process map as region (loosely typed)- functionality not built yet.
// Functionality not built yet. Needs custom polygons method and default location fitbounds method.
set_map_type('region');
set_map_label('Region');
break;
default:
// Nothing special so keep going.
}
});
if (map_type) {
// If recline dataset (dkan resource) presents a field that matches a case above,
// we recreate the whole multiview and its current views
// attaching also the Choropleth map View.
var view = new recline.View.ChoroplethMap({
polygons: polygons,
model: model,
map_type: map_type,
label_to_map: map_label,
location_default: location_default,
selectable_fields: choroDataColumn,
unit_of_measure: choroUnitsOfMeasureColumn,
breakpoints: breakpoints,
base_color: colorScale,
});
current_views = explorer.options.views;
new_views = [];
// Choropleth goes first ...
new_views.push({
id: 'choroplethmap',
label: map_label + ' by ' + map_label,
view: view
});
// ... then current views
$.each(current_views, function (i, v) {
var cls = null;
switch(v.id) {
case 'grid':
cls = recline.View.SlickGrid;
break;
case 'graph':
cls = recline.View.Graph;
break;
case 'map':
cls = recline.View.Map;
break;
}
if (cls) {
new_views.push({
id: v.id,
label: v.label,
view: new cls({
model: model
})
});
}
});
// Grab state.
var state = explorer.options.state;
// Remove dom elements.
explorer.remove();
window.explorerDiv.html('');
// Recreate the whole thing.
var $el = $('<div />');
$el.appendTo(window.explorerDiv);
explorer = new recline.View.MultiView({
model: model,
el: $el,
state: state,
views: new_views
});
}
}
});
}
};
})(jQuery);