Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active March 3, 2020 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save d3indepth/d22a326dc44b2d47d9f18815fbccf178 to your computer and use it in GitHub Desktop.
Save d3indepth/d22a326dc44b2d47d9f18815fbccf178 to your computer and use it in GitHub Desktop.
scaleQuantize example
license: gpl-3.0
height: 60
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Quantize scale</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
color: #333;
}
</style>
<body>
<svg width="700" height="40">
<g id="wrapper" transform="translate(40,10)">
</g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var linearScale = d3.scaleLinear()
.domain([0, 100])
.range([0, 600]);
var quantizeScale = d3.scaleQuantize()
.domain([0, 100])
.range(['lightblue', 'orange', 'lightgreen', 'pink']);
var myData = d3.range(0, 100, 1);
d3.select('#wrapper')
.selectAll('rect')
.data(myData)
.enter()
.append('rect')
.attr('x', function(d) {
return linearScale(d);
})
.attr('width', 5)
.attr('height', 30)
.style('fill', function(d) {
return quantizeScale(d);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment