Last active
July 13, 2018 03:59
-
-
Save xiaoiver/f43c6156b3a225ceb5c21333f03a4f24 to your computer and use it in GitHub Desktop.
D3 in Action - Boxplot Demo
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
| svg { | |
| height: 500px; | |
| width: 500px; | |
| border: 1px solid gray; | |
| } | |
| path.domain { | |
| fill: none; | |
| } | |
| #xAxisG .tick line { | |
| stroke: black; | |
| stroke-dasharray: 2,2; | |
| } | |
| #yAxisG .tick line { | |
| stroke: black; | |
| stroke-dasharray: 10,5; | |
| } |
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
| window.onload = function () { | |
| const data = d3.csv.parse(`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`); | |
| const xScale = d3.scale.linear().domain([0, 8]).range([20,470]); | |
| // padding 20px | |
| const yScale = d3.scale.linear().domain([0,100]).range([480,20]); | |
| const 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); | |
| const yAxis = d3.svg.axis() | |
| .scale(yScale) | |
| .orient("right") | |
| .ticks(8) | |
| .tickSize(-470); | |
| d3.select("svg").append("g") | |
| .attr("transform", "translate(470,0)") | |
| .attr("id", "yAxisG") | |
| .call(yAxis); | |
| 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", d => `translate(${xScale(d.day)}, ${yScale(d.median)})`) | |
| .each(function(d,i) { | |
| // range | |
| 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"); | |
| // top bar | |
| 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("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"); | |
| d3.select(this) | |
| .append("line") | |
| .attr("x1", -10) | |
| .attr("x2", 10) | |
| .attr("y1", 0) | |
| .attr("y2", 0) | |
| .style("stroke", "darkgray") | |
| .style("stroke-width", "4px"); | |
| }) | |
| } |
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
| <html> | |
| <head> | |
| <title>D3 in Action - Boxplot Demo</title> | |
| <link rel="stylesheet" type="text/css" href="./boxplot.css" /> | |
| </head> | |
| <body> | |
| <svg></svg> | |
| <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js"></script> | |
| <script src="./boxplot.js"></script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment