D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
chiaochi
Full window
Github gist
Oscar Winners with Axis
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Oscar Winners</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; } h1 { color: orange; } svg { background-color: white; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .text { font-family: sans-serif; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .y.axis path, .y.axis line { opacity: 0; } rect:hover { fill: orange; } </style> </head> <body> <h1>Academy Awards Winners</h1> <p>A list of Academy Awards winners from 1987 to 2004. Source: <a href="https://www.boxofficemojo.com/oscar/">Box Office Mojo</a></p> <script type="text/javascript"> //All variables var w = 800, h = 800, margin = {top:0, right:50, bottom:50, left:50} body = d3.select("body") svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h), barX = 150, barSpacing = 20, labelSpacing = 10 var widthScale = d3.scale.linear() .range([0, w - barX - margin.right - margin.left]); var heightScale = d3.scale.ordinal() .rangeRoundBands([margin.top, h - margin.top - margin.bottom],0.1); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); //Loading Data and Render d3.csv("Oscar Winner.csv", function(data) { svg.selectAll("g") .data(data) .enter() .append("g") var barGroup = d3.selectAll("g") //Add the domain to the scale. Now that the data is loaded, we can widthScale.domain([0, d3.max(data, function(d) { return +d.BoxOfficeMM/1000000.0})]); heightScale.domain(data.map(function(d) {return d.Picture; })); //Drawing the bars barGroup.append("rect") .attr('width',0) .attr("fill","#fff") .transition() .duration(1000) .attr({ 'x':margin.left+barX, 'y':function(d, i) { return heightScale(d.Picture) + margin.top;}, 'width': function(d) {return widthScale(d.BoxOfficeMM/1000000);}, 'height': heightScale.rangeBand(), 'stroke':"#ccc", 'fill':"steelblue" }) //Drawing the Y-axis svg.append("g") .attr("class","y axis") .attr("transform", "translate(" + (barX+margin.left) + "," + margin.top + ")") .attr("font-size",12) .call(yAxis); //Drawing the X-axis svg.append("g") .attr("class","x axis") .attr("transform", "translate(" + (margin.left+barX) + "," + (h - margin.bottom) + ")") .attr("font-size",12) .call(xAxis); //Drawing the X-axis label svg.append("text") .attr("x", barX+(w-barX-margin.right)/2) .attr("y", h-margin.bottom/4) .attr("font-size",12) .text("Box Office ($ Millions)"); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js