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/55f3253d6232f682708a3db0da207e65 to your computer and use it in GitHub Desktop.
Save d3indepth/55f3253d6232f682708a3db0da207e65 to your computer and use it in GitHub Desktop.
Multi-segment scale
license: gpl-3.0
height: 90
border: no
<!DOCTYPE html>
<meta charset="utf-8">
<head>
<title>Multi-segment domain and range</title>
</head>
<style>
body {
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-size: 14px;
color: #333;
}
</style>
<body>
<svg width="700" height="80">
<g id="wrapper" transform="translate(40, 40)">
</g>
</svg>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.2/d3.min.js"></script>
<script>
var xScale = d3.scaleLinear()
.domain([-10, 10])
.range([0, 600]);
var linearScale = d3.scaleLinear()
.domain([-10, 0, 10])
.range(['red', '#ddd', 'blue']);
var myData = [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10];
d3.select('#wrapper')
.selectAll('circle')
.data(myData)
.enter()
.append('circle')
.attr('r', 10)
.attr('cx', function(d) {
return xScale(d);
})
.style('fill', function(d) {
return linearScale(d);
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment