Skip to content

Instantly share code, notes, and snippets.

@romsson
Last active January 17, 2020 07:00
Show Gist options
  • Save romsson/4e431453beb5c92ff6eb029771b3b8d3 to your computer and use it in GitHub Desktop.
Save romsson/4e431453beb5c92ff6eb029771b3b8d3 to your computer and use it in GitHub Desktop.
bar chart (static data)
license: mit
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment