### Example21 - join by key
Built with blockbuilder.org
forked from jjelosua's block: d3intro_ex21
forked from jjelosua's block: d3intro_exercise_data_w_key_solved
### Example21 - join by key
Built with blockbuilder.org
forked from jjelosua's block: d3intro_ex21
forked from jjelosua's block: d3intro_exercise_data_w_key_solved
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Intro d3js examples</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
p:hover { | |
cursor: pointer; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Intro d3js - exercise binding with key</h1> | |
<p id="update">Click here to update data</p> | |
<!--<p> previous paragraph</p>--> | |
<script type="text/javascript"> | |
//Width and height | |
var active2015 = true; | |
var w = 600; | |
var h = 250; | |
var offset = 10; | |
var data2015 = [ { product: "camisas", value: 5 }, | |
{ product: "pantalones", value: 10 }]; | |
var data2016 = [{product: "camisas", value: 10}, | |
{product: "camisetas", value:8}, | |
{product: "ropa interior", value:10}]; | |
var data = active2015 ? data2015 : data2016; | |
var xScale = d3.scale.ordinal() | |
.domain(d3.range(data.length)) | |
.rangeRoundBands([0, w], 0.05); | |
var yScale = d3.scale.linear() | |
.domain([0, d3.max(data, function(d) { return d.value; })]) | |
.range([h, 0]); | |
//Define key function, to be used when binding data | |
var key_func = function(d) { | |
return d.product; | |
}; | |
//Create SVG element | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", w) | |
.attr("height", h); | |
//Create bars | |
svg.selectAll("rect") | |
.data(data2015, key_func) | |
.enter() | |
.append("rect") | |
.attr("x", function(d, i) { | |
return xScale(i); | |
}) | |
.attr("y", function(d) { | |
return yScale(d.value); | |
}) | |
.attr("width", xScale.rangeBand()) | |
.attr("height", function(d) { | |
return h - yScale(d.value); | |
}) | |
.attr("fill", function(d) { | |
return "rgb(0, 0, " + (d.value * 10) + ")"; | |
}); | |
//Create labels | |
svg.selectAll("text") | |
.data(data, key_func) | |
.enter() | |
.append("text") | |
.text(function(d) { | |
return d.product + ": " + d.value; | |
}) | |
.attr("text-anchor", "middle") | |
.attr("x", function(d, i) { | |
return xScale(i) + xScale.rangeBand() / 2; | |
}) | |
.attr("y", function(d) { | |
return yScale(d.value) + offset; | |
}) | |
.attr("font-family", "sans-serif") | |
.attr("font-size", "11px") | |
.attr("fill", "white"); | |
//On click, update with new data | |
d3.select("p#update") | |
.on("click", function() { | |
// Toggle dataset | |
active2015 = !active2015; | |
var data = active2015 ? data2015 : data2016; | |
//Update scale domains | |
xScale.domain(d3.range(data.length)); | |
yScale.domain([0, d3.max(data, function(d) { return d.value; })]); | |
//Select… | |
var bars = svg.selectAll("rect") | |
.data(data, key_func); | |
//Enter… | |
bars.enter() | |
.append("rect") | |
.attr("x", w) | |
.attr("y", function(d) { | |
return yScale(d.value); | |
}) | |
.attr("width", xScale.rangeBand()) | |
.attr("height", function(d) { | |
return h - yScale(d.value); | |
}) | |
.attr("fill", function(d) { | |
return "rgb(0, 0, " + (d.value * 10) + ")"; | |
}); | |
//Update… | |
bars.transition() | |
.duration(500) | |
.attr("x", function(d, i) { | |
return xScale(i); | |
}) | |
.attr("y", function(d) { | |
return yScale(d.value); | |
}) | |
.attr("width", xScale.rangeBand()) | |
.attr("height", function(d) { | |
return h - yScale(d.value); | |
}); | |
//Exit… | |
bars.exit() | |
.transition() | |
.duration(500) | |
.style("opacity", 0) | |
.remove(); | |
//Update all labels | |
//Select… | |
var labels = svg.selectAll("text") | |
.data(data, key_func); | |
//Enter… | |
labels.enter() | |
.append("text") | |
.text(function(d) { | |
return d.product + ": " + d.value; | |
}) | |
.attr("text-anchor", "middle") | |
.attr("x", w) | |
.attr("y", function(d) { | |
return yScale(d.value) + offset; | |
}) | |
.attr("font-family", "sans-serif") | |
.attr("font-size", "11px") | |
.attr("fill", "white"); | |
//Update… | |
labels.transition() | |
.duration(500) | |
.attr("x", function(d, i) { | |
return xScale(i) + xScale.rangeBand() / 2; | |
}) | |
.attr("y", function(d) { | |
return yScale(d.value) + offset; | |
}) | |
.text(function(d) { | |
return d.product + ": " + d.value; | |
}); | |
//Exit… | |
labels.exit() | |
.transition() | |
.duration(500) | |
.style("opacity", 0) | |
.remove(); | |
}); | |
</script> | |
</body> | |
</html> |