Skip to content

Instantly share code, notes, and snippets.

@Vintharas
Last active August 22, 2019 09:16
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 Vintharas/6629362515ffba8dd7d40bf3cf70f9a2 to your computer and use it in GitHub Desktop.
Save Vintharas/6629362515ffba8dd7d40bf3cf70f9a2 to your computer and use it in GitHub Desktop.
Create a simple bar chart
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
<svg width=900 height=500></svg>
</head>
<body>
<script>
// create a bar chart to get an understanding of how many cinnamon buns
// you eat throuhgout a week
// => SVG in MDN: https://developer.mozilla.org/en-US/docs/Web/SVG
// => D3 API: https://github.com/d3/d3/blob/master/API.md
// cinnamon buns
const kanelbullarThisWeek = [
{day: 'Monday', buns: 10},
{day: 'Tuesday', buns: 20},
{day: 'Wednesday', buns: 10},
{day: 'Thursday', buns: 61},
{day: 'Friday', buns: 1},
{day: 'Saturday', buns: 12},
{day: 'Sunday', buns: 2}];
const svg = d3.select("svg");
svg
.selectAll("rect")
.data(kanelbullarThisWeek)
.enter()
.append('rect')
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment