block by romsson 4e431453beb5c92ff6eb029771b3b8d3

bar chart (static data)

Full Screen

Built with blockbuilder.org

forked from romsson‘s block: D3 bar chart without data-binding

forked from anonymous‘s block: D3 bar chart with data-binding

forked from anonymous‘s block: D3 bar chart with data-binding

forked from romsson‘s block: bar chart (static data)

index.html

<!DOCTYPE html>
<head>
  <meta charset="utf-8">
  <script src="https://d3js.org/d3.v5.min.js"></script>
  <style>
    rect {
      fill: white;
      stroke: black;
      stroke-width: 1;
    }
  </style>
</head>

<body>
  <script>

    var margin = {top: 50, right: 30, bottom: 30, left: 30};

		var width = 480 - margin.left - margin.right,
    	height = 250 - margin.top - margin.bottom;
    
    var svg = d3.select("body").append("svg")
      .attr("width", width)
      .attr("height", height);

    var data = [22, 32, 21, 23, 10, 22, 11, 19, 30, 50, 19, 30, 50, 19];
    
    var y = d3.scaleLinear()
    	.domain([0, d3.max(data)])
    	.range([0, height]);

    var x = d3.scaleBand()
    	.domain(data)
    	.range([0, width]);
    
		var s = svg.selectAll("rect").data(data)
    	.enter()
     		.append("rect")
    
    svg.selectAll("rect")
	    .attr("width", function(d, i) { return x.bandwidth(); })
      .attr("height", function(d, i) { return y(d); })
      .attr("x", function(d, i) { return x(d); })
      .attr("y",  function(d) { return height - y(d); })

  </script>
</body>