D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jnathaniel
Full window
Github gist
ChartExperiment
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <head> <style> body { font-size: 10pt; font-family: Arial; } .bar { fill: rgb(27,54,104); } .bar:hover { fill: rgb(98,167,68); } .axis--x path { display: none; } .axis--x { font-size:12pt; font-weight:bold; font-family: Arial; } .axis--y { font-size:10pt; font-weight:normal; font-family: Arial; } </style> </head> <body> <svg width="860" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 40}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom; var x = d3.scaleBand().rangeRound([0, width]).padding(0.1), y = d3.scaleLinear().rangeRound([height, 0]); var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); d3.tsv("data.tsv", function(d) { d.reach = +d.reach; return d; }, function(error, data) { if (error) throw error; x.domain(data.map(function(d) { return d.demo; })); y.domain([0, d3.max(data, function(d) { return d.reach; })]); g.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)); g.append("g") .attr("class", "axis axis--y") .call(d3.axisLeft(y).ticks(10, "%")) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "0.71em") .attr("text-anchor", "end") .text("Reach"); g.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.demo); }) .attr("y", function(d) { return y(d.reach); }) .attr("width", x.bandwidth()) .attr("height", function(d) { return height - y(d.reach); }); }); </script> <script> var dispatch = d3.dispatch("load", "statechange"); var groups = [ "A18-34", "A35-49", "A50+" ]; d3.csv("data.csv", type, function(error, states) { if (error) throw error; var stateById = d3.map(); states.forEach(function(d) { stateById.set(d.id, d); }); dispatch.call("load", this, stateById); dispatch.call("statechange", this, stateById.get("CA")); }); // A drop-down menu for selecting a state; uses the "menu" namespace. dispatch.on("load.menu", function(stateById) { var select = d3.select("body") .append("div") .append("select") .on("change", function() { dispatch.call("statechange", this, stateById.get(this.value)); }); select.selectAll("option") .data(stateById.values()) .enter().append("option") .attr("value", function(d) { return d.id; }) .text(function(d) { return d.id; }); dispatch.on("statechange.menu", function(state) { select.property("value", state.id); }); }); // A bar chart to show total population; uses the "bar" namespace. dispatch.on("load.bar", function(stateById) { var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 80 - margin.left - margin.right, height = 460 - margin.top - margin.bottom; var y = d3.scaleLinear() .domain([0, .5]) .rangeRound([height, 0]) .nice(); var yAxis = d3.axisLeft(y) .tickFormat(d3.format(".0%")); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); svg.append("g") .attr("class", "y axis") .call(yAxis); var rect = svg.append("rect") .attr("x", 4) .attr("width", width - 4) .attr("y", height) .attr("height", 0) .style("fill", "#aaa"); dispatch.on("statechange.bar", function(d) { rect.transition() .attr("y", y(d.Reach)) .attr("height", y(0) - y(d.Reach)); }); }); // A pie chart to show population by age group; uses the "pie" namespace. dispatch.on("load.pie", function(stateById) { var width = 600, height = 460, radius = Math.min(width, height) / 2; var color = d3.scaleOrdinal() .domain(groups) .range(["#98abc5", "#8a89a6", "#7b6888", "#6b486b", "#a05d56", "#d0743c", "#ff8c00"]); var arc = d3.arc() .outerRadius(radius - 10) .innerRadius(radius - 70); var pie = d3.pie() .sort(null); var svg = d3.select("body").append("svg") .attr("width", width) .attr("height", height) .append("g") .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")"); var path = svg.selectAll("path") .data(groups) .enter().append("path") .style("fill", color) .each(function() { this._current = {startAngle: 0, endAngle: 0}; }); dispatch.on("statechange.pie", function(d) { path.data(pie.value(function(g) { return d[g]; })(groups)).transition() .attrTween("d", function(d) { var interpolate = d3.interpolate(this._current, d); this._current = interpolate(0); return function(t) { return arc(interpolate(t)); }; }); }); }); // Coerce population counts to numbers and compute total per state. function type(d) { d.total = d3.sum(groups, function(k) { return d[k] = +d[k]; }); return d; } </script> </body>
https://d3js.org/d3.v4.min.js