D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
atmccann
Full window
Github gist
bar chart transitions (can you tell i like bar charts?)
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; position: relative; width: 960px; } path { stroke: steelblue; stroke-width: 2; fill: none; } .axis path, .axis line { fill: none; stroke: grey; stroke-width: 1; shape-rendering: crispEdges; } .axis text { font: 10px sans-serif; } /*.axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; }*/ .bar { fill: steelblue; /*fill-opacity: .9;*/ } label { position: absolute; top: 10px; right: 10px; } </style> <div id="option"> <select type='select' onchange='updateData(value);' style='color:steelblue;font-size:1.1em;'> <option value='year_one' name='2002' selected='true' >2002</option> <option value='year_two' name='2003'>2003</option> <option value='year_three' name='2004'>2004</option> <option value='year_four' name='2005'>2005</option> <option value='year_five' name='2006'>2006</option> <option value='year_six' name='2007'>2007</option> <option value='year_seven' name='2008'>2008</option> <option value='year_eight' name='2009'>2009</option> <option value='year_nine' name='2010'>2010</option> <option value='year_ten' name='2011'>2011</option> <option value='year_eleven' name='2012'>2012</option> </select> </div> <script src="https://d3js.org/d3.v3.min.js"></script> <script> var margin = {top: 20, right: 20, bottom: 30, left: 40}, width = 560 - margin.left - margin.right, height = 300 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], 0.1); var y = d3.scale.linear() .range([height, 0]); var xAxis = d3.svg.axis() .scale(x) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); 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 + ")"); //create line of values var valueline = d3.svg.line() .x(function(d) { return x(d.network); }) .y(function(d) { return y(d.variable); }); var json d3.json("data.json", function(error, result){ json = result; var data = []; json.forEach(function(d){ data.push({variable: +d['year_one'], network: d['network']}); }); x.domain(data.map(function(d) { return d.network; })); y.domain([d3.min(data, function(d) { return d.variable; }), d3.max(data, function(d) { return d.variable; })]); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) .append("text") .attr("y", 26) .attr("dx", "50.71em") .style("text-anchor", "end") .text("network"); svg.append("g") .attr("class", "y axis") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("viewers"); svg.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.network); }) .attr("width", x.rangeBand()) .attr("y", function(d) { return y(d.variable); }) .attr("height", function(d) { return height - y(d.variable); }); }); //update data from combobox onChange function updateData(value) { var data = []; json.forEach(function(d){ data.push({variable: +d[value], network: d['network']}); }); x.domain(data.map(function(d) { return d.network; })); y.domain([d3.min(data, function(d) { return d.variable; }), d3.max(data, function(d) { return d.variable; })]); // Select the section we want to apply our changes to var trans = d3.select("body").transition(); // Make the changes var transition = svg.transition().duration(750), delay = function(d, i) { return i * 50; }; svg.selectAll(".bar") .data(data) .transition().duration(750) .attr("x", function(d) { return x(d.network); }) .attr("y", function(d) { return y(d.variable); }) .attr("height", function(d) { return height - y(d.variable); }); transition.select(".y.axis") // change the y axis .call(yAxis); }; </script> </html>
Modified
http://d3js.org/d3.v3.min.js
to a secure url
https://d3js.org/d3.v3.min.js