D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
mph006
Full window
Github gist
Nonfarm employment change
<!DOCTYPE html> <meta charset="utf-8"> <style type="text/css"> /*css goes here*/ .header{ text-align: center; font-weight: bold; border-bottom: thin solid black; margin-left: 100px; margin-right: 100px; } svg{ border: 1px solid #f0f; } .axis text { font-size: 12px; fill: #777; } .axis path { display: none; } .axis line { stroke-width:1px; stroke: #ccc; opacity: 0.7; /* stroke-dasharray: 2px 2px;*/ } .info{ position: absolute; top:100px; left:1000px; font-size: 20px; color:#4682B4; font-weight: bold; opacity: 0; } .g-minor text{ display: none; } .g-major line{ stroke-width:2px; stroke: black; opacity: 0.7; } </style> <body> <!-- html goes here --> <h1 class="header">Change in Nonfarm Payroll - Thousands of Jobs</h1> <div class="info"> blah </div> </body> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script> <script> //JS to go here var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; var margin = {top: 20, right: 30, bottom: 50, left: 20}; var width = 1400 - margin.left - margin.right, height = 700 - margin.top - margin.bottom; 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 + ")"); var y = d3.scale.linear() .range([height, 0]); var x = d3.scale.ordinal() .rangeRoundBands([0,width],0.1); var yAxis = d3.svg.axis() .scale(y) .tickSize(-width) .tickPadding(8) .ticks(10,"+") .orient("right"); var xAxis = d3.svg.axis() .scale(x) .tickSize(-height) .tickPadding(8) .ticks(4) .tickFormat(function(d){return d.split("-")[0];}) //.tickValues([2009,2010,2011,2012]) .orient("bottom"); var barWidth = 5; function mouseover(){ var element = d3.select(this).style("fill","#4682B4"); var data = element[0][0].__data__; var info = d3.select(".info") .style("opacity",1) .text(monthNames[+data.month_year.split("-")[1]-1]+ " "+ data.month_year.split("-")[0] + " Jobs Change = "+data.jobs_change+",000"); } function mouseleave(){ var element = d3.select(this).style("fill","#D3D3D3"); var info = d3.select(".info").style("opacity",0); } d3.tsv("./jobs.tsv",function(error,data){ if(error){ alert("error!"); return;} console.log("all data",data); //type casting data.forEach(function(d){ //hack to parse the int, pretty slick d.jobs_change = +d.jobs_change; // d.month_year = new Date(d.month_year.split("-")[0], d.month_year.split("-")[1]-1); }); var jobsChange = data.map(function(d){return d.jobs_change;}); var months = data.map(function(d){return d.month_year;}); var jobsMin = d3.min(jobsChange); var jobsMax = d3.max(jobsChange); //could use d3.extent() here y.domain([jobsMin,jobsMax]); x.domain(months); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (height) + ")") .call(xAxis) .selectAll("g") .attr("class",function(d){return (d.split("-")[1]==="01")? "g-major": "g-minor";}); //d3.selectAll(".g-major text").text(function(d){return d.split("-")[0];}); svg.append("g") .attr("class", "y axis") .attr("transform", "translate("+(width-10)+",0)") .call(yAxis); var bars = svg.append("g").attr("class","bars").selectAll("rect") .data(data) .enter() .append("rect") .attr("class","bar-element") .attr("y", function(d) { return y(Math.max(0,d.jobs_change)); }) .attr("height", function(d) { return Math.abs(y(d.jobs_change)-y(0)); }) .attr("width", x.rangeBand()) .attr("x", function(d){return x(d.month_year)-(barWidth/2)+2+"px";}) .style("fill","#D3D3D3") .on("mouseover",mouseover) .on("mouseleave",mouseleave); }); </script>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js