Data Visualization

Projected Time

8 hours (1 full day)

Prerequisites

Motivation

Data visualization’s benefits are numerous. It can provide new insights by simplifying complex data, revealing new patterns, and generally making data easier and more interesting for the everyday user. Charts and graphs are powerfully enriching analytic tools for business.

Just as there are many different ways to visualize a single set of data, there are also a number of great open-source libraries to leverage. D3.js is covered more extensively in the slides and presentation; a few other ones to explore are chart.js, processing.js, pygal (python), and ember-charts (built with Ember.js and d3.js). For JavaScript libraries choices, D3 is the most known and popular choice due to its power and flexibility; and we will use it to create data visualization (charts, graphs) in the guided activity and independent practice here.

Companies like Fidelity and Strava use charts and graphs to monitor financial, medical, and geographical data for the user. The products of Datadog, Carto, and Plot.ly revolve around data visualizations for site dashboards.

Objectives

Participants will be able to::

Specific Things to Learn

Materials

Lesson

Things to Remember

Guided Practice

Let’s Make a Bar Chart (using div)

const data = [4, 8, 15, 16, 23, 42];

const width = 500;
// D3's linear scaling method to convert data into chart width proportion
const x = d3
  .scaleLinear()
  .domain([0, d3.max(data)])
  .range([0, width]);

// D3 creating div elements using selection
// We feed in the data, dimensions and style using D3's methods
d3.select('.chart')
  .selectAll('div')
  // data() method takes a data set and passes 'd' when chaining methods together
  .data(data)
  // enter() method matches the data being handed with the DOM elements by creating new elements
  .enter()
  .append('div')
  .style('width', function (d) {
    return x(d) + 'px';
  })
  .text(function (d) {
    return d;
  });

Style with D3 rather than CSS

   .style("background-color", "steelblue")
   .style("color", "white")
   .style("padding", "3px")
   .style("margin", "1px")
   .style("font", "10px sans-serif")
   .style("text-align", "right")

Make it rainbow

const colors = ['violet', 'lightblue', 'limegreen', 'yellow', 'orange', 'red'];
   .style("background-color", function(d, i) { return colors[i] })
   .style("background-color", function(d, i) { return colors[i] })

The above all works fine, but div elements are limited in their shape and positioning in the DOM.

Now that we have done this once in a div, we’re going to look at doing it another way. We will use another element called SVG (Scalable Vector Graphics) introduced in HTML5 which is created for drawing paths, boxes, circles, text, and graphic images. Today, we won’t be learning the ins and outs of vector graphics–all we need is to learn what SVG stands for and learn enough d3 methods that manipulate SVGs to make a graph appear on your webpage.

// From above
const data = [4, 8, 15, 16, 23, 42];
const colors = ['violet', 'lightblue', 'limegreen', 'yellow', 'orange', 'red'];
const width = 500,
  height = 16,
  margin = 3;
const x = d3
  .scaleLinear()
  .domain([0, d3.max(data)])
  .range([0, width]);
// Create SVG element
// We will group each rect and corresponding text in a group (g)
const g = d3
  .select('svg')
  .attr('width', width)
  .attr('height', (height + margin) * data.length)
  .selectAll('g')
  .data(data)
  .enter()
  .append('g')
  .attr('transform', function (d, i) {
    return 'translate(0,' + i * (height + margin) + ')';
  });
// Create each bar using rect and text elements
g.append('rect')
  .attr('fill', function (d, i) {
    return colors[i];
  })
  .attr('width', x)
  .attr('height', height);
g.append('text')
  .attr('x', 1)
  .attr('y', height / 2)
  .attr('dy', '.35em')
  .style('font', '10px sans-serif')
  .attr('fill', 'white')
  .text(function (d) {
    return d;
  });

Let’s flip our SVG chart:

const g = d3
  .select('svg')
  .attr('height', width)
  .attr('width', (height + margin) * data.length)
  .selectAll('g')
  .data(data)
  .enter()
  .append('g')
  .attr('transform', function (d, i) {
    return 'translate(' + i * (height + margin) + ',' + (width - x(d)) + ')';
  });
// Create each bar using rect and text elements
g.append('rect')
  .attr('fill', function (d, i) {
    return colors[i];
  })
  .attr('height', x)
  .attr('width', height);
g.append('text')
  .attr('x', 1)
  .attr('y', height / 2)
  .attr('dy', '.35em')
  .style('font', '10px sans-serif')
  .attr('fill', 'white')
  .text(function (d) {
    return d;
  });

Independent Practice

More Practice with SVG & D3.js

Challenge

An Activity Aimed at Revealing Meaning through Data

  1. Select a data set that interests you!
  1. Review the dataset you selected. Consider questions you might want to answer through it, and determine what columns/subsets of the set you would need to use in order to gain insights into those questions.

  2. Determine what visualization(s) would be most meaningful to answer your question using your data.

  1. Select an open-source tool to create one or two meaningful charts/graphs. Embed your visualization(s) on a webpage.
  1. Present to the cohort or a peer and discuss the process you went through to create meaning and stories through data!

Check for Understanding

Supplemental Materials

Tools and libaries

Some suggestions for data sources

Resources on how best to visualize data