Skip to content

Instantly share code, notes, and snippets.

@derekjwilliams
Created December 3, 2018 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekjwilliams/3213b9dbbdde1878b21e99645fdae2fe to your computer and use it in GitHub Desktop.
Save derekjwilliams/3213b9dbbdde1878b21e99645fdae2fe to your computer and use it in GitHub Desktop.
D3 Bar Chart

D3 Bar Chart

A minimal demonstration of how to create an HTML bar chart with D3. Fork this template to create your own chart.

A Pen by Mike Bostock on CodePen.

License.

<div class="chart"></div>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
var data = [4, 8, 15, 16, 23, 42];
var x = d3.scale.linear()
.domain([0, d3.max(data)])
.range([0, 420]);
d3.select(".chart")
.selectAll("div")
.data(data)
.enter().append("div")
.style("width", function(d) { return x(d) + "px"; })
.text(function(d) { return d; });
.chart div {
font: 10px sans-serif;
background-color: steelblue;
text-align: right;
padding: 3px;
margin: 1px;
color: white;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment