-
-
Save jljouannic/cb1a9f7a92b39e2155cc42834a736064 to your computer and use it in GitHub Desktop.
4.0 Color Scales - Sequential
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<style> | |
body { | |
font: 12px sans-serif; | |
} | |
div { | |
max-width: 950px; | |
} | |
</style> | |
<div class="container"></div> | |
<div class="sequentialButtons">Sequential Color Scales: </div> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<script src="//d3js.org/d3-scale-chromatic.v0.3.min.js"></script> | |
<script> | |
//JS to go here | |
var interpolators = [ | |
// These are from d3-scale. | |
"Viridis", | |
"Inferno", | |
"Magma", | |
"Plasma", | |
"Warm", | |
"Cool", | |
"Rainbow", | |
"CubehelixDefault", | |
// These are from d3-scale-chromatic | |
"Blues", | |
"Greens", | |
"Greys", | |
"Oranges", | |
"Purples", | |
"Reds", | |
"BuGn", | |
"BuPu", | |
"GnBu", | |
"OrRd", | |
"PuBuGn", | |
"PuBu", | |
"PuRd", | |
"RdPu", | |
"YlGnBu", | |
"YlGn", | |
"YlOrBr", | |
"YlOrRd" | |
]; | |
var width = 960, | |
height = 500; | |
var svg = d3.select("body") | |
.append("svg") | |
.attr("width", width) | |
.attr("height", height) | |
.append("g") | |
var colorScale = d3.scaleQuantize() | |
.domain([0, width]).range(Array.from({length: 9}).map((_, i) => d3.interpolateViridis(i * 1 / 9))) | |
var bars = svg.selectAll(".bars") | |
.data(d3.range(width), function (d) { return d; }) | |
.enter().append("rect") | |
.attr("class", "bars") | |
.attr("x", function (d, i) { return i; }) | |
.attr("y", 0) | |
.attr("height", height) | |
.attr("width", 1) | |
.style("fill", function (d, i) { return colorScale(d); }) | |
var sequentialButtons = d3.select(".sequentialButtons") | |
.selectAll("button") | |
.data(interpolators) | |
.enter().append("button") | |
.text(function (d) { return d; }) | |
.on("click", function (buttonValue) { | |
var colorScale = d3.scaleQuantize() | |
.domain([0, width]).range(Array.from({length: 9}).map((_, i) => d3[`interpolate${buttonValue}`](i * 1 / 9))) | |
svg.selectAll(".bars") | |
.transition() | |
.style("fill", function (d) { return colorScale(d); }); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment