D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
TMoore24
Full window
Github gist
date2
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> .axis--x path { display: none; } .area { fill: none; stroke: black; clip-path: url(#clip); } .area1 { fill: none; stroke: red; clip-path: url(#clip); } .area2 { fill: none; stroke: orange; clip-path: url(#clip); } .area3 { fill: none; stroke: pink; clip-path: url(#clip); } .zoom { cursor: move; fill: none; pointer-events: all; } ////*style of the modal used for the help button popup */ .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ padding-top: 100px; /* Location of the box */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content of help popup */ .modal-content { background-color: #cccccc; margin: auto; padding: 20px; border: 1px solid #303030; width: 80%; } /* The Close Button of help popup*/ .close { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } circle { fill: steelblue; clip-path: url(#clip) } </style> <svg width="1065" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> ////*set size of graph */ var svg = d3.select("svg"), margin = {top: 20, right: 132, bottom: 110, left: 123}, margin2 = {top: 430, right: 132, bottom: 30, left: 123}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, height2 = +svg.attr("height") - margin2.top - margin2.bottom, radius = 3, radius1=1; /////dots ////*what we can zoom and pan on in relation to graph */ focus = svg.append("g") .attr("class", "focus") .attr("transform", "translate(" + margin.left + "," + margin.top + ")"), context = svg.append("g") .attr("class", "context") .attr("transform", "translate(" + margin2.left + "," + margin2.top + ")"); ////*make date format from JSON work as a scale (MAY NEED TO CHANGE) */ var format = d3.timeParse("%Y-%m-%dT%H:%M:%SZ"); ////*create variable for all a and y axes */ var x = d3.scaleTime() .rangeRound([0, width]); var x2 = d3.scaleTime() .rangeRound([0, width]); var x3 = d3.scaleTime() .rangeRound([0, width]); var y = d3.scaleLinear() .rangeRound([height, 0]); var y2 = d3.scaleLinear() .rangeRound([height2, 0]); var y3 = d3.scaleLinear() .rangeRound([height, 0]); ////*the little grpah at the bottom needs them tooo!! */ var y32 = d3.scaleLinear() .rangeRound([height2, 0]); var y4 = d3.scaleLinear() .rangeRound([height, 0]); var y42 = d3.scaleLinear() .rangeRound([height2, 0]); var y5 = d3.scaleLinear() .rangeRound([height, 0]); var y52 = d3.scaleLinear() .rangeRound([height2, 0]); ////*create variable for each object needed represented and the zoom/toggle function */ var brush = d3.brushX() .extent([[0, 0], [width, height2]]) .on("brush end", brushed); var zoom = d3.zoom() .scaleExtent([1, Infinity]) .translateExtent([[0, 0], [width, height]]) .extent([[0, 0], [width, height]]) .on("zoom", zoomed); var area = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x(d.date2); }) .y0(height) .y1(function(d, i) { return y(d.wind); }) ; var linee = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x(d.date2); }) .y0(height) .y1(function(d,i) { return y3(d.windg); }); var lineee = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x(d.date2); }) .y0(height) .y1(function(d,i) { return y4(d.solar); }); var lineeee = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x3(d.date); }) .y0(height) .y1(function(d,i) { return y5(d.windd); }); var area2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x2(d.date2); }) .y0(height2) .y1(function(d,i) { return y2(d.wind); }); var linee2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x2(d.date2); }) .y0(height2) .y1(function(d,i) { return y32(d.windg); }); var lineee2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x2(d.date2); }) .y0(height) .y1(function(d,i) { return y42(d.solar); }); var lineeee2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x2(d.date); }) .y0(height) .y1(function(d,i) { return y52(d.windd); }); svg.append("defs").append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); // load the data, the date 2 object is faked...will have a date for each parmaeter d3.json("data2.json", function(error, data) { var obs = data[0].STATION[0].OBSERVATIONS; var dates = obs.date_time.map(format); var date2 = obs.date_time2.map(format);//// creating 2nd dates object var wind = obs.wind_speed_set_1 var windg = obs.wind_gust_set_1 var windd = obs.wind_direction_set_1 var humid = obs.relative_humidity_set_1 var solar = obs.solar_radiation_set_1 var precip = obs.precip_accum_set_1 var air = obs.air_temp_set_1.map(function(d){ return +d; }); data.date = dates; data.date2= date2; ////calling 2nd dates object in data.air = air; data.wind=wind; data.windg=windg; data.windd=windd; data.humid=humid; data.solar=solar; data.precip=precip; // scale the range of the data x.domain(d3.extent(data.date)); x3.domain(d3.extent(data.date2)); y.domain(d3.extent(data.windg)); x2.domain(x3.domain()); y2.domain(y.domain()); y3.domain(d3.extent(data.windg)); y32.domain(y3.domain()); y4.domain(d3.extent(data.solar)); y42.domain(y4.domain()); y5.domain(d3.extent(data.windd)); y52.domain(y5.domain()); data.airTempByDate = data.date.map(function(d, i){ return { date: d, date2:date2[i], wind: wind[i], windg: windg[i], solar:solar[i], windd: windd[i] }; }) //var colorMap = ["lightblue"]; //var colorScale = d3.scaleLinear().rangeRound([height, 0]) .range(colorMap); focus.append("g").attr("id","redLine6").selectAll("dot") .data(data.airTempByDate) .enter().append("svg:circle") .attr("r",radius) .attr("cx",function(d){return x3(d.date);}) .attr("cy",function(d){return y5(d.windd);}) ; //.style("fill",function(d,i){return colorScale(i);}) //.on("mouseover",function(d){ // d3.select(this).style("fill","orange").append("svg:title") // .text(d.windd+"("+d.date+""+")"); //}).on("mouseout",function(d,i){ // d3.select(this).style("fill",colorScale(i)); // d3.select(this).select("title").remove(); // }); var dot1 = context.selectAll("dot").data(data.airTempByDate).enter().append("circle") .attr("r",radius1) .attr("cx",function(d){return x3(d.date);}) .attr("cy",function(d){return y52(d.windd);}) focus.append("path") .datum(data.airTempByDate) .attr("class", "area2") .attr("id","greenLine") .attr("d", lineee); focus.append("path") .datum(data.airTempByDate) .attr("class", "area1") .attr("id","redLine") .attr("d", linee); focus.append("path") .datum(data.airTempByDate) .attr("class", "area") .attr("id","blueLine") .attr("d", area); focus.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height + ")") .call(d3.axisBottom(x3)); focus.append("g") .attr("class", "axis axis--y") .attr("id","blueLine1") .attr("stroke","black") .call(d3.axisLeft(y)) .call(d3.axisLeft(y3)); focus.append("g") .attr("class", "axis axis--y") // .attr("stroke","green") //.attr("id","redLine1") // .call(d3.axisRight(y3)); focus.append("g") .attr("class", "axis axis--y") .attr("transform", "translate( " + width + ", 0 )") .attr("stroke","orange") .attr("id","greenLine1") .call(d3.axisRight(y4)); context.append("path") .datum(data.airTempByDate) .attr("class", "area") .attr("id","blueLine2") .attr("d", area2); context.append("path") .datum(data.airTempByDate) .attr("class", "area1") .attr("id","redLine2") .attr("d", linee2); context.append("path") .datum(data.airTempByDate) .attr("class", "area2") .attr("id","greenLine2") .attr("d", lineee2); context.append("g") .attr("class", "axis axis--x") .attr("transform", "translate(0," + height2 + ")") .call(d3.axisBottom(x2)); context.append("g") .attr("class", "brush") .call(brush) .call(brush.move, x.range()); svg.append("rect") .attr("class", "zoom") .attr("width", width) .attr("height", height) .attr("transform", "translate(" + margin.left + "," + margin.top + ")") .call(zoom); ////off and onn svg.append("rect") .attr("x", 898) .attr("y", height + margin.top + -216) .attr("height", 20) .attr("width", 50) .attr("fill", "black") .style("stroke", "black"); svg.append("text") .attr("x", 902) .attr("y", height + margin.top + -200) .attr("class", "legend") .style("fill", "red") .on("click", function(){ // determine if current line is visible var active = blueLine1.active ? false : true, newOpacity = active ? 0 : 1; // hide or show the elements d3.select("#blueLine1").style("opacity", newOpacity); d3.select("#blueLine").style("opacity", newOpacity); d3.select("#blueLine2").style("opacity", newOpacity); d3.select("#blueLine4").style("opacity", newOpacity); d3.select("#redLine").style("opacity", newOpacity); d3.select("#redLine2").style("opacity", newOpacity); d3.select("#redLine4").style("opacity", newOpacity); d3.select("#redLine5").style("opacity", newOpacity); d3.select("#redLine6").style("opacity", newOpacity); d3.select("#redLine7").style("opacity", newOpacity); d3.select("#redLine8").style("opacity", newOpacity); d3.select("#redLine9").style("opacity", newOpacity); d3.select("#redLine10").style("opacity", newOpacity); // update whether or not the elements are active blueLine1.active = active; }) .text("Wind"); svg.append("rect") .attr("x", 898) .attr("y", height + margin.top + -193) .attr("height", 20) .attr("width", 50) .attr("fill", "black") .style("stroke", "black"); svg.append("text") .attr("x", 904) .attr("y", height + margin.top + -178) .attr("class", "legend") .style("fill", "orange") .on("click", function(){ // determine if current line is visible var active = greenLine1.active ? false : true, newOpacity = active ? 0 : 1; // hide or show the elements d3.select("#greenLine1").style("opacity", newOpacity); d3.select("#greenLine").style("opacity", newOpacity); d3.select("#greenLine2").style("opacity", newOpacity); d3.select("#greenLine4").style("opacity", newOpacity); d3.select("redLine2").style("opacity", newOpacity); // update whether or not the elements are active greenLine1.active = active; }) .text("Solar"); svg.append("text") .attr("id","blueLine4") .attr("transform", "rotate(-90)") .attr("y", 15) .attr("x", -253) .attr("fill","black") .text("Wind Speed (m/s)"); svg.append("text") .attr("id","redLine4") .attr("transform", "rotate(-90)") .attr("y", 31) .attr("x", -240) .attr("fill","red") .text("Wind Gust"); svg.append("text") .attr("transform", "rotate(-90)") .attr("y", 879) .attr("x", -272) .attr("id","greenLine4") .attr("fill","orange") .text("Solar Radiation(W/m^2)"); svg.append("text") .attr("x", 900) .attr("y", height + margin.top + -222) .text("On/Off"); }); function brushed() { if (d3.event.sourceEvent && d3.event.sourceEvent.type === "zoom") return; // ignore brush-by-zoom var s = d3.event.selection || x.range(); x.domain(s.map(x.invert, x)); focus.selectAll(".area2").attr("d", lineee); focus.selectAll(".area1").attr("d", linee); focus.selectAll(".area").attr("d", area); focus.select("circle").attr("cx", function(d) { return x(d.date2); }) ; svg.selectAll(".zoom").call(zoom.transform, d3.zoomIdentity .scale(width / (s[1] - s[0])) .translate(-s[0], 0)); } function zoomed() { if (d3.event.sourceEvent && d3.event.sourceEvent.type === "brush") return; // ignore zoom-by-brush var t = d3.event.transform; x.domain(t.rescaleX(x3).domain()); focus.selectAll(".area2").attr("d", lineee); focus.selectAll(".area1").attr("d", linee); focus.selectAll(".area").attr("d", area); focus.selectAll("circle") .attr("cx", function(d) { return x(d.date2); }) ; //Ensure the date shifts with zoom// focus.select(".axis--x").call(d3.axisBottom(x));///labels x axis context.select(".brush").call(brush.move, x.range().map(t.invertX, t)); } svg.append("text") .style("fill", "steelblue") .attr("id","redLine5") .attr("x", 60) .attr("y", height + margin.top + -364) .text("North"); svg.append("text") .style("fill", "steelblue") .attr("id","redLine10") .attr("x", 60) .attr("y", height + margin.top + -9) .text("North"); svg.append("text") .style("fill", "steelblue") .attr("id","redLine7") .attr("x", 60) .attr("y", height + margin.top + -187) .text("South"); svg.append("text") .style("fill", "steelblue") .attr("id","redLine8") .attr("x", 60) .attr("y", height + margin.top + -111) .text("East"); svg.append("text") .style("fill", "steelblue") .attr("id","redLine9") .attr("x", 60) .attr("y", height + margin.top + -265) .text("West"); </script>
https://d3js.org/d3.v4.min.js