D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
lilian-li
Full window
Github gist
Bar Chart Test v2
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script> <style> .axis path, .axis line { fill: black ; stroke: #000; shape-rendering: crispEdges; } .bar { fill: steelblue; } .bar:hover{fill: green;} body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } </style> </head> <body> <script> var letters = [ {letter: "A", frequency: "0.08167"}, {letter: "B", frequency: "0.07167"}, {letter: "C", frequency: "0.06167"}, {letter: "D", frequency: "0.05167"}, {letter: "E", frequency: "0.04167"}, {letter: "F", frequency: "0.03167"}, {letter: "G", frequency: "0.02167"} ]; var margin = {top: 20, right: 30, bottom: 30, left: 40}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; var x = d3.scale.ordinal() .rangeRoundBands([0, width], 0.1, 0.2); var y = d3.scale.linear() .range([height, 0]); var svg = d3.select("body").append("svg") .attr("width",width + margin.left +margin.right) .attr("height",height + margin.top +margin.bottom) var g = svg.append("g") .attr("transform","translate("+margin.left+","+margin.top+")"); x.domain(letters.map(function(d) {return d.letter})); y.domain([0, d3.max(letters, function(d) { return d.frequency; })]); g.append("g") .attr("class","x axis") .attr("transform","translate(0,"+height+")") .call(d3.svg.axis().scale(x).orient("bottom")); g.append("g") .attr("class","y axis") .attr("transform","translate(0,"+0+")") .call(d3.svg.axis().scale(y).orient("left")); g.selectAll(".bar") .data(letters) .enter().append("rect") .attr("class","bar") .attr("x",function(d) {return x(d.letter);}) .attr("width",x.rangeBand()) .attr("y",function(d) {return y(d.frequency);}) // .attr("height", function(d){return height}); .attr("height", function(d) {return height-y(d.frequency);}); </script> </body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js