Reading Notes for D3.js Part II (Chapter 6)

  |   Source

This post is transferred from my old Hexo blog site, created on 2014.

This is a Reading Note for Interactive Data Visualization for the Web - An Introduction to Designing with D3 by Scott Murray, pulished by O'Reilly, 2013

This post will contains concepts that I think it's important for me to be familiar with in D3 book.

Chapter 6 : Draw Graph basiced on Data

code example for this chapter is here. This part involves lots of code practice.

Everything is rectangular for browser. Using div is the easiest way to draw a Bar Chart.

code part I

attr() set up the value for a DOM attribute, such as .attr('class','bar')

style() add css style to an element

classed() quickly add/delete class to an element, such as .classed('bar',true/false) to add/delete a class bar to an element

select().selectAll().data().enter() put data to data, then use/create elements using selectAll(), append them in select(), for example:"test")) //select the space that is a div "test"
    .selectAll('div') //create divs in 'test' div
    .data(dataset).enter(); //for each new div, put data to it

Draw SVG

code part II

For each SVG element, all its properties are defined by property='XXX', such as <element property = 'value'></element>

First step is always create a SVG element:

var w = 200; //This is pixel
var h = 100;
var svg ='chapter6').getElementsByClassName("2")[0])
//Create size of svg

Then draw a circle:

var dataset = [10,9,15,25,18,26,11,13];
var circles = svg.selectAll('circle')
                .append('circle'); //create circle and put data in

            return (i*20)+10;
            return d;
            //Add center point and radius. Then add color to it

Draw Bar Chart

code part III

In D3, always choose element that you need, even before it is created.

SVG only support alignment through left-upper corner. To make sure bar chart using bottom alignment:

  • make the y, which is the vertical point of alignment point, equals to total height - its own height

  • then make each bar has height equal to its data.

Make color be ratio to data:

//add color to rectangular. Make each bar has color ratio to its data

The way to add tags is the same to add rect:

//create text
var text = svg.selectAll('text')

//set text position. Adjust position a little bit
                return i*(w/dataset.length)+5;

text.attr('y', function(d){
                return h-(d+5);

//add text value
                return d; //Get integer value in JavaScript

//set font family
        .attr('text-anchor','middle'); //set text alignment

Draw Scatter Plot

code part IV

For a scatter plot, data should look like an array of arrays:

var dataset = [
//First Numer is X axis, Second Numer is y axis

The way to draw a scatter plot is generating lots of circles on svg, each circle is a point:

//prepare data
var dataset = [

//create svg element
var w = 300; //This is pixel
var h = 150;
var svg ='chapter6').getElementsByClassName("4")[0])

//create circles
var circle = svg.selectAll('circle')

//set up cx, cy, r
circle.attr('cx',function(d){ //Here, d is the single array in array group
                    return d[0]; //note it should be first element in array
                    return h-d[1]; //y should be second element in array

//Make area of circle be proportional to data, using A = π*r^2. We just assume y is the area here
                    return Math.sqrt(d[1]/3.14) + 5;

//Add Tags
var text = svg.selectAll('text')

                    return d[0] + "," + d[1];

                    return d[0];
                    return h-d[1];
Comments powered by Disqus