-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.js
248 lines (221 loc) · 9.19 KB
/
javascript.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
//Javascript file
//LEAFLET STAMEN BASEMAP
// var map = new L.map('map').setView([40,-98], 4);
// var layer = new L.StamenTileLayer("toner-lite");
// map.addLayer(layer);
// CARTODB EASY MAP
// get the viz.json url from the CartoDB Editor
// - click on visualize
// - create new visualization
// - make visualization public
// - click on publish
// - go to API tab
// window.onload = function() {
// cartodb.createVis('map', 'http://mtaylorlong.cartodb.com/api/v2/viz/81f31a2a-8021-11e4-8fc1-0e4fddd5de28/viz.json', {
// cartodb_logo: false
// });
// }
// Set Variables
var break1 = '0';
var break2 = '5';
var break3 = '10';
var yrbreak1 = '12';
var yrbreak2 = '60';
var yrbreak3 = '120';
// var brkcol1 = '#253494'; //dark blue
// var brkcol2 = '#41B6C4'; //turquoise
// var brkcol3 = '#FFFFCC'; //yellow
var brkcol1 = 'rgba(153,0,0,0.8)'; //red
var brkcol2 = 'rgba(204,183,0,0.8)'; //yellow
var brkcol3 = 'rgba(0,153,0,0.25)'; //green translucent
cssOrig = '#county_merge {polygon-fill:'+brkcol3+';'+
'polygon-opacity: 0.8;'+
'line-color: #FFFFFF;'+
'line-width: 0.5;'+
'line-opacity: 0.5;}'+
'#county_merge [yr_obstotal <='+yrbreak3+'] {'+
'polygon-fill:'+brkcol3+';}'+
'#county_merge [yr_obstotal <='+yrbreak2+'] {'+
'polygon-fill:'+brkcol2+';}'+
'#county_merge [yr_obstotal <='+yrbreak1+'] {'+
'polygon-fill:'+brkcol1+';}';
// A horribly cumbersome way to set up my infowindow template
var myInfoTemplateX = [
'<div class="cartodb-popup">',
'<a href="#close" class="cartodb-popup-close-button close">x</a>',
'<div class="cartodb-popup-content-wrapper">',
'<div class="cartodb-popup-header">',
'<h2>{{content.data.name}} Co., {{content.data.state}}</h2>',
'</div>',
'<div class="cartodb-popup-content">',
'<p style="font-size:1.5em"><strong>{{content.data.yr_obstotal}}</strong> lists in 2013</p>',
'<p style="font-size:1.5em">Lists by month:</p>',
'<canvas id="myChart" width="190" height="110">',
'<script>',
'var obsdata = {',
'labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],',
'datasets: [',
'{ label: "My First dataset",',
'fillColor: "rgba(0,0,0,1)",',
'highlightFill: "rgba(0,0,0,0.5)",',
'data: [',
'{{jan_obstotal}}, ',
'{{feb_obstotal}}, ',
'{{mar_obstotal}}, ',
'{{apr_obstotal}}, ',
'{{may_obstotal}}, ',
'{{jun_obstotal}}, ',
'{{jul_obstotal}}, ',
'{{aug_obstotal}}, ',
'{{sep_obstotal}}, ',
'{{oct_obstotal}}, ',
'{{nov_obstotal}}, ',
'{{dec_obstotal}}, ',
']',
'}]};',
'var ctx = document.getElementById("myChart").getContext("2d");',
'var myNewChart = new Chart(ctx).Bar(obsdata, {',
'showScale: true,',
'scaleShowLabels: false,',
'barShowStroke: false,',
'scaleShowGridLines: false,',
'barValueSpacing: 1',
'});',
'</scr'+'ipt>',
'</canvas>',
'</div>',
'</div>',
'<div class="cartodb-popup-tip-container"></div>',
'</div>'
].join('');
// Second try at simpler info template
var myInfoTemplate = '\
<div class="cartodb-popup">\
<a href="#close" class="cartodb-popup-close-button close">x</a>\
<div class="cartodb-popup-content-wrapper">\
<div class="cartodb-popup-header">\
<h2>{{content.data.name}} Co., {{content.data.state}}</h2>\
</div>\
<div class="cartodb-popup-content">\
<p style="font-size:1.5em"><strong>{{content.data.yr_obstotal}}</strong> lists in 2013</p>\
<p style="font-size:1.5em">Lists by month:</p>\
<canvas id="myChart" width="190" height="110">\
<script>\
var obsdata = {\
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],\
datasets: [\
{ label: "My First dataset",\
fillColor: "rgba(0,0,0,1)",\
highlightFill: "rgba(0,0,0,0.5)",\
data: [\
{{jan_obstotal}},\
{{feb_obstotal}},\
{{mar_obstotal}},\
{{apr_obstotal}},\
{{may_obstotal}},\
{{jun_obstotal}},\
{{jul_obstotal}},\
{{aug_obstotal}},\
{{sep_obstotal}},\
{{oct_obstotal}},\
{{nov_obstotal}},\
{{dec_obstotal}},\
]\
}]};\
var ctx = document.getElementById("myChart").getContext("2d");\
var myNewChart = new Chart(ctx).Bar(obsdata, {\
showScale: true,\
scaleShowLabels: false,\
barShowStroke: false,\
scaleShowGridLines: false,\
barValueSpacing: 1\
});\
</script>\
</canvas>\
</div>\
</div>\
<div class="cartodb-popup-tip-container"></div>\
</div>';
console.log(myInfoTemplate);
console.log(myInfoTemplateX);
// create layer selector
function createSelector(layer) {
//var sql = new cartodb.SQL({ user: 'mtaylorlong' });
var $options = $('#month-filter li');
$options.click(function(e) {
// get the area of the selected layer
var $li = $(e.target);
var month = $li.attr('data');
// deselect all and select the clicked one
$options.removeClass('selected');
$li.addClass('selected');
// create query based on data from the layer
//var query = "SELECT * FROM county_merge";
var css = '';
if(month !== 'yr') {
css = '#county_merge {polygon-fill:'+brkcol3+';'+
'polygon-opacity: 0.8;'+
'line-color: #FFFFFF;'+
'line-width: 0.5;'+
'line-opacity: 0.5;}'+
'#county_merge ['+month+'_obstotal <='+break3+'] {'+
'polygon-fill:'+brkcol3+';}'+
'#county_merge ['+month+'_obstotal <='+break2+'] {'+
'polygon-fill:'+brkcol2+';}'+
'#county_merge ['+month+'_obstotal <='+break1+'] {'+
'polygon-fill:'+brkcol1+';}'
} else {
css = cssOrig
};
// Reset the CartoCSS on the layer
layer.setCartoCSS(css);
});
};
//----------------------
// Create main function
//----------------------
function main() {
cartodb.createVis('map', 'http://mtaylorlong.cartodb.com/api/v2/viz/67031608-c044-11e4-bbeb-0e0c41326911/viz.json', {
center_lat: 40,
center_lon: -98,
zoom: 4
})
.done(function(vis, layers) {
// layer 0 is the base layer, layer 1 is cartodb layer
var subLayer = layers[1].getSubLayer(0);
var sql = new cartodb.SQL({ user: 'mtaylorlong' });
var query = "SELECT * FROM county_merge WHERE state != 'AK' AND name IS NOT NULL";
subLayer.setSQL(query);
subLayer.setCartoCSS(cssOrig);
subLayer.infowindow.set('template', myInfoTemplate);
createSelector(subLayer);
})
.error(function(err) {
console.log(err);
});
};
// Execute Main function
window.onload = main;
//-----------------------------------------------------
// create a layer with 1 sublayer
// function main() {
// }
// window.onload = main;
// cartodb.createLayer(map, {
// user_name: 'mtaylorlong',
// type: 'cartodb',
// sublayers: [{
// sql: "SELECT * FROM county",
// cartocss: '#county {marker-fill: #F0F0F0;}'
// }]
// })
// .addTo(map) // add the layer to our map which already contains 1 sublayer
// .done(function(layer) {
// // create and add a new sublayer
// layer.createSubLayer({
// sql: "SELECT * FROM county limit 200",
// cartocss: '#county {marker-fill: #F0F0F0;}'
// });
// // change the query for the first layer
// // layer.getSubLayer(0).setSQL("SELECT * FROM #county limit 10");
// });