D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ruigomesnunes
Full window
Github gist
bar chart
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; } svg { font: 10px sans-serif; } path.line { fill: none; stroke: #666; stroke-width: 1.5px; } path.area { fill: #e7e7e7; } .axis { shape-rendering: crispEdges; } .x.axis line { stroke: #fff; } .x.axis .minor { stroke-opacity: .5; } .x.axis path { display: none; } .y.axis line, .y.axis path { fill: none; stroke: #000; } </style> </head> <body> <script> var data = [{color: "#85C1E9", value: 1},{color: "#3498DB", value: 2},{color: "#2874A6", value: 3},{color: "#2874A6", value: 4},{color: "#1B4F72", value: 5},] var margin = {top:20, right:60, bottom: 30, left:60} var width = 960 - margin.left - margin.right; var height = 500 - margin.top - margin.bottom; var svg = d3.select("body").append("svg") .attr({ width: width + margin.left + margin.right, height: height + margin.top + margin.bottom }) .style("background-color", "#fafafa") var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); //Scales var y_scale = d3.scale.linear() .domain([0,(d3.max(data.map(function(d) { return d.value; })))]) .range([height,100]) var x_scale = d3.scale.ordinal() .domain(data.map(function(d) { return d.value; })) .rangeRoundBands([0, width], 0.4, 0.4); var bars = g.selectAll('rect').data(data) bars.enter().append('rect') .attr({ x: function(d, i) { return x_scale(d.value)}, y: function(d, i) { return y_scale(d.value);}, width: x_scale.rangeBand(), height: function(d, i) { return height - y_scale(d.value); }, }) .style("fill", function(d, i) { return d.color; }); bars.exit().remove(); //Axis var xAxis = d3.svg.axis() .scale(x_scale) .orient("bottom") var yAxis = d3.svg.axis() .scale(y_scale) .orient("left") g.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis) g.append("g") .attr("class", "y axis") .call(yAxis) //.attr("transform", "translate(" + width + ", 0)") </script> </body>
https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js