D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
TMoore24
Full window
Github gist
wind
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); } .zoom { cursor: move; fill: none; pointer-events: all; } .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 */ .modal-content { background-color: #cccccc; margin: auto; padding: 20px; border: 1px solid #303030; width: 80%; } /* The Close Button */ .close { color: #aaaaaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: #000; text-decoration: none; cursor: pointer; } </style> <button id="myBtn">Help</button> <div id="myModal" class="modal"> <!-- Modal content --> <div class="modal-content"> <span class="close">×</span> <p>To turn on or off a variable in the graph click on the black button on the right side.<br> To zoom in or out on the graph use to fingers and bring them together, or pull them apart. <br>To move on the graph hold your finger down and slide in the direction you want to go. </p> <h1>Trends</h1> <p>Notice that humidity and temperature are often opposite, this is because cooler air can hold less moisture. When does precipiation occur in relation to humidity and temperature?</p> </div> </div> <svg width="960" height="500"></svg> <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 100, bottom: 110, left: 50}, margin2 = {top: 430, right: 100, bottom: 30, left: 50}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, height2 = +svg.attr("height") - margin2.top - margin2.bottom, 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 + ")"); var format = d3.timeParse("%Y-%m-%dT%H:%M:%SZ"); var x = d3.scaleTime() .rangeRound([0, width]); var x2 = 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]); var y32 = d3.scaleLinear() .rangeRound([height2, 0]); var y4 = d3.scaleLinear() .rangeRound([height, 0]); var y42 = d3.scaleLinear() .rangeRound([height2, 0]); 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.date); }) .y0(height) .y1(function(d, i) { return y(d.wind); }) ; var linee = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x(d.date); }) .y0(height) .y1(function(d,i) { return y3(d.windg); }); var lineee = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x(d.date); }) .y0(height) .y1(function(d,i) { return y4(d.solar); }); var area2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x2(d.date); }) .y0(height2) .y1(function(d,i) { return y2(d.wind); }); var linee2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x2(d.date); }) .y0(height2) .y1(function(d,i) { return y32(d.windg); }); var lineee2 = d3.area() .curve(d3.curveMonotoneX) .x(function(d,i) { return x(d.date); }) .y0(height) .y1(function(d,i) { return y42(d.solar); }); svg.append("defs").append("clipPath") .attr("id", "clip") .append("rect") .attr("width", width) .attr("height", height); // load the data d3.json("data2.json", function(error, data) { var obs = data[0].STATION[0].OBSERVATIONS; var dates = obs.date_time.map(format); 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.air = air; data.wind=wind; data.windg=windg; data.windd=windd; data.humid=humid; data.solar=solar; data.precip=precip; console.log(data.solar); // scale the range of the data x.domain(d3.extent(data.date)); y.domain(d3.extent(data.windg)); x2.domain(x.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()); data.airTempByDate = data.date.map(function(d, i){ return { date: d, wind: wind[i], windg: windg[i], solar:solar[i] }; }) 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(x)); 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.axisLeft(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", 900) .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); // update whether or not the elements are active blueLine1.active = active; }) .text("Temp"); svg.append("rect") .attr("x", 898) .attr("y", height + margin.top + -195) .attr("height", 20) .attr("width", 50) .attr("fill", "black") .style("stroke", "black"); svg.append("text") .attr("x", 900) .attr("y", height + margin.top + -180) .attr("class", "legend") .style("fill", "green") .on("click", function(){ // determine if current line is visible var active = redLine1.active ? false : true, newOpacity = active ? 0 : 1; // hide or show the elements d3.select("#redLine1").style("opacity", newOpacity); d3.select("#redLine").style("opacity", newOpacity); d3.select("#redLine2").style("opacity", newOpacity); d3.select("#redLine4").style("opacity", newOpacity); // update whether or not the elements are active redLine1.active = active; }) .text("Humid"); svg.append("rect") .attr("x", 898) .attr("y", height + margin.top + -174) .attr("height", 20) .attr("width", 50) .attr("fill", "black") .style("stroke", "black"); svg.append("text") .attr("x", 900) .attr("y", height + margin.top + -160) .attr("class", "legend") .style("fill", "blue") .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); // update whether or not the elements are active greenLine1.active = active; }) .text("Precip"); 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.selectAll(".axis--x").call(d3.axisBottom(x)); 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(x2).domain()); focus.selectAll(".area2").attr("d", lineee); focus.selectAll(".area1").attr("d", linee); focus.selectAll(".area").attr("d", area); focus.select(".axis--x").call(d3.axisBottom(x)); context.select(".brush").call(brush.move, x.range().map(t.invertX, t)); } // Get the modal var modal = document.getElementById('myModal'); // Get the button that opens the modal var btn = document.getElementById("myBtn"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks the button, open the modal btn.onclick = function() { modal.style.display = "block"; } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } </script>
https://d3js.org/d3.v4.min.js