D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
superstooge
Full window
Github gist
D3 chart training
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> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } .axis text { font: 10px sans-serif; } .axis line, .axis path { fill: none; stroke: #000; shape-rendering: crispEdges; } </style> </head> <body> <script> var margin = {top:20, right: 20, bottom:30, left: 50}; var width = 600 - margin.left - margin.right; var height = 500 - margin.top - margin.bottom; var data = [ {x: "A", y:20}, {x: "B", y:50}, {x: "C" ,y:80}, {x:"D", y:120}, {x: "E" ,y:550} ]; var y_scale = d3.scale.linear() .domain([0, 550]) .range([height, 0]) var x_scale = d3.scale.ordinal() .domain(data.map(function(d){return d.x})) .rangeRoundBands([0, width], .3, .2) // axes var xAxis = d3.svg.axis() .scale(x_scale) var yAxis = d3.svg.axis() .scale(y_scale) .orient("left"); var svg = d3.select("body").append("svg") .attr("width", width + margin.left + margin.right) .attr("height", height + margin.top + margin.bottom) .attr("class", "axis") .style("background-color", "#aba") var g = svg.append('g') .attr("transform", "translate("+margin.left+", "+margin.top+")") var xAxisG = svg.append('g') .attr("transform", "translate("+margin.left+", "+(height+margin.top)+")") .call(xAxis) var yAxisG = svg.append('g') .attr("transform", "translate("+margin.left+", "+margin.top+")") .call(yAxis) var bars = g.selectAll('.fava').data(data); bars.enter().append('rect') .attr({ x:function(d, i){ return x_scale(d.x)}, y: function(d, i){return y_scale(d.y)}, fill:"yellow", width: x_scale.rangeBand(), height:function(d, i){return height - y_scale(d.y)} }) </script> </body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js