<html> <head><p>Qingdao daily high and low temperature of Jun, Jul and Aug, 2020</p></head> <body> <meta charset = "utf-8"> <script src = "https://d3js.org/d3.v4.js"></script> <div id = "qdtempera"></div> <script> var p = d3.select("body").selectAll("p"); p.style("color","#6d6875").style("font-size","24px");
var margin = {top: 10, right: 30, bottom: 30, left: 40}, width = 460 - margin.left - margin.right, height = 400 - margin.top - margin.bottom;
var svg = d3.select("#qdtempera") .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 + ")");
svg.append("text") .attr("x", 6) .attr("y", 0) .text("Temperature / ℃") .style("font-size", "10px") .attr("alignment-baseline","middle")
d3.csv("https://raw.githubusercontent.com/Co10/d3_files/master/CSV_files/02/qingdao_temperature.csv", function(data) {
var sumstat = d3.nest() .key(function(d) { return d.month; }) .rollup(function(d) { q1 = d3.quantile(d.map(function(g) { return g.high;}).sort(d3.ascending),.25) median = d3.quantile(d.map(function(g) { return g.high;}).sort(d3.ascending),.5) q3 = d3.quantile(d.map(function(g) { return g.high;}).sort(d3.ascending),.75) interQuantileRange = q3 - q1 min = q1 - 1.5 * interQuantileRange; max = q3 + 1.5 * interQuantileRange; return({q1: q1, median: median, q3: q3, interQuantileRange: interQuantileRange, min: min, max: max}) }) .entries(data) var sumstat2 = d3.nest() .key(function(d) { return d.month; }) .rollup(function(d) { q1 = d3.quantile(d.map(function(g) { return g.low;}).sort(d3.ascending),.25) median = d3.quantile(d.map(function(g) { return g.low;}).sort(d3.ascending),.5) q3 = d3.quantile(d.map(function(g) { return g.low;}).sort(d3.ascending),.75) interQuantileRange = q3 - q1 min = q1 - 1.5 * interQuantileRange; max = q3 + 1.5 * interQuantileRange; return({q1: q1, median: median, q3: q3, interQuantileRange: interQuantileRange, min: min, max: max}) }) .entries(data)
var color1 = ["#e85d04", "#f48c06", "#faa307", "#ffba08"]; var color2 = ["#00b4d8", "#48cae4", "#90e0ef", "#ade8f4"];
var x = d3.scaleBand() .range([0, width]) .domain(["Jun", "Jul", "Aug"]) .paddingInner(1) .paddingOuter(.5) svg.append("g") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x))
var y = d3.scaleLinear() .domain([13,33]) .range([height, 0]) svg.append("g").call(d3.axisLeft(y))
var fillopa = 0.8;
svg.selectAll("vertLines") .data(sumstat) .enter() .append("line") .attr("x1", function(d){return(x(d.key))}) .attr("x2", function(d){return(x(d.key))}) .attr("y1", function(d){return(y(d.value.min))}) .attr("y2", function(d){return(y(d.value.max))}) .attr("fill-opacity",fillopa) .attr("stroke", color1[1]) .style("stroke-dasharray", ("4, 4"))
var s2offset = 20; var s20 = svg.selectAll("vertLines") .data(sumstat2) .enter() .append("line") .attr("x1", function(d){return(x(d.key)+s2offset)}) .attr("x2", function(d){return(x(d.key)+s2offset)}) .attr("y1", function(d){return(y(d.value.min))}) .attr("y2", function(d){return(y(d.value.max))}) .attr("fill-opacity",fillopa) .attr("stroke", color2[1]) .style("stroke-dasharray", ("4, 4"))
var boxWidth = 100; svg.selectAll("boxes") .data(sumstat) .enter() .append("rect") .attr("x", function(d){return(x(d.key)-boxWidth/2)}) .attr("y", function(d){return(y(d.value.q3))}) .attr("height", function(d){return(y(d.value.q1)-y(d.value.q3))}) .attr("width", boxWidth ) .attr("fill-opacity",fillopa) .attr("stroke", color1[1]) .attr("fill-opacity",fillopa) .style("fill", color1[0]);
var s21 = svg.selectAll("boxes") .data(sumstat2) .enter() .append("rect") .attr("x", function(d){return(x(d.key)-boxWidth/2 + s2offset)}) .attr("y", function(d){return(y(d.value.q3))}) .attr("height", function(d){return(y(d.value.q1)-y(d.value.q3))}) .attr("width", boxWidth ) .attr("fill-opacity",fillopa) .attr("stroke", color2[1]) .attr("fill-opacity",fillopa) .style("fill", color2[0]);
svg.selectAll("medianLines") .data(sumstat) .enter() .append("line") .attr("x1", function(d){return(x(d.key)-boxWidth/2) }) .attr("x2", function(d){return(x(d.key)+boxWidth/2) }) .attr("y1", function(d){return(y(d.value.median))}) .attr("y2", function(d){return(y(d.value.median))}) .attr("fill-opacity",fillopa) .attr("stroke", color1[2]) .style("width", 80);
var s22 = svg.selectAll("medianLines") .data(sumstat2) .enter() .append("line") .attr("x1", function(d){return(x(d.key)-boxWidth/2+s2offset) }) .attr("x2", function(d){return(x(d.key)+boxWidth/2+s2offset) }) .attr("y1", function(d){return(y(d.value.median))}) .attr("y2", function(d){return(y(d.value.median))}) .attr("fill-opacity",fillopa) .attr("stroke", color2[2]) .style("width", 80);
var jitterWidth = 60; svg.selectAll("indPoints") .data(data) .enter() .append("circle") .attr("cx", function(d){return(x(d.month) - jitterWidth/2 + Math.random()*jitterWidth )}) .attr("cy", function(d){return(y(d.high))}) .attr("r", 4) .attr("fill-opacity",fillopa) .style("fill", color1[3]);
var s23 = svg.selectAll("indPoints") .data(data) .enter() .append("circle") .attr("cx", function(d){return(x(d.month) - jitterWidth/2 + Math.random()*jitterWidth + s2offset )}) .attr("cy", function(d){return(y(d.low))}) .attr("r", 4) .attr("fill-opacity",fillopa) .style("fill", color2[3]); }); </script> </body> </html>
|