Last active
March 8, 2017 11:36
-
-
Save netzwerg/7908ec2045f27f95e65a9d517a9c3140 to your computer and use it in GitHub Desktop.
D3.js Linear Scale
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
license: apache-2.0 | |
border: no |
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 lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Linear Scale</title> | |
<style> | |
body { | |
font: 10px sans-serif; | |
} | |
h2 { | |
margin-top: 0; | |
color: grey; | |
} | |
.chart { | |
background-color: lightgrey; | |
padding: 30px; | |
} | |
</style> | |
</head> | |
<body> | |
<h2>Continuous Input Domain → Continuous Output Range</h2> | |
<svg class="chart"></svg> | |
<script src="https://d3js.org/d3.v4.js"></script> | |
<script> | |
const chartWidth = 50; | |
const chartHeight = 300; | |
const data = [30, 20, 10, 200]; | |
const yExtent = d3.extent(data); // [min, max] | |
const yScale = d3.scaleLinear() | |
.domain(yExtent) | |
.range([chartHeight, 0]); // y-down | |
const svg = d3.select(".chart") | |
.attr("width", chartWidth) | |
.attr("height", chartHeight) | |
.append("g"); | |
svg.selectAll("circle") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("cx", chartWidth / 2) | |
.attr("cy", yScale) // d => yScale(d) | |
.attr("r", 20) | |
.style("fill", "steelblue") | |
.style("stroke", "lightsteelblue"); | |
const yAxis = d3.axisLeft(yScale); | |
svg.call(yAxis); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment