Last active
March 8, 2017 11:42
-
-
Save netzwerg/39cc14803c36404fdaaed7251fdae834 to your computer and use it in GitHub Desktop.
D3.js Ordinal Scale
This file contains 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
license: apache-2.0 | |
border: no |
This file contains 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 lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Ordinal Scales</title> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
h2 { | |
margin-top: 0; | |
color: grey; | |
} | |
div { | |
height: 50px; | |
font-size: x-large; | |
text-align: right; | |
color: white; | |
padding: 10px; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>Discrete Input Domain → Discrete Output Range</h2> | |
<script src="https://d3js.org/d3.v4.js"></script> | |
<script> | |
const data = ["I", "II", "III"]; | |
const nameScale = d3.scaleOrdinal() | |
.domain(data) | |
.range(["Jan", "Feb", "Mar"]); | |
const widthScale = d3.scaleOrdinal() | |
.domain(data) | |
.range([300, 100, 900]); | |
const color = d3.scaleOrdinal(d3.schemeCategory10); | |
d3.select("body") | |
.selectAll("div") | |
.data(data) | |
.enter() | |
.append("div") | |
.text(nameScale) | |
.style("width", d => widthScale(d) + "px") | |
.style("background-color", color); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment