Skip to content

Instantly share code, notes, and snippets.

@netzwerg
Last active March 8, 2017 11:42
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 netzwerg/39cc14803c36404fdaaed7251fdae834 to your computer and use it in GitHub Desktop.
Save netzwerg/39cc14803c36404fdaaed7251fdae834 to your computer and use it in GitHub Desktop.
D3.js Ordinal Scale
license: apache-2.0
border: no
<!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 &rarr; 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