@andyreagan/hedotools 3.0.0 → 4.5.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.
@@ -8,7 +8,7 @@ hedotools.map = function() {
8
8
  return hedotools.map;
9
9
  }
10
10
 
11
- var classColor = d3.scale.quantize()
11
+ var classColor = d3.scaleQuantize()
12
12
  .range([0,1,2,3,4,5,6])
13
13
  .domain([50,1]);
14
14
 
@@ -51,79 +51,76 @@ hedotools.map = function() {
51
51
  boxpadding = 5,
52
52
  fullselboxwidth = selarray.length*boxpadding*2-boxpadding+initialpadding+d3.sum(selstringslen);
53
53
 
54
- var legendscale = d3.scale.linear()
54
+ var legendscale = d3.scaleLinear()
55
55
  .domain([340,730])
56
56
  .range([0,1]);
57
57
 
58
58
  function makeSelector() {
59
59
 
60
60
  canvas.append("text")
61
- .attr({
62
- "x": (w-70-fullselboxwidth-56),
63
- "y": 54,
64
- "fill": "grey",
65
- })
61
+ .attr("x", (w-70-fullselboxwidth-56))
62
+ .attr("y", 54)
63
+ .attr("fill", "grey")
66
64
  .text("Selecting ");
67
65
 
68
66
  var selgroup = canvas.append("g")
69
- .attr({"class": "selgroup",
70
- "transform": "translate("+(w-70-fullselboxwidth)+","+40+")",});
67
+ .attr("class", "selgroup")
68
+ .attr("transform", "translate("+(w-70-fullselboxwidth)+","+40+")");
71
69
 
72
70
  selgroup.append("rect")
73
- .attr({"class": "selbox",
74
- "x": 0,
75
- "y": 0,
76
- "rx": 3,
77
- "ry": 3,
78
- "width": fullselboxwidth,
79
- "height": 19,
80
- "fill": "#F8F8F8",
81
- 'stroke-width': '0.5',
82
- 'stroke': 'rgb(0,0,0)'});
71
+ .attr("class", "selbox")
72
+ .attr("x", 0)
73
+ .attr("y", 0)
74
+ .attr("rx", 3)
75
+ .attr("ry", 3)
76
+ .attr("width", fullselboxwidth)
77
+ .attr("height", 19)
78
+ .attr("fill", "#F8F8F8")
79
+ .attr('stroke-width', '0.5')
80
+ .attr('stroke', 'rgb(0,0,0)');
83
81
 
84
82
  selgroup.selectAll("rect.colorclick")
85
83
  .data(selarray)
86
84
  .enter()
87
85
  .append("rect")
88
- .attr({"class": function(d,i) { return "colorclick "+intStr[i]; },
89
- "x": function(d,i) { if (i === 0) { return 0; }
90
- else { return d3.sum(selstringslen.slice(0,i))+i*boxpadding+(i-1)*boxpadding+initialpadding; } },
91
- "y": 0,
92
- "rx": 3,
93
- "ry": 3,
94
- "width": function(d,i) { if (i === 0) { return selstringslen[i]+initialpadding+boxpadding; } else { return selstringslen[i]+boxpadding*2; }},
95
- "height": 19,
96
- "fill": "#F8F8F8", //http://www.w3schools.com/html/html_colors.asp
97
- 'stroke-width': '0.5',
98
- 'stroke': 'rgb(0,0,0)'});
86
+ .attr("class", function(d,i) { return "colorclick "+intStr[i]; })
87
+ .attr("x", function(d,i) { if (i === 0) { return 0; }
88
+ else { return d3.sum(selstringslen.slice(0,i))+i*boxpadding+(i-1)*boxpadding+initialpadding; } })
89
+ .attr("y", 0)
90
+ .attr("rx", 3)
91
+ .attr("ry", 3)
92
+ .attr("width", function(d,i) { if (i === 0) { return selstringslen[i]+initialpadding+boxpadding; } else { return selstringslen[i]+boxpadding*2; }})
93
+ .attr("height", 19)
94
+ .attr("fill", "#F8F8F8") //http://www.w3schools.com/html/html_colors.asp
95
+ .attr('stroke-width', '0.5')
96
+ .attr('stroke', 'rgb(0,0,0)');
99
97
 
100
98
  selgroup.selectAll("text")
101
99
  .data(selstrings)
102
100
  .enter()
103
101
  .append("text")
104
- .attr({ "x": function(d,i) {
102
+ .attr("x", function(d,i) {
105
103
  // start at 2
106
104
  if (i==0) { return initialpadding; }
107
105
  // then use 2+width+10+width+10+width...
108
106
  // for default padding of 5 on L/R
109
- else { return d3.sum(selstringslen.slice(0,i))+initialpadding+i*boxpadding*2; } },
110
- "y": 14,
111
- "class": function(d,i) { return "seltext "+intStr[i]; },
112
- })
107
+ else { return d3.sum(selstringslen.slice(0,i))+initialpadding+i*boxpadding*2; } })
108
+ .attr("y", 14)
109
+ .attr("class", function(d,i) { return "seltext "+intStr[i]; })
113
110
  .text(function(d,i) { return d; });
114
111
 
115
112
  selgroup.selectAll("rect.selclick")
116
113
  .data(selarray)
117
114
  .enter()
118
115
  .append("rect")
119
- .attr({"class": "selrect",
120
- "x": function(d,i) { if (i === 0) { return 0; }
121
- else { return d3.sum(selstringslen.slice(0,i))+i*boxpadding+(i-1)*boxpadding+initialpadding; } },
122
- "y": 0,
123
- "width": function(d,i) { if (i === 0) { return selstringslen[i]+initialpadding+boxpadding; } else { return selstringslen[i]+boxpadding*2; }},
124
- "height": 19,
125
- "fill": "white", //http://www.w3schools.com/html/html_colors.asp
126
- "opacity": "0.0",})
116
+ .attr("class", "selrect")
117
+ .attr("x", function(d,i) { if (i === 0) { return 0; }
118
+ else { return d3.sum(selstringslen.slice(0,i))+i*boxpadding+(i-1)*boxpadding+initialpadding; } })
119
+ .attr("y", 0)
120
+ .attr("width", function(d,i) { if (i === 0) { return selstringslen[i]+initialpadding+boxpadding; } else { return selstringslen[i]+boxpadding*2; }})
121
+ .attr("height", 19)
122
+ .attr("fill", "white") //http://www.w3schools.com/html/html_colors.asp
123
+ .attr("opacity", "0.0")
127
124
  .on("mousedown", function(d,i) {
128
125
  if (stateSelType !== d) {
129
126
  stateSelType = d;
@@ -177,35 +174,34 @@ hedotools.map = function() {
177
174
  fulllegendboxwidth = legendarray.length*boxpadding*2-boxpadding+initialpadding+d3.sum(legendstringslen);
178
175
 
179
176
  var legendgroup = canvas.append("g")
180
- .attr({"class": "legendgroup",
181
- "transform": "translate("+(w-50-fulllegendboxwidth)+","+(h-legendheight-legendheight-2)+")",});
177
+ .attr("class", "legendgroup")
178
+ .attr("transform", "translate("+(w-50-fulllegendboxwidth)+","+(h-legendheight-legendheight-2)+")");
182
179
 
183
180
  legendgroup.selectAll("rect.legendrect")
184
181
  .data(legendarray)
185
182
  .enter()
186
183
  .append("rect")
187
- .attr({"class": function(d,i) { return "q"+i+"-8"; },
188
- "x": function(d,i) { if (i === 0) { return 0; }
189
- else { return d3.sum(legendstringslen.slice(0,i))+i*boxpadding+(i-1)*boxpadding+initialpadding; } },
190
- "y": 0,
184
+ .attr("class", function(d,i) { return "q"+i+"-8"; })
185
+ .attr("x", function(d,i) { if (i === 0) { return 0; }
186
+ else { return d3.sum(legendstringslen.slice(0,i))+i*boxpadding+(i-1)*boxpadding+initialpadding; } })
187
+ .attr("y", 0)
191
188
  // "rx": 3,
192
189
  // "ry": 3,
193
- "width": function(d,i) { return legendstringslen[i]; },
194
- "height": legendheight,
195
- 'stroke-width': '1',
196
- 'stroke': 'rgb(0,0,0)'});
190
+ .attr("width", function(d,i) { return legendstringslen[i]; })
191
+ .attr("height", legendheight)
192
+ .attr('stroke-width', '1')
193
+ .attr('stroke', 'rgb(0,0,0)');
197
194
 
198
195
  legendgroup.selectAll("text.legendtext")
199
196
  .data(["less happy","happier"])
200
197
  .enter()
201
198
  .append("text")
202
- .attr({"x": function(d,i) {
199
+ .attr("x", function(d,i) {
203
200
  if (i==0) { return 0; }
204
- else { return fulllegendboxwidth-d.width(textsize+"px arial"); } },
205
- "y": legendheight+legendheight,
206
- "class": function(d,i) { return "legendtext"; },
207
- "font-size": textsize+"px",
208
- })
201
+ else { return fulllegendboxwidth-d.width(textsize+"px arial"); } })
202
+ .attr("y", legendheight+legendheight)
203
+ .attr("class", function(d,i) { return "legendtext"; })
204
+ .attr("font-size", textsize+"px")
209
205
  .text(function(d,i) { return d; });
210
206
  }
211
207
 
@@ -214,13 +210,13 @@ hedotools.map = function() {
214
210
  makeLegend((20+10*scaleFactor),(8+5*scaleFactor),(9+3*scaleFactor));
215
211
 
216
212
  //Define map projection
217
- var projection = d3.geo.albersUsa()
213
+ var projection = d3.geoAlbersUsa()
218
214
  .translate([w/2, h/2])
219
215
  .scale(w*1.3);
220
216
  //.scale(1000);
221
217
 
222
218
  //Define path generator
223
- var path = d3.geo.path()
219
+ var path = d3.geoPath()
224
220
  .projection(projection);
225
221
 
226
222
  var numColors = 20,
@@ -238,7 +234,7 @@ hedotools.map = function() {
238
234
  // console.log(colorStrings);
239
235
 
240
236
  //Define quantize scale to sort data values into buckets of color
241
- color = d3.scale.quantize()
237
+ color = d3.scaleQuantize()
242
238
  //.range(["rgb(237,248,233)","rgb(186,228,179)","rgb(116,196,118)","rgb(49,163,84)","rgb(0,109,44)"]);
243
239
  .range(colorStrings)
244
240
  .domain([
@@ -363,113 +359,101 @@ hedotools.map = function() {
363
359
  hoverboxyoffset = -30;
364
360
  }
365
361
 
366
- var hovergroup = canvas.append("g").attr({
367
- "class": "hoverinfogroup",
368
- "transform": "translate("+(x+hoverboxxoffset)+","+(y-hoverboxheight/2-hoverboxyoffset)+")",});
369
-
370
- var hoverbox = hovergroup.append("rect").attr({
371
- "class": "hoverinfobox",
372
- "x": 0,
373
- "y": 0,
374
- "width": hoverboxwidth,
375
- "height": hoverboxheight,
376
- "fill": "white",
377
- "stroke": "black",
378
- });
379
-
380
- hovergroup.append("text").attr({
381
- "class": "hoverinfotext",
382
- "x": 10,
383
- "y": 15,
384
- "font-size": 15,
385
- })
362
+ var hovergroup = canvas.append("g")
363
+ .attr("class", "hoverinfogroup")
364
+ .attr("transform", "translate("+(x+hoverboxxoffset)+","+(y-hoverboxheight/2-hoverboxyoffset)+")");
365
+
366
+ var hoverbox = hovergroup.append("rect")
367
+ .attr("class", "hoverinfobox")
368
+ .attr("x", 0)
369
+ .attr("y", 0)
370
+ .attr("width", hoverboxwidth)
371
+ .attr("height", hoverboxheight)
372
+ .attr("fill", "white")
373
+ .attr("stroke", "black");
374
+
375
+ hovergroup.append("text")
376
+ .attr("class", "hoverinfotext")
377
+ .attr("x", 10)
378
+ .attr("y", 15)
379
+ .attr("font-size", 15)
386
380
  .text(allData[i].name);
387
381
 
388
- hovergroup.append("line").attr({
389
- "class": "hoverinfotext",
390
- "x": 10,
391
- "y": 15,
392
- "font-size": 15,
393
- })
382
+ hovergroup.append("line")
383
+ .attr("class", "hoverinfotext")
384
+ .attr("x", 10)
385
+ .attr("y", 15)
386
+ .attr("font-size", 15)
394
387
  .text(allData[i].name);
395
388
 
396
- hovergroup.append("text").attr({
397
- "class": "hoverinfotext",
398
- "x": 10,
389
+ hovergroup.append("text")
390
+ .attr("class", "hoverinfotext")
391
+ .attr("x", 10)
399
392
  //"y": 55,
400
- "y": 38,
401
- "font-size": 17,
402
- })
393
+ .attr("y", 38)
394
+ .attr("font-size", 17)
403
395
  .text("Rank:"); // +"/51");
404
396
 
405
- hovergroup.append("text").attr({
406
- "class": "hoverinfotext",
407
- "x": 59,
408
- "y": 55,
409
- "font-size": 40,
410
- })
397
+ hovergroup.append("text")
398
+ .attr("class", "hoverinfotext")
399
+ .attr("x", 59)
400
+ .attr("y", 55)
401
+ .attr("font-size", 40)
411
402
  .text(sortedStateList[i]); // +"/51");
412
403
 
413
- hovergroup.append("text").attr({
414
- "class": "hoverinfotext",
415
- "x": 105,
416
- "y": 56,
417
- "font-size": 20,
418
- })
404
+ hovergroup.append("text")
405
+ .attr("class", "hoverinfotext")
406
+ .attr("x", 105)
407
+ .attr("y", 56)
408
+ .attr("font-size", 20)
419
409
  .text("out of 51");
420
410
 
421
- hovergroup.append("text").attr({
422
- "class": "hoverinfotext",
423
- "x": 10,
411
+ hovergroup.append("text")
412
+ .attr("class", "hoverinfotext")
413
+ .attr("x", 10)
424
414
  //"y": 73,
425
- "y": 79,
426
- "font-size": 15,
427
- })
415
+ .attr("y", 79)
416
+ .attr("font-size", 15)
428
417
  .text(happsstring);
429
418
 
430
- hovergroup.append("text").attr({
431
- "class": "hoverinfotext",
432
- "x": 10,
419
+ hovergroup.append("text")
420
+ .attr("class", "hoverinfotext")
421
+ .attr("x", 10)
433
422
  //"y": 89,
434
- "y": 97,
435
- "font-size": 13,
436
- })
423
+ .attr("y", 97)
424
+ .attr("font-size", 13)
437
425
  .text(wordsstring);
438
426
 
439
- hovergroup.append("text").attr({
440
- "class": "hoverinfotext",
441
- "x": 10,
427
+ hovergroup.append("text")
428
+ .attr("class", "hoverinfotext")
429
+ .attr("x", 10)
442
430
  //"y": 106,
443
- "y": 114,
444
- "font-size": 13,
445
- })
431
+ .attr("y", 114)
432
+ .attr("font-size", 13)
446
433
  .text(wordsstring2);
447
434
 
448
- hovergroup.append("text").attr({
449
- "class": "hoverinfotext",
450
- "x": 10,
435
+ hovergroup.append("text")
436
+ .attr("class", "hoverinfotext")
437
+ .attr("x", 10)
451
438
  //"y": 106,
452
- "y": 131,
453
- "font-size": 13,
454
- })
439
+ .attr("y", 131)
440
+ .attr("font-size", 13)
455
441
  .text("US Average Happiness: "+allData[51].avhapps.toFixed(2));
456
442
 
457
- hovergroup.append("text").attr({
458
- "class": "hoverinfotext",
459
- "x": 10,
443
+ hovergroup.append("text")
444
+ .attr("class", "hoverinfotext")
445
+ .attr("x", 10)
460
446
  //"y": 89,
461
- "y": 97+51,
462
- "font-size": 13,
463
- })
447
+ .attr("y", 97+51)
448
+ .attr("font-size", 13)
464
449
  .text(USwordsstring);
465
450
 
466
- hovergroup.append("text").attr({
467
- "class": "hoverinfotext",
468
- "x": 10,
451
+ hovergroup.append("text")
452
+ .attr("class", "hoverinfotext")
453
+ .attr("x", 10)
469
454
  //"y": 106,
470
- "y": 114+51,
471
- "font-size": 13,
472
- })
455
+ .attr("y", 114+51)
456
+ .attr("font-size", 13)
473
457
  .text(USwordsstring2);
474
458
 
475
459
  if (activeHover) {
@@ -129,24 +129,17 @@ hedotools.sankey = function() {
129
129
  figcenter = width/2;
130
130
  leftOffsetStatic = axeslabelmargin.left;
131
131
 
132
- var hovergroup = figure.append("div").attr({
133
- "class": "hoverinfogroup",
134
- // "transform": "translate("+(x+hoverboxxoffset+axeslabelmargin.left)+","+(d3.min([d3.max([0,y-hoverboxheight/2-hoverboxyoffset]),height-hoverboxheight]))+")",
135
- })
136
- .style({
137
- "position": "absolute",
138
- "top": "100px",
139
- "left": "100px",
140
- "visibility": "hidden",
141
- });
132
+ var hovergroup = figure.append("div").attr("class", "hoverinfogroup")
133
+ .style("position", "absolute")
134
+ .style("top", "100px")
135
+ .style("left", "100px")
136
+ .style("visibility", "hidden");
142
137
 
143
138
  function hidehover() {
144
139
  console.log("hiding hover");
145
140
  d3.selectAll("path").transition().duration(500).style("opacity","1.0");
146
141
  if (useTip) {
147
- hovergroup.style({
148
- "visibility": "hidden",
149
- });
142
+ hovergroup.style("visibility", "hidden");
150
143
  }
151
144
  }
152
145
 
@@ -160,12 +153,12 @@ hedotools.sankey = function() {
160
153
  .attr("class","canvas")
161
154
 
162
155
  // x scale, maps all the data to
163
- x = d3.scale.linear()
156
+ x = d3.scaleLinear()
164
157
  .domain([0,1])
165
158
  .range([5,width-10]);
166
159
 
167
160
  // linear scale function
168
- y = d3.scale.linear()
161
+ y = d3.scaleLinear()
169
162
  .domain([newlist.length,1])
170
163
  .range([height-20, 5]);
171
164
 
@@ -204,8 +197,22 @@ hedotools.sankey = function() {
204
197
  .text(function(d,i) { return (d.newindex+1)+". "+d.name; });
205
198
 
206
199
  // create an instance of the sankey to make paths
207
- var sankey = d3.sankey();
208
- path = sankey.link();
200
+ // horizontal sankey-style link path: this is the v3 d3.sankey().link()
201
+ // generator inlined. Nodes are positioned manually above, so we only need
202
+ // the path shape (a horizontal cubic Bezier), not the sankey layout.
203
+ path = function(d) {
204
+ var x0 = d.source.x + d.source.dx,
205
+ x1 = d.target.x,
206
+ xi = d3.interpolateNumber(x0, x1),
207
+ x2 = xi(0.5),
208
+ x3 = xi(0.5),
209
+ y0 = d.source.y + d.sy + d.dy / 2,
210
+ y1 = d.target.y + d.ty + d.dy / 2;
211
+ return "M" + x0 + "," + y0
212
+ + "C" + x2 + "," + y0
213
+ + " " + x3 + "," + y1
214
+ + " " + x1 + "," + y1;
215
+ };
209
216
 
210
217
  // create the sankey data thingy
211
218
  sankeydata = Array(oldlist.length);
@@ -232,17 +239,17 @@ hedotools.sankey = function() {
232
239
  };
233
240
  }
234
241
 
235
- pathwidth = d3.scale.linear()
242
+ pathwidth = d3.scaleLinear()
236
243
  .domain(d3.extent(data.map(function(d) { return Math.abs(d.change); })))
237
244
  .range([2,13]);
238
245
 
239
246
  pathselection = axes.selectAll("path.sankey").data(sankeydata)
240
247
  .enter()
241
248
  .append("path")
242
- .attr({ "d": path,
243
- "fill": "none",
244
- "class": function(d,i) { return "r"+classColor(data[i].oldindex)+"-8"; },
245
- "stroke-width": function(d,i) { return pathwidth(Math.abs(data[i].change)); } })
249
+ .attr("d", path)
250
+ .attr("fill", "none")
251
+ .attr("class", function(d,i) { return "r"+classColor(data[i].oldindex)+"-8"; })
252
+ .attr("stroke-width", function(d,i) { return pathwidth(Math.abs(data[i].change)); })
246
253
  .on("mouseover", function(d,i) {
247
254
  // console.log(i);
248
255
  // console.log(data[i]);
@@ -281,12 +288,10 @@ hedotools.sankey = function() {
281
288
  // tip.show;
282
289
  // console.log(d);
283
290
 
284
- hovergroup.style({
285
- "position": "absolute",
286
- "top": y+"px",
287
- "left": x+"px",
288
- "visibility": "visible",
289
- });
291
+ hovergroup.style("position", "absolute")
292
+ .style("top", y+"px")
293
+ .style("left", x+"px")
294
+ .style("visibility", "visible");
290
295
 
291
296
  hovergroup.selectAll("p,h3,button,br").remove();
292
297
 
@@ -438,7 +443,7 @@ hedotools.sankey = function() {
438
443
  clearTimeout(popuptimer);
439
444
  popuptimer = setTimeout(hidehover,timeout);
440
445
  var rectSelection = d3.select(this)
441
- .style({ 'opacity':'1.0', })
446
+ .style('opacity', '1.0')
442
447
  });
443
448
 
444
449
  return hedotools.sankey;
@@ -493,12 +498,11 @@ hedotools.sankey = function() {
493
498
  pathselection.data(sankeydata)
494
499
  .transition()
495
500
  .duration(3000)
496
- .attr({ "d": path,
501
+ .attr("d", path)
497
502
  // don't update this
498
503
  // because the transition is applied by the css at the end
499
504
  // and it messes up the whole effect
500
- // "class": function(d,i) { return "r"+classColor(data[i].oldindex)+"-8"; },
501
- "stroke-width": function(d,i) { return pathwidth(Math.abs(data[i].change)); } });
505
+ .attr("stroke-width", function(d,i) { return pathwidth(Math.abs(data[i].change)); });
502
506
 
503
507
  return hedotools.sankey;
504
508
  };