@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,348 @@
1
+ hedotools.barchartoncall = function() {
2
+ var test = function(d,i) {
3
+ // console.log(i);
4
+ i = indices[i];
5
+ if (stateSelType) {
6
+ shiftComp = i;
7
+ d3.select(".complabel").text(allData[i].name);
8
+ compencoder.varval(allData[i].name);
9
+ }
10
+ else {
11
+ shiftRef = i;
12
+ d3.select(".reflabel").text(allData[i].name);
13
+ refencoder.varval(allData[i].name);
14
+ }
15
+
16
+ if (shiftRef !== shiftComp) {
17
+ hedotools.shifter.shift(allData[shiftRef].freq,allData[shiftComp].freq,lens,words);
18
+ var happysad = hedotools.shifter._compH() > hedotools.shifter._refH() ? "happier" : "less happy";
19
+ hedotools.shifter.setfigure(d3.select('#shift01')).setText(["Why "+allData[shiftComp].name+" is "+happysad+" than "+allData[shiftRef].name+":"]).plot();
20
+ }
21
+ }
22
+ var opublic = { test: test,
23
+ };
24
+ return opublic;
25
+ }();
26
+
27
+ // make the plot
28
+ hedotools.barchart = function() {
29
+ var figure;
30
+
31
+ var setfigure = function(_) {
32
+ // console.log("setting figure");
33
+ figure = _;
34
+ return hedotools.barchart;
35
+ }
36
+
37
+ var xlabeltext = "Happiness difference from US as a whole";
38
+ var _xlabeltext = function(_) {
39
+ if (!arguments.length) return xlabeltext;
40
+ xlabeltext = _;
41
+ return hedotools.barchart;
42
+ }
43
+
44
+ var data;
45
+ var datanames;
46
+ var geodata;
47
+
48
+ var setdata = function(a,b) {
49
+ data = a;
50
+ geodata = b;
51
+ datanames = Array(geodata.length);
52
+ for (var i=0; i<geodata.length; i++) {
53
+ datanames[i] = geodata[i].properties.name;
54
+ }
55
+ return hedotools.barchart;
56
+ }
57
+
58
+ var _data = function(_) {
59
+ if (!arguments.length) return data;
60
+ data = _;
61
+ return hedotools.barchart;
62
+ }
63
+
64
+ var _datanames = function(_) {
65
+ if (!arguments.length) return datanames;
66
+ datanames = _;
67
+ return hedotools.barchart;
68
+ }
69
+
70
+ var figheight = 730;
71
+ var _figheight = function(_) {
72
+ if (!arguments.length) return figheight;
73
+ figheight = _;
74
+ return hedotools.barchart;
75
+ }
76
+
77
+ var manualTicks = [];
78
+ var _manualTicks = function(_) {
79
+ if (!arguments.length) return manualTicks;
80
+ manualTicks = _;
81
+ return hedotools.barchart;
82
+ }
83
+
84
+ var sortedStates;
85
+ var getSorted = function(_) {
86
+ if (!arguments.length) return sortedStates.map(function(d) { return d[2]; });
87
+ if (_) {
88
+ return sortedStates.map(function(d,i) { return (i+1)+". "+d[2]; });
89
+ }
90
+ else {
91
+ return sortedStates.map(function(d) { return d[2]; });
92
+ }
93
+ return hedotools.barchart;
94
+ }
95
+
96
+
97
+ var plot = function() {
98
+ /* plot the bar chart
99
+
100
+ -take a d3 selection, and draw the bar chart SVG on it
101
+ -requires the magnitude for each state, and the geojson
102
+ with the names
103
+
104
+ */
105
+ var margin = {top: 0, right: 0, bottom: 0, left: 0};
106
+ var axeslabelmargin = {top: 0, right: 0, bottom: 50, left: 0};
107
+ var figwidth = parseInt(figure.style('width')) - margin.left - margin.right;
108
+ // aspectRatio = 1.9,
109
+ // figheight = parseInt(d3.select('#barChart').style('width'))*aspectRatio - margin.top - margin.bottom,
110
+ var width = figwidth-axeslabelmargin.left-axeslabelmargin.right;
111
+ var height = figheight-axeslabelmargin.top-axeslabelmargin.bottom;
112
+ var figcenter = width/2;
113
+ var leftOffsetStatic = axeslabelmargin.left;
114
+
115
+ // do the sorting
116
+ var indices = Array(data.length);
117
+ for (var i = 0; i < data.length; i++) { indices[i] = i; }
118
+ // sort by abs magnitude
119
+ // indices.sort(function(a,b) { return Math.abs(data[a]) < Math.abs(data[b]) ? 1 : Math.abs(data[a]) > Math.abs(data[b]) ? -1 : 0; });
120
+ // sort by magnitude, parity preserving
121
+ indices.sort(function(a,b) { return data[a] < data[b] ? 1 : data[a] > data[b] ? -1 : 0; });
122
+ sortedStates = Array(data.length);
123
+ for (var i = 0; i < data.length; i++) { sortedStates[i] = [i,indices[i],datanames[indices[i]],data[indices[i]]]; }
124
+ // console.log(sortedStates);
125
+
126
+ // remove an old figure if it exists
127
+ figure.select(".canvas").remove();
128
+
129
+ var canvas = figure.append("svg")
130
+ .attr("width",figwidth)
131
+ .attr("height",figheight)
132
+ .attr("class","canvas")
133
+ .attr("id","barchartsvg");
134
+
135
+ // x scale, maps all the data to
136
+ var absDataMax = d3.max([d3.max(data),-d3.min(data)]);
137
+ var x = d3.scale.linear()
138
+ .domain([-absDataMax,absDataMax])
139
+ .range([5,width-10]);
140
+
141
+ // linear scale function
142
+ var y = d3.scale.linear()
143
+ .domain([data.length,1])
144
+ .range([height-20, 5]);
145
+
146
+ // // zoom object for the axes
147
+ // var zoom = d3.behavior.zoom()
148
+ // .y(y) // pass linear scale function
149
+ // // .translate([10,10])
150
+ // .scaleExtent([1,1])
151
+ // .on("zoom",zoomed);
152
+
153
+ // create the axes themselves
154
+ var axes = canvas.append("g")
155
+ .attr("transform", "translate(" + (axeslabelmargin.left) + "," +
156
+ (axeslabelmargin.top) + ")")
157
+ .attr("width", width)
158
+ .attr("height", height)
159
+ .attr("class", "main");
160
+ // .call(zoom);
161
+
162
+ // create the axes background
163
+ // var bgrect = axes.append("svg:rect")
164
+ // .attr("width", width)
165
+ // .attr("height", height)
166
+ // .attr("class", "bg")
167
+ // .style({'stroke-width':'2','stroke':'rgb(0,0,0)'})
168
+ // .attr("fill", "#FCFCFC");
169
+
170
+ // create the x axes
171
+ var bgrect = axes.append("svg:line")
172
+ .attr("x1", width)
173
+ .attr("y1", height)
174
+ .attr("x2", axeslabelmargin.left)
175
+ .attr("y2", height)
176
+ //.attr("class", "bg")
177
+ .style({'stroke-width':'1','stroke':'rgb(10,10,10)'});
178
+ //.attr("fill", "#FCFCFC");
179
+
180
+ // axes creation functions
181
+ var create_xAxis = function() {
182
+ return d3.svg.axis()
183
+ .ticks(4)
184
+ .scale(x)
185
+ .orient("bottom"); }
186
+
187
+ // // axis creation function
188
+ // var create_yAxis = function() {
189
+ // return d3.svg.axis()
190
+ // .scale(y) //linear scale function
191
+ // .orient("left"); }
192
+
193
+ // // draw the axes
194
+ // var yAxis = create_yAxis()
195
+ // .innerTickSize(6)
196
+ // .outerTickSize(0);
197
+
198
+ // axes.append("g")
199
+ // .attr("class", "y axis ")
200
+ // .attr("font-size", "14.0px")
201
+ // .attr("transform", "translate(0,0)")
202
+ // .call(yAxis);
203
+
204
+ var xAxis;
205
+ if (manualTicks.length > 0) {
206
+ xAxis = create_xAxis()
207
+ .innerTickSize(6)
208
+ .outerTickSize(0)
209
+ .tickValues(manualTicks);
210
+ }
211
+ else {
212
+ xAxis = create_xAxis()
213
+ .innerTickSize(6)
214
+ .outerTickSize(0);
215
+ }
216
+
217
+ axes.append("g")
218
+ .attr("class", "x axis ")
219
+ .attr("font-size", "14.0px")
220
+ .attr("transform", "translate(0," + (height) + ")")
221
+ .call(xAxis);
222
+
223
+ d3.selectAll(".tick line").style({'stroke':'black'});
224
+
225
+ // create the clip boundary
226
+ // var clip = axes.append("svg:clipPath")
227
+ // .attr("id","clip")
228
+ // .append("svg:rect")
229
+ // .attr("x",0)
230
+ // .attr("y",0)
231
+ // .attr("width",width)
232
+ // .attr("height",height);
233
+
234
+ // // now something else
235
+ // var unclipped_axes = axes;
236
+
237
+ // axes = axes.append("g")
238
+ // .attr("clip-path","url(#clip)");
239
+
240
+ // var ylabel = canvas.append("text")
241
+ // .text("State Rank")
242
+ // .attr("class","axes-text")
243
+ // .attr("x",(figwidth-width)/4)
244
+ // .attr("y",figheight/2+30)
245
+ // .attr("font-size", "16.0px")
246
+ // .attr("fill", "#000000")
247
+ // .attr("transform", "rotate(-90.0," + (figwidth-width)/4 + "," + (figheight/2+30) + ")");
248
+
249
+ var xlabel = canvas.append("text")
250
+ // .text("Happiness")
251
+ .text(xlabeltext)
252
+ .attr("class","axes-text")
253
+ .attr("x",width/2+(figwidth-width)/2)
254
+ .attr("y",3*(figheight-height)/4+height)
255
+ .attr("font-size", "16.0px")
256
+ .attr("fill", "#000000")
257
+ .attr("style", "text-anchor: middle;");
258
+
259
+ axes.selectAll("rect.staterect")
260
+ .data(sortedStates)
261
+ .enter()
262
+ .append("rect")
263
+ .attr("class", function(d,i) { return d[2]+" staterect"+" q"+classColor(i+1)+"-8"; })
264
+ .attr("x", function(d,i) { if (d[3]>0) { return figcenter; } else { return x(d[3]); } })
265
+ .attr("y", function(d,i) { return y(i+1); })
266
+ .style({'opacity':'1.0','stroke-width':'1.0','stroke':'rgb(100,100,100)'})
267
+ .attr("height",function(d,i) { return 11; } )
268
+ .attr("width",function(d,i) { if (d[3]>0) {return d3.max([x(d[3])-figcenter,0]);} else {return d3.max([figcenter-x(d[3]),0]); } } )
269
+ .on('mouseover', function(d,i){
270
+ var rectSelection = d3.select(this).style({'opacity':'1.0','stroke':'black','stroke-width':'1.0',});
271
+ hedotools.barchartoncall.test(d,i);
272
+ })
273
+ .on('mouseout', function(d){
274
+ var rectSelection = d3.select(this).style({'opacity':'1.0','stroke':'rgb(100,100,100)','stroke-width':'1.0',});
275
+ // var rectSelection = d3.select(this).style({opacity:'0.7'});
276
+ });
277
+
278
+ axes.selectAll("text.statetext")
279
+ .data(sortedStates)
280
+ .enter()
281
+ .append("text")
282
+ .attr("class", function(d,i) { return d[2]+" statetext"; })
283
+ .attr("x", function(d,i) { if (d[3]>0) { return figcenter-6; } else { return figcenter+6; } })
284
+ .style("text-anchor", function(d,i) { if (d[3]>0) { return "end";} else { return "start";}})
285
+ .attr("y",function(d,i) { return y(i+1)+11; } )
286
+ .text(function(d,i) { return (i+1)+". "+d[2]; })
287
+ .on('mouseover', function(d,i){
288
+ hedotools.barchartoncall.test(d,i);
289
+ });
290
+
291
+ // d3.select(window).on("resize.shiftplot",resizeshift);
292
+
293
+ // function resizeshift() {
294
+ // figwidth = parseInt(d3.select("#shift01").style('width')) - margin.left - margin.right,
295
+ // width = .775*figwidth
296
+ // figcenter = width/2;
297
+
298
+ // canvas.attr("width",figwidth);
299
+
300
+ // x.range([(sortedWords[0].length+3)*9, width-(sortedWords[0].length+3)*9]);
301
+ // topScale.range([width*.1,width*.9]);
302
+
303
+ // bgrect.attr("width",width);
304
+ // //axes.attr("transform", "translate(" + (0.125 * figwidth) + "," +
305
+ // // ((1 - 0.125 - 0.775) * figheight) + ")");
306
+
307
+ // // mainline.attr("d",line);
308
+
309
+ // // fix the x axis
310
+ // canvas.select(".x.axis").call(xAxis);
311
+
312
+ // clip.attr("width",width);
313
+
314
+ // // get the x label
315
+ // xlabel.attr("x",(leftOffsetStatic+width/2));
316
+
317
+ // // the andy reagan credit
318
+ // credit.attr("x",width-7);
319
+
320
+ // // line separating summary
321
+ // sepline.attr("x2",width);
322
+
323
+ // // all of the lower shift text
324
+ // axes.selectAll("text.shifttext").attr("x",function(d,i) { if (d>0) {return x(d)+2;} else {return x(d)-2; } } );
325
+ // }
326
+ };
327
+
328
+ var opublic = { setfigure: setfigure,
329
+ setdata: setdata,
330
+ _data: _data,
331
+ _manualTicks: _manualTicks,
332
+ _datanames: _datanames,
333
+ _figheight: _figheight,
334
+ _xlabeltext: _xlabeltext,
335
+ getSorted: getSorted,
336
+ plot: plot, };
337
+
338
+ return opublic;
339
+ };
340
+
341
+
342
+
343
+
344
+
345
+
346
+
347
+
348
+
@@ -0,0 +1,59 @@
1
+ // begin with some helper functions
2
+ // http://stackoverflow.com/a/1026087/3780153
3
+ function capitaliseFirstLetter(string)
4
+ {
5
+ return string.charAt(0).toUpperCase() + string.slice(1);
6
+ }
7
+
8
+ // this works really well, but it's deadly slow (working max 5 elements)
9
+ // and it's coupled to jquery
10
+ // http://stackoverflow.com/a/5047712/3780153
11
+ String.prototype.width = function(font) {
12
+ var f = font || '12px arial',
13
+ o = $('<div>' + this + '</div>')
14
+ .css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
15
+ .appendTo($('body')),
16
+ w = o.width();
17
+ o.remove();
18
+ return w;
19
+ }
20
+
21
+ String.prototype.safe = function() {
22
+ var tmp = this.split("/")
23
+ tmp[tmp.length-1] = escape(tmp[tmp.length-1])
24
+ return tmp.join("/");
25
+ }
26
+
27
+ // yup
28
+ // http://stackoverflow.com/questions/3883342/add-commas-to-a-number-in-jquery
29
+ function commaSeparateNumber(val){
30
+ while (/(\d+)(\d{3})/.test(val.toString())){
31
+ val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
32
+ }
33
+ return val;
34
+ }
35
+
36
+ function splitWidth(s,w) {
37
+ // s is the string
38
+ // w is the width that we want to split it to
39
+ var t = s.split(" ");
40
+ var n = [t[0]];
41
+ var i = 1;
42
+ var j = 0;
43
+ while (i<t.length) {
44
+ if ((n[j]+t[i]).width() < w) {
45
+ n[j] += " "+t[i]
46
+ }
47
+ else {
48
+ j++;
49
+ n.push(t[i]);
50
+ }
51
+ i++;
52
+ }
53
+ return n;
54
+ }
55
+
56
+ // look away
57
+ var intStr0 = ["zero","one","two","three","four","five","six","seven","eight","nine","then"];
58
+ var intStr = intStr0.slice(1,100);
59
+
@@ -0,0 +1,69 @@
1
+ // namespace it
2
+ var hedotools = hedotools || {};
3
+
4
+ // hedonometer.org/maps.html needs this in hedotools.map.js
5
+ var classColor = d3.scaleQuantize()
6
+ .range([0,1,2,3,4,5,6])
7
+ .domain([50,1]);
8
+
9
+ // begin with some helper functions
10
+ // http://stackoverflow.com/a/1026087/3780153
11
+ function capitaliseFirstLetter(string)
12
+ {
13
+ return string.charAt(0).toUpperCase() + string.slice(1);
14
+ }
15
+
16
+ // this works really well, but it's deadly slow (working max 5 elements)
17
+ // and it's coupled to jquery
18
+ // http://stackoverflow.com/a/5047712/3780153
19
+ String.prototype.width = function(font) {
20
+ var f = font || '12px arial',
21
+ o = $('<div>' + this + '</div>')
22
+ .css({'position': 'absolute', 'float': 'left', 'white-space': 'nowrap', 'visibility': 'hidden', 'font': f})
23
+ .appendTo($('body')),
24
+ w = o.width();
25
+ o.remove();
26
+ return w;
27
+ }
28
+
29
+
30
+
31
+ String.prototype.safe = function() {
32
+ var tmp = this.split("/")
33
+ tmp[tmp.length-1] = escape(tmp[tmp.length-1])
34
+ return tmp.join("/");
35
+ }
36
+
37
+ // yup
38
+ // http://stackoverflow.com/questions/3883342/add-commas-to-a-number-in-jquery
39
+ function commaSeparateNumber(val){
40
+ while (/(\d+)(\d{3})/.test(val.toString())){
41
+ val = val.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2');
42
+ }
43
+ return val;
44
+ }
45
+
46
+ function splitWidth(s,w) {
47
+ // s is the string
48
+ // w is the width that we want to split it to
49
+ var t = s.split(" ");
50
+ var n = [t[0]];
51
+ var i = 1;
52
+ var j = 0;
53
+ while (i<t.length) {
54
+ if ((n[j]+t[i]).width() < w) {
55
+ n[j] += " "+t[i]
56
+ }
57
+ else {
58
+ j++;
59
+ n.push(t[i]);
60
+ }
61
+ i++;
62
+ }
63
+ return n;
64
+ }
65
+
66
+ // look away
67
+ var intStr0 = ["zero","one","two","three","four","five","six","seven","eight","nine","then"];
68
+ var intStr = intStr0.slice(1,100);
69
+