@andyreagan/hedotools 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,353 @@
1
+ // on call as a module
2
+ // in the test function, can set the function that gets called
3
+ // when the lens is moved
4
+ // full flexibility
5
+ hedotools.lensoncall = function() {
6
+ var test = function(extent1) {
7
+ console.log("set on load (works for maps.html)");
8
+ // reset
9
+ for (var j=0; j<allData.length; j++) {
10
+ for (var i=0; i<allData[j].rawFreq.length; i++) {
11
+ var include = true;
12
+ // check if in removed word list
13
+ for (var k=0; k<ignoreWords.length; k++) {
14
+ if (ignoreWords[k] == words[i]) {
15
+ include = false;
16
+ //console.log("ignored "+ignoreWords[k]);
17
+ }
18
+ }
19
+ // check if underneath lens cover
20
+ if (lens[i] >= extent1[0] && lens[i] <= extent1[1]) {
21
+ include = false;
22
+ }
23
+ // include it, or set to 0
24
+ if (include) {
25
+ allData[j].freq[i] = allData[j].rawFreq[i];
26
+ }
27
+ else { allData[j].freq[i] = 0; }
28
+
29
+ }
30
+ }
31
+ hedotools.computeHapps.go();
32
+ hedotools.map.setfigure(d3.select('#map01')).setdata(geoJson).plot();
33
+ if (shiftRef !== shiftComp) {
34
+ hedotools.shifter.shift(allData[shiftRef].freq,allData[shiftComp].freq,lens,words);
35
+ var happysad = hedotools.shifter._compH() > hedotools.shifter._refH() ? "happier" : "less happy";
36
+ hedotools.shifter.setfigure(d3.select('#shift01')).setText(["Why "+allData[shiftComp].name+" is "+happysad+" than "+allData[shiftRef].name+":"]).plot();
37
+ }
38
+ }
39
+ var opublic = { test: test, };
40
+ return opublic;
41
+ }();
42
+
43
+ hedotools.lens = function() {
44
+
45
+ // for now, keep track of which page we're in
46
+ // since they're all a bit different
47
+ var page = "geo";
48
+
49
+ var encoder = hedotools.urllib.encoder().varname("lens"); //.varval(lensExtent);
50
+ var decoder = hedotools.urllib.decoder().varname("lens").varresult([4,6]); //.varval(lensExtent);
51
+
52
+ var figure;
53
+ var lens;
54
+ var margin = {top: 0, right: 55, bottom: 0, left: 0};
55
+ var figwidth;
56
+ var figheight = 100 - margin.top - margin.bottom;
57
+ var width;
58
+ var height = .875*figheight-5;
59
+ var leftOffsetStatic;
60
+
61
+ var grabwidth = function() {
62
+ figwidth = parseInt(figure.style('width')) - margin.left - margin.right;
63
+ width = .875*figwidth-5;
64
+ leftOffsetStatic = 0.125*figwidth;
65
+ }
66
+
67
+ var setfigure = function(_) {
68
+ console.log("setting figure");
69
+ figure = _;
70
+ grabwidth();
71
+ return hedotools.lens;
72
+ }
73
+
74
+ var setdata = function(_) {
75
+ lens = _;
76
+ return hedotools.lens;
77
+ }
78
+
79
+ lensExtent = decoder().cached;
80
+
81
+ var plot = function () {
82
+
83
+ if (figwidth > 10) {
84
+
85
+ // remove an old figure if it exists
86
+ figure.selectAll(".canvas").remove();
87
+
88
+ var canvas = figure.append("svg")
89
+ .attr("width",figwidth)
90
+ .attr("height",figheight)
91
+ .attr("id","lenssvg")
92
+ .attr("class","canvas");
93
+
94
+
95
+ // create the x and y axis
96
+ var x = d3.scale.linear()
97
+ .domain([1.00,9.00])
98
+ // .domain(d3.extent(lens))
99
+ .range([0,width]);
100
+
101
+ // use d3.layout http://bl.ocks.org/mbostock/3048450
102
+ var data = d3.layout.histogram()
103
+ .bins(x.ticks(65))
104
+ (lens);
105
+
106
+ // linear scale function
107
+ var y = d3.scale.linear()
108
+ .domain([0,d3.max(data,function(d) { return d.y; } )])
109
+ .range([height, 0]);
110
+
111
+ // create the axes themselves
112
+ var axes = canvas.append("g")
113
+ .attr("transform", "translate(" + (0.125 * figwidth) + "," +
114
+ ((1 - 0.125 - 0.875) * figheight) + ")")
115
+ .attr("width", width)
116
+ .attr("height", height)
117
+ .attr("class", "main");
118
+
119
+ // create the axes background
120
+ var bgrect = axes.append("svg:rect")
121
+ .attr("width", width)
122
+ .attr("height", height)
123
+ .attr("class", "bg")
124
+ .style({'stroke-width':'2','stroke':'rgb(0,0,0)'})
125
+ .attr("fill", "#FFFFF0");
126
+
127
+ // axes creation functions
128
+ var create_xAxis = function() {
129
+ return d3.svg.axis()
130
+ .scale(x)
131
+ .ticks(9)
132
+ .orient("bottom"); }
133
+
134
+ // axis creation function
135
+ var create_yAxis = function() {
136
+ return d3.svg.axis()
137
+ .ticks(3)
138
+ .scale(y) //linear scale function
139
+ .orient("left"); }
140
+
141
+ // draw the axes
142
+ var yAxis = create_yAxis()
143
+ .innerTickSize(6)
144
+ .outerTickSize(0);
145
+
146
+ axes.append("g")
147
+ .attr("class", "top")
148
+ .attr("transform", "translate(0,0)")
149
+ .attr("font-size", "12.0px")
150
+ .call(yAxis);
151
+
152
+ var xAxis = create_xAxis()
153
+ .innerTickSize(6)
154
+ .outerTickSize(0);
155
+
156
+ axes.append("g")
157
+ .attr("class", "x axis ")
158
+ .attr("font-size", "12.0px")
159
+ .attr("transform", "translate(0," + (height) + ")")
160
+ .call(xAxis);
161
+
162
+ d3.selectAll(".tick line").style({'stroke':'black'});
163
+
164
+ // create the clip boundary
165
+ var clip = axes.append("svg:clipPath")
166
+ .attr("id","clip")
167
+ .append("svg:rect")
168
+ .attr("x",0)
169
+ .attr("y",80)
170
+ .attr("width",width)
171
+ .attr("height",height-80);
172
+
173
+ var unclipped_axes = axes;
174
+
175
+ //axes = axes.append("g")
176
+ //.attr("clip-path","url(#clip)");
177
+
178
+ canvas.append("text")
179
+ .text("Num Words")
180
+ .attr("class","axes-text")
181
+ .attr("x",(figwidth-width)/4)
182
+ .attr("y",figheight/2+30)
183
+ .attr("font-size", "12.0px")
184
+ .attr("fill", "#000000")
185
+ .attr("transform", "rotate(-90.0," + (figwidth-width)/4 + "," + (figheight/2+30) + ")");
186
+
187
+ // var xlabel = canvas.append("text")
188
+ // .text("Word score")
189
+ // .attr("class","axes-text")
190
+ // .attr("x",width/2+(figwidth-width)/2)
191
+ // .attr("y",figheight)
192
+ // .attr("font-size", "12.0px")
193
+ // .attr("fill", "#000000")
194
+ // .attr("style", "text-anchor: middle;");
195
+
196
+ var lensMean = d3.mean(lens);
197
+
198
+ var bar = axes.selectAll(".distrect")
199
+ .data(data)
200
+ .enter()
201
+ .append("g")
202
+ .attr("class","distrect")
203
+ .attr("fill",function(d,i) { if (d.x > lensMean) {return "#D3D3D3";} else { return "#D3D3D3";}})
204
+ .attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
205
+
206
+ var mainrect = bar.append("rect")
207
+ .attr("x", 1)
208
+ .attr("width", x(data[0].dx+1)-2 )
209
+ .attr("height", function(d) { return height - y(d.y); });
210
+
211
+ var line = d3.svg.line()
212
+ .x(function(d,i) { return x(d.x); })
213
+ .y(function(d) { return y(d.y); })
214
+ .interpolate("linear");
215
+
216
+ var mainline = axes.append("path")
217
+ .datum(data)
218
+ .attr("class", "line")
219
+ .attr("d", line)
220
+ .attr("stroke","black")
221
+ .attr("stroke-width",3)
222
+ .attr("fill","none");
223
+
224
+ //console.log(x(d3.min(lens)));
225
+
226
+ var brushX = d3.scale.linear()
227
+ .domain([1,9])
228
+ // .domain(d3.extent(lens))
229
+ .range([figwidth*.125,width+figwidth*.125]);
230
+
231
+
232
+
233
+ function brushended() {
234
+ if (!d3.event.sourceEvent) return;
235
+ var extent0 = brush.extent(),
236
+ extent1 = extent0; // should round it to bins
237
+
238
+ onredrawfunction();
239
+
240
+ // window.stopVals = extent1;
241
+ // console.log(extent1);
242
+ if ((extent1[0] !== lensExtent[0]) || (extent1[1] !== lensExtent[1]))
243
+ {
244
+
245
+ lensExtent = [Math.round(extent1[0]*4)/4,Math.round(extent1[1]*4)/4];
246
+ hedotools.lensoncall.test(extent1);
247
+ }
248
+
249
+ d3.select(this).transition()
250
+ .call(brush.extent(lensExtent))
251
+ .call(brush.event);
252
+
253
+ encoder.varval(lensExtent);
254
+ }
255
+
256
+ var brush = d3.svg.brush()
257
+ .x(brushX)
258
+ .extent(lensExtent)
259
+ .on("brushend",brushended);
260
+
261
+ var gBrush = canvas.append("g")
262
+ .attr("class","lensbrush")
263
+ .call(brush)
264
+ .call(brush.event);
265
+
266
+ gBrush.selectAll("rect")
267
+ .attr("height",height)
268
+ .attr("y",0)
269
+ .style({'stroke-width':'2','stroke':'rgb(100,100,100)','opacity': 0.95})
270
+ .attr("fill", "#FCFCFC");
271
+
272
+ //console.log(lensExtent);
273
+
274
+ function resizelens() {
275
+ figwidth = parseInt(d3.select("#lens01").style('width')) - margin.left - margin.right,
276
+ width = .775*figwidth;
277
+
278
+ canvas.attr("width",figwidth);
279
+
280
+ x.range([0,width]);
281
+ bgrect.attr("width",width);
282
+ //axes.attr("transform", "translate(" + (0.125 * figwidth) + "," +
283
+ // ((1 - 0.125 - 0.775) * figheight) + ")");
284
+
285
+ mainline.attr("d",line);
286
+
287
+ //create_xAxis.scale(x);
288
+ //xAxisHandle.call(xAxis);
289
+ canvas.select(".x.axis").call(xAxis);
290
+
291
+ canvas.selectAll(".distrect").attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
292
+
293
+ // xlabel.attr("x",(leftOffsetStatic+width/2));
294
+
295
+ d3.selectAll(".tick line").style({'stroke':'black'});
296
+
297
+ // //brushX.range([figwidth*.125,width+figwidth*.125]);
298
+ brushX.range([leftOffsetStatic,leftOffsetStatic+width]);
299
+ brush.x(brushX);
300
+ d3.select(".lensbrush") //.transition()
301
+ .call(brush.extent(lensExtent))
302
+ .call(brush.event);
303
+ //brushing();
304
+ //brush.event();
305
+ };
306
+
307
+ d3.select(window).on("resize.selectlens",resizelens);
308
+
309
+ // var buttongroup = figure.append("div").attr({"class":"btn-group-vertical",});
310
+ //buttongroup.html('<button type="button" class="btn btn-default">Button</button><button type="button" class="btn btn-default">Button</button><div class="btn-group"><button id="btnGroupVerticalDrop1" type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">Dropdown<span class="caret"></span> </button> <ul class="dropdown-menu" role="menu" aria-labelledby="btnGroupVerticalDrop1"> <li><a href="#">Dropdown link</a></li> <li><a href="#">Dropdown link</a></li> </ul></div> <button type="button" class="btn btn-default">Button</button>'
311
+
312
+ figure.selectAll("div.btn-group-vertical").remove();
313
+ var buttongroup = figure.append("div").attr({"class":"btn-group-vertical pull-right",})
314
+ // var defaults = [[4,6],[3,7],[3,9],[1,7],[5,5]];
315
+ var defaults = [[4,6],[3,7],[5,5]];
316
+ // var defaultnames = ["Default","Wide","Sad","Happy","None"];
317
+ var defaultnames = ["Default","Wide","None"];
318
+ buttongroup.selectAll("button").data(defaults).enter()
319
+ .append("button")
320
+ .attr({"type":"button",
321
+ "class": function(d,i) { return "btn btn-default btn-xs "+defaultnames[i]; },})
322
+ .html(function(d,i) { return defaultnames[i]; })
323
+ .on("click",function(d,i) {
324
+ figure.selectAll("button").attr("class","btn btn-default btn-xs");
325
+ d3.select(this).attr("class","btn btn-primary btn-xs");
326
+ d3.select(".lensbrush") //.transition()
327
+ .call(brush.extent(d))
328
+ .call(brush.event);
329
+ });
330
+ // initially check if any are matched
331
+ console.log(lensExtent);
332
+ for (var i=0; i<defaults.length; i++) {
333
+ if (defaults[i][0] === parseFloat(lensExtent[0]) && defaults[i][1] === parseFloat(lensExtent[1])) {
334
+ // make it active
335
+ buttongroup.select("button."+defaultnames[i]).attr("class","btn btn-primary btn-xs");
336
+ }
337
+ }
338
+
339
+ }; // if figwidth > 10
340
+ }
341
+
342
+ var onredrawfunction = function() {
343
+ console.log("I got called");
344
+ }
345
+
346
+ var opublic = { setfigure: setfigure,
347
+ setdata: setdata,
348
+ plot: plot,
349
+ onredrawfunction: onredrawfunction,
350
+ };
351
+
352
+ return opublic;
353
+ };