Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```html
- <!DOCTYPE html>
- <meta charset="utf-8" />
- <!-- Load d3.js -->
- <script src="https://d3js.org/d3.v4.js"></script>
- <script src="https://rawgit.com/susielu/d3-annotation/master/d3-annotation.min.js"></script>
- <!-- Create a div where the graph will take place -->
- <div id="my_dataviz"></div>
- <script>
- // set the dimensions and margins of the graph
- var margin = { top: 10, right: 30, bottom: 30, left: 40 },
- width = 960 - margin.left - margin.right,
- height = 600 - margin.top - margin.bottom;
- var chartWidth = 700;
- var boxWidth = 100;
- // append the svg object to the body of the page
- var svg = d3
- .select("#my_dataviz")
- .append("svg")
- .attr("width", width + margin.left + margin.right)
- .attr("height", height + margin.top + margin.bottom)
- .append("g")
- .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
- var months = ["Jan", "Apr", "Jul"];
- // Actual data for chickenpox cases - Data for January, April, and July with added data points and outliers
- //These data are used to render the box plot
- var actualData = [
- [
- 956, 922, 1320, 1500, 1124, 970, 1120, 1154, 970, 1220, 1100, 980, 1050,
- 950, 1000, 1800, 1650,
- ], // Data for January
- [
- 1005, 1075, 1035, 1200, 1125, 980, 1080, 1150, 930, 1185, 1065, 955, 1000,
- 920, 980, 1500, 1420,
- ], // Data for April
- [
- 1120, 980, 1035, 1250, 1300, 1400, 1500, 1150, 950, 1000, 1045, 1280,
- 1200, 1250, 1380, 1420, 1500,
- ], // Data for July
- ];
- var rectangleColors = ["#8ebad9", "#ffbe86", "#95cf95"];
- var lineColors = ["#1f77b4", "#ff7f0e", "#2ca02c"];
- //blue, tan, red colors
- var legendColors = d3
- .scaleOrdinal()
- .domain(months)
- .range(["#8ebad9", "#ffbe86", "#95cf95"]);
- //Title
- svg
- .append("text")
- .attr("x", chartWidth / 2)
- .attr("y", 40 - margin.top / 2)
- .attr("text-anchor", "middle")
- .style("font-size", "16px")
- .style("text-decoration", "underline")
- .text("Monthly Chickenpox Cases in NYC 1931-1971");
- // Show the X scale
- var x = d3
- .scaleBand()
- .range([0, chartWidth])
- .domain(months)
- .paddingInner(1)
- .paddingOuter(0.5);
- svg
- .append("g")
- .attr("transform", "translate(40," + height + ")")
- .call(d3.axisBottom(x));
- // Show the Y scale
- var y = d3.scaleLinear().domain([400, 2000]).range([height, 80]);
- svg
- .append("g")
- .attr("transform", "translate(40," + 0 + ")")
- .call(d3.axisLeft(y));
- // Y axis label:
- svg
- .append("text")
- .attr("text-anchor", "middle")
- .attr("transform", "rotate(-90)")
- .attr("y", -margin.left + 20)
- .attr("x", -height / 2 + 20)
- .text("Number of Chickenpox Cases");
- // Loop through the data and draw the boxplots
- var boxes = svg
- .selectAll(".box")
- .data(months)
- .enter()
- .append("g")
- .attr("class", "box")
- .attr("transform", function (d) {
- return "translate(" + (x(d) + 40 - boxWidth / 2) + ",0)";
- });
- boxes.each(function (month, i) {
- var data = actualData[i].sort(d3.ascending);
- var q1 = d3.quantile(data, 0.25);
- var median = d3.quantile(data, 0.5);
- var q3 = d3.quantile(data, 0.75);
- var interQuantileRange = q3 - q1;
- var min = q1 - 1.5 * interQuantileRange;
- var max = q3 + 1.5 * interQuantileRange;
- console.log(min);
- console.log(max);
- // Show the main vertical line
- d3.select(this)
- .append("line")
- .attr("x1", boxWidth / 2)
- .attr("x2", boxWidth / 2)
- .attr("y1", y(min))
- .attr("y2", y(max))
- .attr("stroke", lineColors[i]);
- // Show the box
- d3.select(this)
- .append("rect")
- .attr("y", y(q3))
- .attr("height", y(q1) - y(q3))
- .attr("width", boxWidth)
- .attr("stroke", lineColors[i])
- .style("fill", rectangleColors[i]);
- // show median, min and max horizontal lines
- d3.select(this)
- .selectAll("toto")
- .data([min, median, max])
- .enter()
- .append("line")
- .attr("x1", 0)
- .attr("x2", boxWidth)
- .attr("y1", function (d) {
- return y(d);
- })
- .attr("y2", function (d) {
- return y(d);
- })
- .attr("stroke", lineColors[i]);
- var filteredData = data.filter(function (d) {
- return d > max || d < min;
- });
- // Annotate outliers
- d3.select(this)
- .selectAll("circle.outlier")
- .data(filteredData)
- .enter()
- .append("circle")
- .attr("class", "outlier")
- .attr("cx", function (d) {
- return boxWidth / 2;
- })
- .attr("cy", function (d) {
- return y(d);
- })
- .attr("r", 4)
- .style("fill", "none") // Transparent fill
- .attr("stroke", "#c86984"); // Color for outliers
- });
- // Legend
- var size = 20;
- svg
- .selectAll(".mydots")
- .data(months)
- .enter()
- .append("rect")
- .attr("class", "mydots")
- .attr("x", 800)
- .attr("y", function (d, i) {
- return 100 + i * (size + 5);
- })
- .attr("width", size)
- .attr("height", size)
- .style("fill", function (d) {
- return legendColors(d);
- });
- svg
- .selectAll(".mylabels")
- .data(months)
- .enter()
- .append("text")
- .attr("class", "mylabels")
- .attr("x", 800 + size * 1.2)
- .attr("y", function (d, i) {
- return 100 + i * (size + 5) + size / 2;
- })
- .style("fill", function (d) {
- return legendColors(d);
- })
- .text(function (d) {
- return d;
- })
- .attr("text-anchor", "left")
- .style("alignment-baseline", "middle");
- </script>
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement