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.
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.
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:
varww=window.innerWidth,wh=window.innerHeight;functiongetRandomArbitrary(min,max){returnMath.random()*(max-min)+min;}functionlineData(){vardata=newArray();for(varpoint=0;point<5;point++){varx=getRandomArbitrary(ww/10,ww-ww/10);// Constrains within the middle 80%vary=getRandomArbitrary(wh/10,wh-wh/10);data.push({x:x,y:y})}// Starting point upper leftdata.push({x:0,y:0});// End point lower rightdata.push({x:ww,y:wh})returndata;}
For 853, things were a little trickier since the left is oriented vertically:
varww=window.innerWidth,wh=window.innerHeight-80,halfwidth=ww/2-60;functiongetRandomArbitrary(min,max){returnMath.random()*(max-min)+min;}functionfillLeft(){vardata=newArray();for(varpoint=0;point<3;point++){varx=getRandomArbitrary(halfwidth/10,halfwidth-halfwidth/10);// Keeps within the middle 80%vary=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})returndata;}functionfillRight(){vardata=newArray();for(varpoint=0;point<3;point++){varx=getRandomArbitrary(halfwidth/10,halfwidth-halfwidth/10);// Keeps within the middle 80%vary=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})returndata;}
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:
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:
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 loadsol852();$(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.
Sol LeWitt is a conceptual artist who typically produced directions for producing a piece of art. Those ideas are the art itself. Learn more about Sol LeWitt here.
I made a standalone version with 40 rectangles and 15px wide bands. I thought it would be a fun learning experience to make it customizable with user inputs, so I made it possible for users to change the number of rectangles and the width of the bands and see the differences in real-time.
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"> px /> Number of rectangles: type="range"name="rectInput"id="rectInputId"value="40"min="5"max="300"oninput="rectOutputId.value = rectInputId.value">
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:
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.
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.
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.
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():
Create a data set that has a clear parent-child structure with unique parents
Pass this to d3.hierarchy
Call .sum() on d3.hierarchy and sum the values before passing it to treemap
Pass it to treemap
See below:
varwidth=window.innerWidth+30,height=window.innerHeight+30;varformat=d3.format(",d");vartreemap=d3.treemap().size([width,height]).padding(15).round(true).tile(d3.treemapBinary);vartree=d3.hierarchy(tree);tree.sum(function(d){returnd.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){returnd.data.id;}).style("left",function(d){returnd.x0+"px";}).style("top",function(d){returnd.y0+"px";}).style("width",function(d){returnd.x1-d.x0+"px";}).style("height",function(d){returnd.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 loadsol614();$(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();});
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.