Skip to content

Instantly share code, notes, and snippets.

@fogonwater
Last active December 7, 2017 00:26
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 fogonwater/13b9f809353c76cd9d2320f6b2e4c381 to your computer and use it in GitHub Desktop.
Save fogonwater/13b9f809353c76cd9d2320f6b2e4c381 to your computer and use it in GitHub Desktop.
chroma d3 tests II
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chroma-js/1.3.4/chroma.min.js"></script>
<style>
body { margin:10px;}
.swatch {
color: #444;
float: left;
font-family: sans-serif;
font-size: 0.8em;
margin: 1px;
padding: 4px;
width: 75px;
height: 70px;
}
textarea {margin-bottom: 6px;}
</style>
</head>
<body>
<textarea id="out" rows="2" cols="90"></textarea>
<div id="viz"></div>
<script>
const num_cols = 7,
data = d3.range(0,num_cols),
color = chroma
.scale([
chroma('#b893a9').saturate(2).brighten(1.5),
chroma('#a86a89').saturate(1.5).brighten(),
chroma('#6584b7').saturate(1).brighten(.5),
chroma('#66578b').saturate(1.2).brighten(),
chroma('#534483').saturate(1).brighten(.5),
chroma('#7f8082'),
chroma('#66686a')
])
//.mode('lch')
.colors(num_cols)
d3.select("#viz").selectAll("div")
.data(data)
.enter().append("div")
.style("background", function(d) {return color[d]})
.text(function(d){ return color[d]; })
.attr("class", "swatch")
const report = data.map(function(d, i){
return color[d]
})
d3.select("#out")
.text(JSON.stringify(report));
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment