Skip to content

Instantly share code, notes, and snippets.

@jamesleesaunders
Last active August 8, 2017 20: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 jamesleesaunders/565a1c487a3aec3790df to your computer and use it in GitHub Desktop.
Save jamesleesaunders/565a1c487a3aec3790df to your computer and use it in GitHub Desktop.
D3 : Colours 3

Colours 3 using Colorbrewer.

<!DOCTYPE html>
<html>
<head>
<title>D3 : Colours 3 (using Colorbrewer)</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
<script type="text/javascript" src="http://colorbrewer2.org/export/colorbrewer.js"></script>
<style type="text/css">
#chartarea {
height: 400px;
width: 600px;
}
.bar {
display: inline-block;
width: 50px;
height: 10px;
margin-right: 2px;
background-color: teal;
}
</style>
</head>
<body>
<div id="chartarea"></div>
<script type="text/javascript">
var data = d3.range(10).map(function () {
return Math.random();
});
var colors = colorbrewer.Set2[5];
var colorIndex = d3.scale.linear()
.domain([0, 1, 2, 3, 4, 5])
.range(colors);
var svg = d3.select('#chartarea').selectAll('div')
.data(data)
.enter()
.append('div')
.attr('class', 'bar')
.style('height', function (d) {
return d * 400 + 'px';
})
.style('background-color', function (d) {
return colorIndex(Math.round(d * 5));
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment