Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3JS selectAll to append rect

I am amazed by the brevity of d3.js to visualize data. The complexity hidden under this brevity also makes it a bit hard to understand how it works. Below is part of the code from http://mbostock.github.com/d3/ex/calendar.html

The question is how it creates rect for day, within the number of svg created (var svg = d3....). In the statement svg.selectAll("rect.day") , I am not sure how it appends rect for each of svg (selectAll trying to select rect.day !)

var margin = {top: 19, right: 20, bottom: 20, left: 19},
    width = 960 - margin.right - margin.left, // width
    height = 136 - margin.top - margin.bottom, // height
    cellSize = 17; // cell size

var day = d3.time.format("%w"),
    week = d3.time.format("%U"),
    percent = d3.format(".1%"),
    format = d3.time.format("%Y-%m-%d");


var svg = d3.select("#chart").selectAll("svg")
    .data(d3.range(1990, 2011))
  .enter().append("svg")
    .attr("width", width + margin.right + margin.left)
    .attr("height", height + margin.top + margin.bottom)
    .attr("class", "RdYlGn")
  .append("g")
    .attr("transform", "translate(" + (margin.left + (width - cellSize * 53) / 2) + "," + (margin.top + (height - cellSize * 7) / 2) + ")");

var rect = svg.selectAll("rect.day")
    .data(function(d) { return d3.time.days(new Date(d, 0, 1), new Date(d + 1, 0, 1)); })
  .enter().append("rect")
    .attr("class", "day")
    .attr("width", cellSize)
    .attr("height", cellSize)
    .attr("x", function(d) { return week(d) * cellSize; })
    .attr("y", function(d) { return day(d) * cellSize; })
    .datum(format);
like image 334
bsr Avatar asked Sep 06 '12 23:09

bsr


People also ask

What does selectAll do in d3?

selectAll() function in D3. js is used to select all the element that matches the specified selector string. Parameters: This function accepts single parameter HTML tag as a parameter. Return Value: This function returns the selected elements.

What is d3 js explain select () selectAll () and data () in brief?

D3 allows you to select DOM elements and manipulate them using functions provided by the library. Elements are selected with select() and selectAll() methods, which return selections. Selections have operators that allow you to modify, add, or remove elements.

What does SVG selectAll do?

svg. selectAll('rect') tells the browser to find the svg element and look inside it for any rectangles. If it finds rectangles, it returns them in a selection that is an array of elements. If it doesn't find any, it returns an empty selection, which is what will happen in this case.

What is append in d3?

append() function is used to append a new element to the HTML tag name as given in the parameters to the end of the element. If the type that is given is a function then it must be evaluated for each element that is in the selection. Syntax: selection.


1 Answers

Have you looked at Thinking With Joins? It explains pretty clearly how data joins work - essentially, in D3, you use .selectAll() to join data to a selection, and then use .enter() to append new elements as necessary. So

svg.selectAll("rect.day")
    .data( ... )
  .enter().append("rect")
    .class("day")

creates new rect elements as necessary, based on the data.

like image 120
nrabinowitz Avatar answered Sep 18 '22 01:09

nrabinowitz