@andyreagan/hedotools 3.0.0 → 4.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.
package/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  hedotools
2
2
  =========
3
3
 
4
- a collection of js tools in use at hedonometer.org
4
+ [![CI](https://github.com/andyreagan/hedotools/actions/workflows/ci.yml/badge.svg)](https://github.com/andyreagan/hedotools/actions/workflows/ci.yml)
5
+ [![npm](https://img.shields.io/npm/v/@andyreagan/hedotools)](https://www.npmjs.com/package/@andyreagan/hedotools)
6
+
7
+ a collection of js tools in use at hedonometer.org
package/bundle.sh CHANGED
@@ -1,11 +1,18 @@
1
- # version them with d3 versions
2
- cat hedotools.init.js hedotools.urllib.js hedotools.barchart.js hedotools.lens.js hedotools.map.js hedotools.sankey.js hedotools.shifter.js > hedotools.v3.js
3
- cat hedotools.init.v4.js hedotools.urllib.js hedotools.shifter.v4.js > hedotools.v4.js
4
- # minify
5
- node ../node_modules/minifier/index.js hedotools.v3.js
6
- node ../node_modules/minifier/index.js hedotools.v4.js
7
- # link
8
- rm hedotools.js hedotools.min.js
9
- ln -s hedotools.v3.js hedotools.js
10
- ln -s hedotools.v3.min.js hedotools.min.js
1
+ #!/usr/bin/env bash
2
+ # Concatenate the D3 v4 sources into a single browser bundle for hedonometer.org.
3
+ # Order matters: init.v4 defines the `hedotools` namespace + helpers first, and
4
+ # the dashboard modules (barchart/lens/map/sankey) depend on the shifter.
5
+ set -euo pipefail
6
+ cd "$(dirname "$0")/js"
11
7
 
8
+ cat \
9
+ hedotools.init.v4.js \
10
+ hedotools.urllib.js \
11
+ hedotools.barchart.js \
12
+ hedotools.lens.js \
13
+ hedotools.map.js \
14
+ hedotools.sankey.js \
15
+ hedotools.shifter.v4.js \
16
+ > hedotools.v4.js
17
+
18
+ echo "built js/hedotools.v4.js"
@@ -134,12 +134,12 @@ hedotools.barchart = function() {
134
134
 
135
135
  // x scale, maps all the data to
136
136
  var absDataMax = d3.max([d3.max(data),-d3.min(data)]);
137
- var x = d3.scale.linear()
137
+ var x = d3.scaleLinear()
138
138
  .domain([-absDataMax,absDataMax])
139
139
  .range([5,width-10]);
140
140
 
141
141
  // linear scale function
142
- var y = d3.scale.linear()
142
+ var y = d3.scaleLinear()
143
143
  .domain([data.length,1])
144
144
  .range([height-20, 5]);
145
145
 
@@ -174,15 +174,13 @@ hedotools.barchart = function() {
174
174
  .attr("x2", axeslabelmargin.left)
175
175
  .attr("y2", height)
176
176
  //.attr("class", "bg")
177
- .style({'stroke-width':'1','stroke':'rgb(10,10,10)'});
177
+ .style('stroke-width','1').style('stroke','rgb(10,10,10)');
178
178
  //.attr("fill", "#FCFCFC");
179
179
 
180
180
  // axes creation functions
181
181
  var create_xAxis = function() {
182
- return d3.svg.axis()
183
- .ticks(4)
184
- .scale(x)
185
- .orient("bottom"); }
182
+ return d3.axisBottom(x)
183
+ .ticks(4); }
186
184
 
187
185
  // // axis creation function
188
186
  // var create_yAxis = function() {
@@ -192,8 +190,8 @@ hedotools.barchart = function() {
192
190
 
193
191
  // // draw the axes
194
192
  // var yAxis = create_yAxis()
195
- // .innerTickSize(6)
196
- // .outerTickSize(0);
193
+ // .tickSizeInner(6)
194
+ // .tickSizeOuter(0);
197
195
 
198
196
  // axes.append("g")
199
197
  // .attr("class", "y axis ")
@@ -204,14 +202,14 @@ hedotools.barchart = function() {
204
202
  var xAxis;
205
203
  if (manualTicks.length > 0) {
206
204
  xAxis = create_xAxis()
207
- .innerTickSize(6)
208
- .outerTickSize(0)
205
+ .tickSizeInner(6)
206
+ .tickSizeOuter(0)
209
207
  .tickValues(manualTicks);
210
208
  }
211
209
  else {
212
210
  xAxis = create_xAxis()
213
- .innerTickSize(6)
214
- .outerTickSize(0);
211
+ .tickSizeInner(6)
212
+ .tickSizeOuter(0);
215
213
  }
216
214
 
217
215
  axes.append("g")
@@ -220,7 +218,7 @@ hedotools.barchart = function() {
220
218
  .attr("transform", "translate(0," + (height) + ")")
221
219
  .call(xAxis);
222
220
 
223
- d3.selectAll(".tick line").style({'stroke':'black'});
221
+ d3.selectAll(".tick line").style('stroke','black');
224
222
 
225
223
  // create the clip boundary
226
224
  // var clip = axes.append("svg:clipPath")
@@ -263,15 +261,15 @@ hedotools.barchart = function() {
263
261
  .attr("class", function(d,i) { return d[2]+" staterect"+" q"+classColor(i+1)+"-8"; })
264
262
  .attr("x", function(d,i) { if (d[3]>0) { return figcenter; } else { return x(d[3]); } })
265
263
  .attr("y", function(d,i) { return y(i+1); })
266
- .style({'opacity':'1.0','stroke-width':'1.0','stroke':'rgb(100,100,100)'})
264
+ .style('opacity','1.0').style('stroke-width','1.0').style('stroke','rgb(100,100,100)')
267
265
  .attr("height",function(d,i) { return 11; } )
268
266
  .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
267
  .on('mouseover', function(d,i){
270
- var rectSelection = d3.select(this).style({'opacity':'1.0','stroke':'black','stroke-width':'1.0',});
268
+ var rectSelection = d3.select(this).style('opacity','1.0').style('stroke','black').style('stroke-width','1.0');
271
269
  hedotools.barchartoncall.test(d,i);
272
270
  })
273
271
  .on('mouseout', function(d){
274
- var rectSelection = d3.select(this).style({'opacity':'1.0','stroke':'rgb(100,100,100)','stroke-width':'1.0',});
272
+ var rectSelection = d3.select(this).style('opacity','1.0').style('stroke','rgb(100,100,100)').style('stroke-width','1.0');
275
273
  // var rectSelection = d3.select(this).style({opacity:'0.7'});
276
274
  });
277
275
 
@@ -93,19 +93,20 @@ hedotools.lens = function() {
93
93
 
94
94
 
95
95
  // create the x and y axis
96
- var x = d3.scale.linear()
96
+ var x = d3.scaleLinear()
97
97
  .domain([1.00,9.00])
98
98
  // .domain(d3.extent(lens))
99
99
  .range([0,width]);
100
100
 
101
101
  // use d3.layout http://bl.ocks.org/mbostock/3048450
102
- var data = d3.layout.histogram()
103
- .bins(x.ticks(65))
102
+ var data = d3.histogram()
103
+ .domain(x.domain())
104
+ .thresholds(x.ticks(65))
104
105
  (lens);
105
106
 
106
107
  // linear scale function
107
- var y = d3.scale.linear()
108
- .domain([0,d3.max(data,function(d) { return d.y; } )])
108
+ var y = d3.scaleLinear()
109
+ .domain([0,d3.max(data,function(d) { return d.length; } )])
109
110
  .range([height, 0]);
110
111
 
111
112
  // create the axes themselves
@@ -121,27 +122,23 @@ hedotools.lens = function() {
121
122
  .attr("width", width)
122
123
  .attr("height", height)
123
124
  .attr("class", "bg")
124
- .style({'stroke-width':'2','stroke':'rgb(0,0,0)'})
125
+ .style('stroke-width','2').style('stroke','rgb(0,0,0)')
125
126
  .attr("fill", "#FFFFF0");
126
127
 
127
128
  // axes creation functions
128
129
  var create_xAxis = function() {
129
- return d3.svg.axis()
130
- .scale(x)
131
- .ticks(9)
132
- .orient("bottom"); }
130
+ return d3.axisBottom(x)
131
+ .ticks(9); }
133
132
 
134
133
  // axis creation function
135
134
  var create_yAxis = function() {
136
- return d3.svg.axis()
137
- .ticks(3)
138
- .scale(y) //linear scale function
139
- .orient("left"); }
135
+ return d3.axisLeft(y)
136
+ .ticks(3); }
140
137
 
141
138
  // draw the axes
142
139
  var yAxis = create_yAxis()
143
- .innerTickSize(6)
144
- .outerTickSize(0);
140
+ .tickSizeInner(6)
141
+ .tickSizeOuter(0);
145
142
 
146
143
  axes.append("g")
147
144
  .attr("class", "top")
@@ -150,8 +147,8 @@ hedotools.lens = function() {
150
147
  .call(yAxis);
151
148
 
152
149
  var xAxis = create_xAxis()
153
- .innerTickSize(6)
154
- .outerTickSize(0);
150
+ .tickSizeInner(6)
151
+ .tickSizeOuter(0);
155
152
 
156
153
  axes.append("g")
157
154
  .attr("class", "x axis ")
@@ -159,7 +156,7 @@ hedotools.lens = function() {
159
156
  .attr("transform", "translate(0," + (height) + ")")
160
157
  .call(xAxis);
161
158
 
162
- d3.selectAll(".tick line").style({'stroke':'black'});
159
+ d3.selectAll(".tick line").style('stroke','black');
163
160
 
164
161
  // create the clip boundary
165
162
  var clip = axes.append("svg:clipPath")
@@ -200,18 +197,18 @@ hedotools.lens = function() {
200
197
  .enter()
201
198
  .append("g")
202
199
  .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) + ")"; });
200
+ .attr("fill",function(d,i) { if (d.x0 > lensMean) {return "#D3D3D3";} else { return "#D3D3D3";}})
201
+ .attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; });
205
202
 
206
203
  var mainrect = bar.append("rect")
207
204
  .attr("x", 1)
208
- .attr("width", x(data[0].dx+1)-2 )
209
- .attr("height", function(d) { return height - y(d.y); });
205
+ .attr("width", x(data[0].x1 - data[0].x0 + 1)-2 )
206
+ .attr("height", function(d) { return height - y(d.length); });
210
207
 
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");
208
+ var line = d3.line()
209
+ .x(function(d,i) { return x(d.x0); })
210
+ .y(function(d) { return y(d.length); })
211
+ .curve(d3.curveLinear);
215
212
 
216
213
  var mainline = axes.append("path")
217
214
  .datum(data)
@@ -223,7 +220,7 @@ hedotools.lens = function() {
223
220
 
224
221
  //console.log(x(d3.min(lens)));
225
222
 
226
- var brushX = d3.scale.linear()
223
+ var brushX = d3.scaleLinear()
227
224
  .domain([1,9])
228
225
  // .domain(d3.extent(lens))
229
226
  .range([figwidth*.125,width+figwidth*.125]);
@@ -232,7 +229,9 @@ hedotools.lens = function() {
232
229
 
233
230
  function brushended() {
234
231
  if (!d3.event.sourceEvent) return;
235
- var extent0 = brush.extent(),
232
+ if (!d3.event.selection) return;
233
+ // selection is in pixels; invert through brushX to data space
234
+ var extent0 = d3.event.selection.map(brushX.invert),
236
235
  extent1 = extent0; // should round it to bins
237
236
 
238
237
  onredrawfunction();
@@ -247,26 +246,24 @@ hedotools.lens = function() {
247
246
  }
248
247
 
249
248
  d3.select(this).transition()
250
- .call(brush.extent(lensExtent))
251
- .call(brush.event);
249
+ .call(brush.move, lensExtent.map(brushX));
252
250
 
253
251
  encoder.varval(lensExtent);
254
252
  }
255
253
 
256
- var brush = d3.svg.brush()
257
- .x(brushX)
258
- .extent(lensExtent)
259
- .on("brushend",brushended);
254
+ var brush = d3.brushX()
255
+ .extent([[brushX.range()[0], 0], [brushX.range()[1], height]])
256
+ .on("end",brushended);
260
257
 
261
258
  var gBrush = canvas.append("g")
262
259
  .attr("class","lensbrush")
263
- .call(brush)
264
- .call(brush.event);
260
+ .call(brush);
261
+ gBrush.call(brush.move, lensExtent.map(brushX));
265
262
 
266
263
  gBrush.selectAll("rect")
267
264
  .attr("height",height)
268
265
  .attr("y",0)
269
- .style({'stroke-width':'2','stroke':'rgb(100,100,100)','opacity': 0.95})
266
+ .style('stroke-width','2').style('stroke','rgb(100,100,100)').style('opacity',0.95)
270
267
  .attr("fill", "#FCFCFC");
271
268
 
272
269
  //console.log(lensExtent);
@@ -288,18 +285,17 @@ hedotools.lens = function() {
288
285
  //xAxisHandle.call(xAxis);
289
286
  canvas.select(".x.axis").call(xAxis);
290
287
 
291
- canvas.selectAll(".distrect").attr("transform", function(d) { return "translate(" + x(d.x) + "," + y(d.y) + ")"; });
288
+ canvas.selectAll(".distrect").attr("transform", function(d) { return "translate(" + x(d.x0) + "," + y(d.length) + ")"; });
292
289
 
293
290
  // xlabel.attr("x",(leftOffsetStatic+width/2));
294
291
 
295
- d3.selectAll(".tick line").style({'stroke':'black'});
292
+ d3.selectAll(".tick line").style('stroke','black');
296
293
 
297
294
  // //brushX.range([figwidth*.125,width+figwidth*.125]);
298
295
  brushX.range([leftOffsetStatic,leftOffsetStatic+width]);
299
- brush.x(brushX);
296
+ brush.extent([[brushX.range()[0], 0], [brushX.range()[1], height]]);
300
297
  d3.select(".lensbrush") //.transition()
301
- .call(brush.extent(lensExtent))
302
- .call(brush.event);
298
+ .call(brush.move, lensExtent.map(brushX));
303
299
  //brushing();
304
300
  //brush.event();
305
301
  };
@@ -310,22 +306,21 @@ hedotools.lens = function() {
310
306
  //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
307
 
312
308
  figure.selectAll("div.btn-group-vertical").remove();
313
- var buttongroup = figure.append("div").attr({"class":"btn-group-vertical pull-right",})
309
+ var buttongroup = figure.append("div").attr("class","btn-group-vertical pull-right")
314
310
  // var defaults = [[4,6],[3,7],[3,9],[1,7],[5,5]];
315
311
  var defaults = [[4,6],[3,7],[5,5]];
316
312
  // var defaultnames = ["Default","Wide","Sad","Happy","None"];
317
313
  var defaultnames = ["Default","Wide","None"];
318
314
  buttongroup.selectAll("button").data(defaults).enter()
319
315
  .append("button")
320
- .attr({"type":"button",
321
- "class": function(d,i) { return "btn btn-default btn-xs "+defaultnames[i]; },})
316
+ .attr("type","button")
317
+ .attr("class", function(d,i) { return "btn btn-default btn-xs "+defaultnames[i]; })
322
318
  .html(function(d,i) { return defaultnames[i]; })
323
319
  .on("click",function(d,i) {
324
320
  figure.selectAll("button").attr("class","btn btn-default btn-xs");
325
321
  d3.select(this).attr("class","btn btn-primary btn-xs");
326
322
  d3.select(".lensbrush") //.transition()
327
- .call(brush.extent(d))
328
- .call(brush.event);
323
+ .call(brush.move, d.map(brushX));
329
324
  });
330
325
  // initially check if any are matched
331
326
  console.log(lensExtent);