Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active July 27, 2020 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save d3indepth/1aef77d17863e603ff4e84226db5b227 to your computer and use it in GitHub Desktop.
Save d3indepth/1aef77d17863e603ff4e84226db5b227 to your computer and use it in GitHub Desktop.
scaleBand example
license: gpl-3.0
height: 250
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Band scale</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
color: #333;
}
rect {
fill: orange;
}
</style>
<body>
<svg width="700" height="240">
<g id="wrapper" transform="translate(40, 20)">
</g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var myData = [
{day : 'Mon', value: 10},
{day : 'Tue', value: 40},
{day : 'Wed', value: 30},
{day : 'Thu', value: 60},
{day : 'Fri', value: 30}
];
var bandScale = d3.scaleBand()
.domain(['Mon', 'Tue', 'Wed', 'Thu', 'Fri'])
.range([0, 200])
.paddingInner(0.05);
d3.select('#wrapper')
.selectAll('rect')
.data(myData)
.enter()
.append('rect')
.attr('y', function(d) {
return bandScale(d.day);
})
.attr('height', bandScale.bandwidth())
.attr('width', function(d) {
return d.value;
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment