Data Driven Documents
d3.select('div');
d3.selectAll('div');
d3.selectAll('.some-class').html() // Get inner html
.text('Some text') // Set text
.classed('.a-class') // Check if has class
.classed('.set-class', true) // Set class
.classed('.remove-class', false) // Remove class
.attr('href', 'google.com')
.style('font-size', '20px')
.append('h1').text(function() { // Pass a function
return getH1Text();
});
// Enter
d3.select("#bar-chart").selectAll("div.h-bar")
.data(data)
.enter()
.append("div")
.attr("class", "h-bar")
.append("span");
// Update
d3.select("#bar-chart").selectAll("div.h-bar")
.data(data)
.style("width", function (d) {
return (d * 3) + "px";
})
.select("span")
.text(function (d) {
return d;
});
// Exit
d3.select("#bar-chart").selectAll("div.h-bar")
.data(data)
.exit()
.remove();
// The data
var data = [10, 15, 30, 50, 80, 65, 55, 30, 20, 10, 8];
// The render function
function render(data) {
// Enter...
// Update...
// Exit...
}
// Call render
render(data);
var data = [
{expense: 10, category: "Retail"},
{expense: 15, category: "Gas"},
{expense: 30, category: "Retail"},
{expense: 50, category: "Dining"},
{expense: 80, category: "Gas"},
{expense: 65, category: "Retail"},
{expense: 55, category: "Gas"},
{expense: 30, category: "Dining"},
{expense: 20, category: "Retail"},
{expense: 10, category: "Dining"},
{expense: 8, category: "Gas"}
];
function render(data, category) {
// Enter (add h-bar and a span)
// Exit (remove)
// Update
d3.select("#example2 .chart").selectAll("div.h-bar")
.data(data)
.attr("class", "h-bar")
.style("width", function (d) {
return (d.expense * 5) + "px";}
)
.select("span")
.text(function (d) {
return d.category;
});
// Filter
d3.select("#example2 .chart").selectAll("div.h-bar")
.filter(function (d, i) {
return d.category == category;
})
.classed("selected", true);
}
render(data);
function select(category) {
render(data, category);
}
<button onclick="select('Retail')">Retail</button>
// Replace filter snippet with the following
if(comparator) {
d3.select("body")
.selectAll("div.h-bar")
.sort(comparator);
}
// Comparator functions
var compareByExpense = function (a, b) {
return a.expense < b.expense?-1:1;
};
var compareByCategory = function (a, b) {
return a.category < b.category?-1:1;
};
render(data);
function sort(comparator) {
render(data, comparator);
}
<button onclick="sort(compareByExpense)">Sort by Expense</button>
// Simple linear scale mapping numbers 0 through 100 // to colors #add8e6 through blue var colorScale = d3.scale.linear() .domain([0,100]) .range(["#add8e6", "blue"]);
// Data as objects
var data = [{width: 10, color: 23},{width: 15, color: 33}...];
// Update with scale
d3.select("#example4 .chart").selectAll("div.h-bar")
.data(data)
.attr("class", "h-bar")
.style("width", function(d) {
return (d.width * 5) + "px";
})
.style("background-color", function(d) {
return colorScale(d.color);
})
.select("span")
.text(function(d) {
return d.width;
});
function renderAxis(scale, i, orient) {
var axis = d3.svg.axis()
.scale(scale)
.orient(orient)
.ticks(5);
svg.append("g")
.attr("transform", function () {
if (["top", "bottom"].indexOf(orient) >= 0) {
return "translate(" + margin + "," + i * offset + ")";
} else {
return "translate(" + i * offset + ", " + margin + ")";
}
})
.call(axis);
}
function renderXGridlines() {
var lines = d3.selectAll("g.x-axis g.tick")
.select("line.grid-line")
.remove();
lines = d3.selectAll("g.x-axis g.tick")
.append("line")
.classed("grid-line", true)
lines.attr("x1", 0)
.attr("y1", 0)
.attr("x2", 0)
.attr("y2", -yAxisLength);
}
// initial yAxis declaration, scale previously established
yAxis = d3.svg.axis()
.scale(scale)
.tickSubdivide(1)
.orient("left");
// On rescale, change domain to the new max value of dataset
yAxis.scale().domain([max, 0]);
svg.select("g.y-axis")
.transition() // <-- This makes everything smooth
.duration(1000) // But it was so easy!?
.call(yAxis);