D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
manojwajekar
Full window
Github gist
Vertical_bar_chart
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .bar { fill: steelblue; } </style> <svg width="900" height="450"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 40}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom; var g = svg.append("g") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var x = d3.scaleBand().rangeRound([0, width]).padding(0.6), y = d3.scaleLinear().rangeRound([height, 0]); function draw_bar_chart () { var data = [{"name": "makespace", "price": 1.159961524}, {"name": "extraspace", "price": 2.186246689}, {"name": "privatecompanies", "price": 2.728926349}]; x.domain(data.map(function(d) { return d.name; })); y.domain([0, d3.max(data, function(d) { return d.price; })]); g.append("g") .attr("class", "axis baraxis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x)) .style("font-size", "15px"); g.append("g") .attr("class", "axis axis--y ") .style("font-size", "14px") .call(d3.axisLeft(y).ticks(10, "$")) .append("text") .attr("fill", "#000") .attr("transform", "rotate(-90)") .style("font-size", "17px") .attr("y", 9) .attr("dy", "0.9em") .attr("text-anchor", "end") .text("Price"); g.selectAll(".bar") .data(data) .enter().append("rect") .attr("class", "bar") .attr("x", function(d) { return x(d.name); }) .attr("y", function(d) { return y(d.price); }) .attr("width", x.bandwidth()) .attr("height", function(d) { return height - y(d.price); }); } draw_bar_chart(); ; </script>
https://d3js.org/d3.v4.min.js