Chuck Grimmett

November 2016

Index

  • Our Inflated Thanksgiving, 2016 Edition

    Our Inflated Thanksgiving, 2016 Edition

    It’s not just the balloons at the Macy’s Thanksgiving Day Parade that are inflated.

    Each year the American Farm Bureau Federation collects an informal price survey for a typical Thanksgiving dinner. The shopping list has remained the same for the past 31 years: turkey, bread stuffing, sweet potatoes, rolls with butter, peas, cranberries, a veggie tray, pumpkin pie with whipped cream, and coffee and milk, all to feed a family of 10.

    At first glance, it seems like the price of food has trended upward since 1986. Once you adjust for inflation, however, you get a different story. The inflation-adjusted price has stayed relatively flat over time with minor fluctuations. The purchasing power of our currency in circulation has gone down over time, driving up the price of goods.

    As Mark Perry points out, this is no reason for despair. The average hourly wage of the American worker has increased steadily, making Thanksgiving more affordable than ever. That is something to be thankful for this year.

    The data for this chart was collected by the American Farm Bureau Federation. I adjusted this price data for inflation using the BLS’s CPI Inflation Calculator. The chart was built using Chart.js.

    I retooled my original 2014 version. I paid closer attention to colors and made this one responsive. I think it is a big improvement!

    This was also published over at FEE’s Anything Peaceful blog.

  • Sol LeWitt’s Wall Drawings 852 and 853 with D3.js

    Sol LeWitt’s Wall Drawings 852 and 853 with D3.js

    Aren’t familiar with Sol LeWitt or his art? Read this.

    I had an insight while going through the D3.js documentation to find a solution to another piece: d3.area() with a highly curved fit line isn’t that different from wall drawings 852 and 853! By the way, 852 and 853 are actually distinct pieces, though they are often grouped together into one display like the one at MASSMoCA. I worked on the solution to 852 first, then modified it to make 853:

    Wall Drawing 852: A wall divided from the upper left to the lower right by a curvy line; left: glossy yellow; right: glossy purple.

    Wall Drawing 853: A wall bordered and divided vertically into two parts by a flat black band. Left part: a square is divided vertically by a curvy line. Left: glossy red; right: glossy green; Right part: a square is divided horizontally by a curvy line. Top: glossy blue; bottom: glossy orange.

    Here are previews of 852 and 853 respectively:

    Open my D3 implementation of Wall Drawing 852 in a new window →

    Open my D3 implementation of Wall Drawing 853 in a new window →

    They are also available as blocks: 852 853

    The underlying data set

    So much of implementing these drawings with D3.js rely on making a solid, usable data set to join elements with. (After all, D3 stands for data-driven-documents.) Creating these data sets is where I spend most of my time. Once I’ve created them, everything else follows pretty quickly.

    Since d3.area() makes an area graph with given points, I had to create a data set of points. I wanted the start and end points to be defined with random (but constrained) points in the middle. Here is what I came up with for 852:

    var	ww = window.innerWidth, 	wh = window.innerHeight;  function getRandomArbitrary(min, max) {   return Math.random() * (max - min) + min; }  function lineData() { 	var data = new Array(); 		for (var point = 0; point < 5; point++) { 			var x = getRandomArbitrary(ww/10, ww-ww/10); // Constrains within the middle 80% 			var y = getRandomArbitrary(wh/10, wh-wh/10);		 				data.push({ 				x: x, 				y: y 			}) 		} 		// Starting point upper left 		data.push({ 			x: 0, 			y: 0 		}); 		// End point lower right 		data.push({ 			x: ww, 			y: wh 		}) 	 	return data; }

    For 853, things were a little trickier since the left is oriented vertically:

    var ww = window.innerWidth, 	wh = window.innerHeight - 80, 	halfwidth = ww/2 - 60;	  function getRandomArbitrary(min, max) {   return Math.random() * (max - min) + min; }  function fillLeft() { 	var data = new Array(); 		for (var point = 0; point < 3; point++) { 			var x = getRandomArbitrary(halfwidth/10, halfwidth-halfwidth/10); // Keeps within the middle 80% 			var y = getRandomArbitrary(wh/10, wh-wh/10);		 				data.push({ 				x: x, 				y: y 			}) 		} 		data.push({ 			x: halfwidth/2, 			y: 0 		}); 		data.push({ 			x: halfwidth/2, 			y: wh 		}) 	 	return data; }   function fillRight() { 	var data = new Array(); 		for (var point = 0; point < 3; point++) { 			var x = getRandomArbitrary(halfwidth/10, halfwidth-halfwidth/10); // Keeps within the middle 80% 			var y = getRandomArbitrary(wh/10, wh-wh/10);		 				data.push({ 				x: x, 				y: y 			}) 		} 		data.push({ 			x: 0, 			y: wh/2 		}); 		data.push({ 			x: halfwidth, 			y: wh/2 		}) 	 	return data; } 

    d3.area()

    One of the most important things to note about d3.area() is that you must sort your data sets (by x if the chart is horizonal, y if vertical) before passing them to the function. Otherwise you end up with something like this because the values are out of order:

    Out of order values!

    D3 has selection.sort() built in, but you also need to pass a function telling it how to sort:

    .sort(function(a,b) { return +a.x - +b.x; }) // Sorting by x ascending .sort(function(a,b) { return +a.y - +b.y; }) // Sorting by y ascending

    Most area charts tend to be horizontal, and nearly all examples you can find are structured in this way. You need to pass .x, .y0, and .y1:

    var area = d3.area()     .x(function(d) { return d.x; })     .y0(window.innerHeight)     .y1(function(d) { return d.y; })     .curve(d3.curveBasis);

    If you are switching to a vertical orientation, you instead need to pass .y, .x0, and .x1:

    var areaLeft = d3.area() 	.x0(0)     .x1(function(d) { return d.x; })     .y(function(d) { return d.y; })     .curve(d3.curveBasis);

    Curves and Points

    I played with a number of curve functions and a number of points to see how they compared to traditional interpretations of Sol LeWitt’s pieces:

    • curveCatmullRom
    • curveCardinal
    • curveBasis
    • curveNatural
    • curveLinear

    I eventually settled on curveBasis for both pieces. It creates the smoothest curving line with the given random points, which produces results similar to the curved lines in Sol’s earlier work. curveCatmullRom was my second choice, but occasionally produced harsh concave regions.

    For the larger standalone piece (852) I used 7 total points, including the end points. For 853, which consists of two different curves, I used 5 points each since the widths were smaller.

    Rebuilding on screen resize

    When you resize the screen, the divs gets destroyed and then rebuilt based on the new screen size. Here is the function I use to handle that. It is the same thing I used on 86 and on my Jekyll posts heatmap calendar. I wrapped the D3 instructions in its own function, called that function on first load, then wrote a function to destroy the node divs when the screen is resized and reexecute the D3 instructions after the screen resize ends. I’m sure it can be done with regular javascript, but jQuery makes this kind of thing fast and easy:

    // run on first load sol852();  $(window).resize(function() {     if(this.resizeTO) clearTimeout(this.resizeTO);     this.resizeTO = setTimeout(function() {         $(this).trigger('resizeEnd');     }, 500); });  //resize on resizeEnd function $(window).bind('resizeEnd', function() { 	 d3.selectAll("div.node").remove(); 	 sol852(); });

    Tools Used

    • D3.js – The D3.js library is well-suited to Sol LeWitt’s works. D3’s foundational principle of joining data to elements is easy to apply to LeWitt’s drawings.
    • jQuery – I’m using jQuery to detect changes in the window size and trigger removing & rebuilding the visualization.

    Inspiration and References

    Detail shot of the version on display at MASSMoCA:

    Detail shot of the version on display at MASSMoCA:

  • Make Your Own Sol LeWitt 614!

    Make Your Own Sol LeWitt 614!

    It is also available as a block.

    Background

    Here are Sol’s original instructions for Wall Drawing 614:

    Wall Drawing 614: Rectangles formed by 3-inch (8 cm) wide India ink bands, meeting at right angles.

    Technical Details

    For the technical details of making the drawing itself, check out my post on it. This post focuses on rebuilding the tool based on user input.

    The Inputs

    I used a form, a range input, an output, and some in-line JavaScript to power the sliders and display their values in real-time:

    Black band width: type="range" name="paddingInput" id="paddingInputId" value="15" min="1" max="50" oninput="paddingOutputId.value = paddingInputId.value"> name="paddingOutput" id="paddingOutputId">15px /> Number of rectangles: type="range" name="rectInput" id="rectInputId" value="40" min="5" max="300" oninput="rectOutputId.value = rectInputId.value"> name="rectOutput" id="rectOutputId">40

    Live example:

    Black band width: 15px
    Number of rectangles: 40

    Basing my functions on the values of the inputs

    A set a variable to the element I wanted to access, then I called the .value of that element in my functions. (See the post on 614 for more info about these functions.)

    var padding = document.getElementById("paddingInputId"); var rectNumber = document.getElementById("rectInputId");  function treeData() {   var obj = {name: "rect"};   var children = [];   for (var row = 0; row < rectNumber.value; row++) {     children.push({id: row, value: getRandomArbitrary(60, 1000)})   }   obj['children'] = children;   return obj; }  var treemap = d3.treemap()     .size([width, height])     .padding(padding.value)     .round(true)     .tile(d3.treemapBinary);

    Rebuilding after inputs are changed

    Similar to rebuilding on screen resize in the original version, I detect when there is a change in one of the inputs and then call a function to remove the divs and rebuild the treemap based on the inputs. jQuery makes this kind of thing fast and easy:

    $( "#paddingInputId" ).change(function() {   	d3.selectAll("div.node").remove(); 	sol614(); });  $( "#rectInputId" ).change(function() {   	d3.selectAll("div.node").remove(); 	sol614(); });

    Want to dig in a little further? Check out my implementation and view source. All of the specs are there.

    Tools Used

    • D3.js – The D3.js library is well-suited to Sol LeWitt’s works. D3’s foundational principle of joining data to elements is easy to apply to LeWitt’s drawings.
    • jQuery – I’m using jQuery to detect changes in the window size and trigger removing & rebuilding the visualization.
  • Sol LeWitt’s Wall Drawing 614 with D3.js Treemap and Randomization

    Sol LeWitt’s Wall Drawing 614 with D3.js Treemap and Randomization

    Aren’t familiar with Sol LeWitt or his art? Read this.

    Wall Drawing 614: Rectangles formed by 3-inch (8 cm) wide India ink bands, meeting at right angles.

    Here is a preview:

    Open my D3 implementation of Wall Drawing 614 in a new window →

    It is also available as a block.

    Design Details

    • LeWitt’s instructions are pretty open. The drafter can choose the number of rectangles. I chose 40.
    • Since screens work in pixels instead of inches, I chose to make the bands 15px wide. I tried various configurations and I think 15px
    • I chose to conform this to the size of the viewer’s window and have each display randomly generated, so each viewer will have a unique experience.
    • Just as the size of the wall this piece was implemented on determined the density of the lines, the size of the window will determine the density. You can try out different densities by resizing the screen. The illustration will rebuild after you resize the window.
    • If you like this, I also made a version that you can customize.

    Technical Details

    • The placement and size of the rectangles are handled by d3.treemap().
    • I tried a few different tiling methods and decided to use d3.treemapBinary. I liked the look of it the best.
    • Each viewer gets a unique experience because the size of each rectangle is set at random on each page load.

    The underlying data set

    So much of implementing these drawings with D3.js rely on making a solid, usable data set to join elements with. (After all, D3 stands for data-driven-documents.) Creating these data sets is where I spend most of my time. Once I’ve created them, everything else follows pretty quickly. Here is the function that creates the underlying data set on this piece. I owe major props to Eric Davis on helping me figure this out. I was getting close, but nothing I made was accepted by treemap(). He helped me identify my issues, which included making this data set and then passing it to d3.hierarchy before passing it to treemap.

    function getRandomArbitrary(min, max) { 	  return Math.random() * (max - min) + min; }  function treeData() {   var obj = {name: "rect"};   var children = [];   for (var row = 0; row < 40; row++) {     children.push({id: row, value: getRandomArbitrary(60, 1000)})   }   obj['children'] = children;   return obj; }

    Working with treemap

    One of the difficulties of D3.js transitioning from v3 to v4 recently is that most of the tutorials you can find on the web no longer work. Thankfully Mike Bostock created a great example of a treemap with v4 that I referenced quite a bit.

    After a few passes through the documentation and a call on my friend Eric Davis, we figured out how to pass data to d3.treemap():

    1. Create a data set that has a clear parent-child structure with unique parents
    2. Pass this to d3.hierarchy
    3. Call .sum() on d3.hierarchy and sum the values before passing it to treemap
    4. Pass it to treemap

    See below:

    var width = window.innerWidth + 30, 	height = window.innerHeight + 30; var format = d3.format(",d");  var treemap = d3.treemap()     .size([width, height])     .padding(15)     .round(true)     .tile(d3.treemapBinary);  var tree = d3.hierarchy(tree);  tree.sum(function(d) { return d.value; }); // always log for debugging! console.log(tree);  treemap(tree);  d3.select("body") 	.selectAll(".node") 	.data(tree.leaves()) 	.enter().append("div") 	  .attr("class", "node") 	  .attr("title", function(d) { return d.data.id; }) 	  .style("left", function(d) { return d.x0 + "px"; }) 	  .style("top", function(d) { return d.y0 + "px"; }) 	  .style("width", function(d) { return d.x1 - d.x0 + "px"; }) 	  .style("height", function(d) { return d.y1 - d.y0 + "px"; }) 	  .style("background", "#fff");

    Rebuilding on screen resize

    When you resize the screen, the divs gets destroyed and then rebuilt based on the new screen size. Here is the function I use to handle that. It is the same thing I used on 86 and on my Jekyll posts heatmap calendar. I wrapped the D3 instructions in its own function, called that function on first load, then wrote a function to destroy the node divs when the screen is resized and reexecute the D3 instructions after the screen resize ends. I’m sure it can be done with regular javascript, but jQuery makes this kind of thing fast and easy:

    // run on first load sol614();  $(window).resize(function() {     if(this.resizeTO) clearTimeout(this.resizeTO);     this.resizeTO = setTimeout(function() {         $(this).trigger('resizeEnd');     }, 500); });  //resize on resizeEnd function $(window).bind('resizeEnd', function() { 	 d3.selectAll("div.node").remove(); 	 sol614(); });

    Want to dig in a little further? Check out my implementation and view source. All of the specs are there.

    Tools Used

    • D3.js – The D3.js library is well-suited to Sol LeWitt’s works. D3’s foundational principle of joining data to elements is easy to apply to LeWitt’s drawings.
    • jQuery – I’m using jQuery to detect changes in the window size and trigger removing & rebuilding the visualization.

    Inspiration and References

    Detail shot of the version on display at MASSMoCA:

    Detail shot of the version on display at MASSMoCA:

    Another shot of the version on display at MASSMoCA:

    Detail shot of the version on display at MASSMoCA:

    If you like this, I also made a version that you can customize. Check it out.