[ Launch: d3 box plot ] 83239bdc4d09ad7fc920 by Aeon
-
-
Save Aeon/83239bdc4d09ad7fc920 to your computer and use it in GitHub Desktop.
d3 box plot
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
{"description":"d3 box plot","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"data.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true} |
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
Q1 | Q2 | Q3 | Q4 | |
---|---|---|---|---|
20000 | 15000 | 8000 | 20000 | |
9879 | 9323 | 3294 | 5629 | |
5070 | 9395 | 17633 | 5752 | |
7343 | 8675 | 12121 | 7557 | |
9136 | 5354 | 4319 | 5125 | |
7943 | 6725 | 18712 | 5116 | |
10546 | 10899 | 17270 | 5828 | |
9385 | 9365 | 13676 | 6014 | |
8669 | 8238 | 6587 | 5995 | |
4000 | 7446 | 16754 | 4892 |
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
var labels = true; // show the text labels beside individual boxplots? | |
var margin = {top: 30, right: 50, bottom: 70, left: 50}; | |
var width = 800 - margin.left - margin.right; | |
var height = 400 - margin.top - margin.bottom; | |
var min = Infinity, | |
max = -Infinity; | |
// parse in the data | |
d3.csv("./data.csv", function(error, csv) { | |
// using an array of arrays with | |
// data[n][2] | |
// where n = number of columns in the csv file | |
// data[i][0] = name of the ith column | |
// data[i][1] = array of values of ith column | |
var data = []; | |
data[0] = []; | |
data[1] = []; | |
data[2] = []; | |
data[3] = []; | |
// add more rows if your csv file has more columns | |
// add here the header of the csv file | |
data[0][0] = "Q1"; | |
data[1][0] = "Q2"; | |
data[2][0] = "Q3"; | |
data[3][0] = "Q4"; | |
// add more rows if your csv file has more columns | |
data[0][1] = []; | |
data[1][1] = []; | |
data[2][1] = []; | |
data[3][1] = []; | |
csv.forEach(function(x) { | |
var v1 = Math.floor(x.Q1), | |
v2 = Math.floor(x.Q2), | |
v3 = Math.floor(x.Q3), | |
v4 = Math.floor(x.Q4); | |
// add more variables if your csv file has more columns | |
var rowMax = Math.max(v1, Math.max(v2, Math.max(v3,v4))); | |
var rowMin = Math.min(v1, Math.min(v2, Math.min(v3,v4))); | |
data[0][1].push(v1); | |
data[1][1].push(v2); | |
data[2][1].push(v3); | |
data[3][1].push(v4); | |
// add more rows if your csv file has more columns | |
if (rowMax > max) max = rowMax; | |
if (rowMin < min) min = rowMin; | |
}); | |
var chart = d3.box() | |
.whiskers(iqr(1.5)) | |
.height(height) | |
.domain([min, max]) | |
.showLabels(labels); | |
var svg = d3.select("body").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.attr("class", "box") | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
// the x-axis | |
var x = d3.scale.ordinal() | |
.domain( data.map(function(d) { console.log(d); return d[0] } ) ) | |
.rangeRoundBands([0 , width], 0.7, 0.3); | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom"); | |
// the y-axis | |
var y = d3.scale.linear() | |
.domain([min, max]) | |
.range([height + margin.top, 0 + margin.top]); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left"); | |
// draw the boxplots | |
svg.selectAll(".box") | |
.data(data) | |
.enter().append("g") | |
.attr("transform", function(d) { return "translate(" + x(d[0]) + "," + margin.top + ")"; } ) | |
.call(chart.width(x.rangeBand())); | |
// add a title | |
svg.append("text") | |
.attr("x", (width / 2)) | |
.attr("y", 0 + (margin.top / 2)) | |
.attr("text-anchor", "middle") | |
.style("font-size", "18px") | |
//.style("text-decoration", "underline") | |
.text("Revenue 2012"); | |
// draw y axis | |
svg.append("g") | |
.attr("class", "y axis") | |
.call(yAxis) | |
.append("text") // and text1 | |
.attr("transform", "rotate(-90)") | |
.attr("y", 6) | |
.attr("dy", ".71em") | |
.style("text-anchor", "end") | |
.style("font-size", "16px") | |
.text("Revenue in €"); | |
// draw x axis | |
svg.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + (height + margin.top + 10) + ")") | |
.call(xAxis) | |
.append("text") // text label for the x axis | |
.attr("x", (width / 2) ) | |
.attr("y", 10 ) | |
.attr("dy", ".71em") | |
.style("text-anchor", "middle") | |
.style("font-size", "16px") | |
.text("Quarter"); | |
}); | |
// Returns a function to compute the interquartile range. | |
function iqr(k) { | |
return function(d, i) { | |
var q1 = d.quartiles[0], | |
q3 = d.quartiles[2], | |
iqr = (q3 - q1) * k, | |
i = -1, | |
j = d.length; | |
while (d[++i] < q1 - iqr); | |
while (d[--j] > q3 + iqr); | |
return [i, j]; | |
}; | |
} |
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
body { | |
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | |
} | |
.box { | |
font: 10px sans-serif; | |
} | |
.box line, | |
.box rect, | |
.box circle { | |
fill: steelblue; | |
stroke: #000; | |
stroke-width: 1px; | |
} | |
.box .center { | |
stroke-dasharray: 3,3; | |
} | |
.box .outlier { | |
fill: none; | |
stroke: #000; | |
} | |
.axis { | |
font: 12px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} | |
.x.axis path { | |
fill: none; | |
stroke: #000; | |
shape-rendering: crispEdges; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment