This is the code for Chapter 4, Figure 16 from D3.js in Action that draws a boxplot using numerous SVG elements appended to a g element and drawn based on the datapoint bound to that g element.
Last active
March 18, 2016 03:37
-
-
Save emeeks/f41b28f062e1e4360fd6 to your computer and use it in GitHub Desktop.
Ch. 4, Fig. 16 - 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
day | min | max | median | q1 | q3 | number | |
---|---|---|---|---|---|---|---|
1 | 14 | 65 | 33 | 20 | 35 | 22 | |
2 | 25 | 73 | 25 | 25 | 30 | 170 | |
3 | 15 | 40 | 25 | 17 | 28 | 185 | |
4 | 18 | 55 | 33 | 28 | 42 | 135 | |
5 | 14 | 66 | 35 | 22 | 45 | 150 | |
6 | 22 | 70 | 34 | 28 | 42 | 170 | |
7 | 14 | 65 | 33 | 30 | 50 | 28 |
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 4 - Example 8</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; | |
} | |
line { | |
shape-rendering: crispEdges; | |
stroke: #000000; | |
} | |
line.minor { | |
stroke: #777777; | |
stroke-dasharray: 2,2; | |
} | |
path.domain { | |
fill: none; | |
stroke: black; | |
} | |
</style> | |
<body> | |
<div id="viz"> | |
<svg> | |
</svg> | |
</div> | |
</body> | |
<footer> | |
<script> | |
d3.csv("boxplot.csv", scatterplot) | |
function scatterplot(data) { | |
xScale = d3.scale.linear().domain([1,8]).range([20,470]); | |
yScale = d3.scale.linear().domain([0,100]).range([480,20]); | |
yAxis = d3.svg.axis() | |
.scale(yScale) | |
.orient("right") | |
.ticks(8) | |
.tickSize(-470) | |
.tickSubdivide(true); | |
d3.select("svg").append("g") | |
.attr("transform", "translate(470,0)") | |
.attr("id", "yAxisG") | |
.call(yAxis); | |
xAxis = d3.svg.axis() | |
.scale(xScale) | |
.orient("bottom") | |
.tickSize(-470) | |
.tickValues([1,2,3,4,5,6,7]); | |
d3.select("svg").append("g") | |
.attr("transform", "translate(0,480)") | |
.attr("id", "xAxisG") | |
.call(xAxis); | |
d3.select("svg").selectAll("circle.median") | |
.data(data) | |
.enter() | |
.append("circle") | |
.attr("class", "tweets") | |
.attr("r", 5) | |
.attr("cx", function(d) {return xScale(d.day)}) | |
.attr("cy", function(d) {return yScale(d.median)}) | |
.style("fill", "darkgray"); | |
d3.select("svg").selectAll("g.box") | |
.data(data) | |
.enter() | |
.append("g") | |
.attr("class", "box") | |
.attr("transform", function(d) {return "translate(" + xScale(d.day) +"," + yScale(d.median) + ")"}) | |
.each(function(d,i) { | |
d3.select(this) | |
.append("line") | |
.attr("class", "range") | |
.attr("x1", 0) | |
.attr("x2", 0) | |
.attr("y1", yScale(d.max) - yScale(d.median)) | |
.attr("y2", yScale(d.min) - yScale(d.median)) | |
.style("stroke", "black") | |
.style("stroke-width", "4px"); | |
d3.select(this) | |
.append("line") | |
.attr("class", "max") | |
.attr("x1", -10) | |
.attr("x2", 10) | |
.attr("y1", yScale(d.max) - yScale(d.median)) | |
.attr("y2", yScale(d.max) - yScale(d.median)) | |
.style("stroke", "black") | |
.style("stroke-width", "4px"); | |
d3.select(this) | |
.append("line") | |
.attr("class", "min") | |
.attr("x1", -10) | |
.attr("x2", 10) | |
.attr("y1", yScale(d.min) - yScale(d.median)) | |
.attr("y2", yScale(d.min) - yScale(d.median)) | |
.style("stroke", "black") | |
.style("stroke-width", "4px"); | |
d3.select(this) | |
.append("rect") | |
.attr("class", "range") | |
.attr("width", 20) | |
.attr("x", -10) | |
.attr("y", yScale(d.q3) - yScale(d.median)) | |
.attr("height", yScale(d.q1) - yScale(d.q3)) | |
.style("fill", "white") | |
.style("stroke", "black") | |
.style("stroke-width", "2px"); | |
d3.select(this) | |
.append("line") | |
.attr("x1", -10) | |
.attr("x2", 10) | |
.attr("y1", 0) | |
.attr("y2", 0) | |
.style("stroke", "darkgray") | |
.style("stroke-width", "4px"); | |
}) | |
} | |
</script> | |
</footer> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment