Skip to content

Instantly share code, notes, and snippets.

@d3indepth
Last active August 2, 2017 11: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 d3indepth/ab3336867f7469e0f4608b59b8aa125c to your computer and use it in GitHub Desktop.
Save d3indepth/ab3336867f7469e0f4608b59b8aa125c to your computer and use it in GitHub Desktop.
Ordinal scale using Brewer colour scheme
license: gpl-3.0
height: 80
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Ordinal scale</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
color: #333;
}
</style>
<body>
<svg width="800" height="60">
<g id="wrapper" transform="translate(100, 40)">
</g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script>
var myData = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
var linearScale = d3.scaleLinear()
.domain([0, 11])
.range([0, 600]);
var ordinalScale = d3.scaleOrdinal()
.domain(myData)
.range(d3.schemePaired);
d3.select('#wrapper')
.selectAll('text')
.data(myData)
.enter()
.append('text')
.attr('x', function(d, i) {
return linearScale(i);
})
.text(function(d) {
return d;
})
.style('fill', function(d) {
return ordinalScale(d);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment