This is the code for Chapter 2, Figure 18 from D3.js in Action that creates rectangles with height based on the data bound to the selection. Rectangles are scaled to fit the small drawing area by utilizing d3.scale.linear() set as a polylinear scale, meaning it has multiple stops ([0,100,1000,24500]) that are mapped to different height values ([0,50,75,100]). As a result, the wide range of values in the data array can be visually represented on the same bar chart, though without an axis this technique would be very hard to understand.
Last active
March 18, 2016 15:13
-
-
Save emeeks/e1d488a49c5bb7fb8957 to your computer and use it in GitHub Desktop.
Ch. 2, Fig. 18 - D3.js in Action
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
<html> | |
<head> | |
<title>D3 in Action Chapter 2 - Example 7</title> | |
<meta charset="utf-8" /> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script> | |
</head> | |
<style> | |
svg { | |
height: 500px; | |
width: 500px; | |
border: 1px solid gray; | |
} | |
</style> | |
<body> | |
<div> | |
<svg> | |
</svg> | |
</div> | |
</body> | |
<footer> | |
<script> | |
var yScale = d3.scale.linear().domain([0,100,1000,24500]).range([0,50,75,100]); | |
d3.select("svg") | |
.selectAll("rect") | |
.data([14, 68, 24500, 430, 19, 1000, 5555]) | |
.enter() | |
.append("rect") | |
.attr("width", 10) | |
.attr("height", function(d) {return yScale(d)}) | |
.style("fill", "blue") | |
.style("stroke", "red") | |
.style("stroke-width", "1px") | |
.style("opacity", .25) | |
.attr("x", function(d,i) {return i * 10}) | |
.attr("y", function(d) {return 100 - yScale(d)}); | |
</script> | |
</footer> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment