A basic SVG bar chart. Part of the Let’s Make a Bar Chart tutorial.
Last active
August 9, 2018 20:27
-
-
Save mbostock/7331275 to your computer and use it in GitHub Desktop.
Bar Chart IIb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: gpl-3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
.chart rect { | |
fill: steelblue; | |
} | |
.chart text { | |
fill: white; | |
font: 10px sans-serif; | |
text-anchor: end; | |
} | |
</style> | |
<svg class="chart"></svg> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var data = [4, 8, 15, 16, 23, 42]; | |
var width = 420, | |
barHeight = 20; | |
var x = d3.scale.linear() | |
.domain([0, d3.max(data)]) | |
.range([0, width]); | |
var chart = d3.select(".chart") | |
.attr("width", width) | |
.attr("height", barHeight * data.length); | |
var bar = chart.selectAll("g") | |
.data(data) | |
.enter().append("g") | |
.attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; }); | |
bar.append("rect") | |
.attr("width", x) | |
.attr("height", barHeight - 1); | |
bar.append("text") | |
.attr("x", function(d) { return x(d) - 3; }) | |
.attr("y", barHeight / 2) | |
.attr("dy", ".35em") | |
.text(function(d) { return d; }); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
line 39 should be also a value translated via function?