-
Notifications
You must be signed in to change notification settings - Fork 34
/
weather.Rmd
189 lines (164 loc) · 4.99 KB
/
weather.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# Weather forecast
<script src="https://d3js.org/d3.v7.js"></script>
<style>
* {
font-family: sans-serif;
}
text {
font-size: 12px;
}
</style>
<h3>Weather forecast for Columbia University</h3>
<p>Lat, Lon: 40.807793, -73.962144</p>
<p>(Hover over points for more details.)</p>
<input type="radio" name="tempscale" value="F" checked="true">Fahrenheit
<input type="radio" name="tempscale" value="C">Celsius
<div id="plot"></div>
<script type="text/javascript">
// Width and height
const w = 700;
const h = 300;
const margin = {top: 25, right: 100, bottom: 50, left: 100};
const innerHeight = h - margin.top - margin.bottom;
const innerWidth = w - margin.left - margin.right;
// create "placeholder" image
const icon = d3.select("div#plot")
.append("div")
.append("img")
.attr("src", "https://github.com/jtr13/d3book/blob/main/images/blank86x86.png?raw=true")
.style("padding-left", w/2 - 43 + "px");
// create SVG element
const svg = d3.select("div#plot")
.append("svg")
.attr("width", w)
.attr("height", h)
// create background rectangle
svg.append("rect")
.attr("width", w)
.attr("height", h)
.attr("fill", "#e7f5fe");
// create caption
d3.select("div#plot")
.append("div")
.style("padding", "10px")
.append("a")
.attr("href", "https://www.weather.gov/documentation/services-web-api")
.text("Data source: https://www.weather.gov/documentation/services-web-api");
// create plot group
svg.append("g")
.attr("id", "plot")
.attr("transform", `translate (${margin.left}, ${margin.top})`);
d3.json("https://api.weather.gov/gridpoints/OKX/33,38/forecast").then(function(data) {
// filter for isDaytime equals "true"
const dataset = data.properties.periods.filter(d => d.isDaytime);
const tempscale = "F";
// create new property with celsius temperature
dataset.forEach(d => d.celsius = (d.temperature - 32) * 5/9);
xScale = d3.scaleLinear()
.domain([d3.min(dataset.map(d => d.temperature - 9)),
d3.max(dataset.map(d => d.temperature + 9))])
.range([0, innerWidth]);
yScale = d3.scaleBand()
.domain(dataset.map(d => d.name))
.range([0, innerHeight]);
xAxis = d3.axisBottom()
.scale(xScale);
yAxis = d3.axisLeft()
.scale(yScale);
// Generate guide lines
svg.select("g#plot")
.selectAll("line")
.data(dataset)
.enter()
.append("line")
.attr("x1", 0)
.attr("x2", innerWidth)
.attr("y1", d => yScale(d.name) + .5*yScale.bandwidth())
.attr("y2", d => yScale(d.name) + .5*yScale.bandwidth())
.attr("stroke", "#ddd")
.attr("stroke-width", 1);
// Create circles
svg.select("g#plot")
.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", d => xScale(d.temperature))
.attr("cy", d => yScale(d.name) + .5 * yScale.bandwidth())
.attr("r", 3)
.on("mouseover", function(event, d) {
const xcoord = +d3.select(event.currentTarget).attr("cx") + 5
const ycoord = +d3.select(event.currentTarget).attr("cy") - 5
svg.select("g#plot")
.append("text")
.attr("id", "tooltip")
.attr("x", xcoord)
.attr("y", ycoord)
.text(d.shortForecast);
icon
.attr("src", d.icon);
})
.on("mouseout", function() {
d3.select("#tooltip")
.remove();
icon
.attr("src", "https://github.com/jtr13/d3book/blob/main/images/blank86x86.png?raw=true");
}
);
// create x-axis
svg.select("g#plot")
.append("g")
.attr("id", "xaxis")
.attr("transform", `translate (0, ${innerHeight})`)
.call(xAxis);
// create x-axis label
svg.select("g#plot")
.append("text")
.attr("id", "xlab")
.attr("x", innerWidth/2)
.attr("y", innerHeight + .75 * margin.bottom)
.attr("text-anchor", "middle")
.text("temperature (Fahrenheit)");
// create y-axis
svg.select("g#plot")
.append("g")
.call(yAxis)
// get value of radio button on click
d3.selectAll("input")
.on("click", function(event) {
const tempscale = event.currentTarget.value;
if (tempscale == "C") {
// update xScale domain
xScale.domain([d3.min(dataset.map(d => d.celsius - 5)),
d3.max(dataset.map(d => d.celsius + 5))]);
// update x-axis label
svg.select("#xlab")
.text("temperature (Celsius)")
// update cx value of circles
svg.selectAll("circle")
.attr("cx", d => xScale(d.celsius))
} else {
// update xScale domain
xScale.domain([d3.min(dataset.map(d => d.temperature - 9)),
d3.max(dataset.map(d => d.temperature + 9))]);
// update x-axis label
svg.select("#xlab")
.text("temperature (Fahrenheit)")
// update cx value of circles
svg.selectAll("circle")
.attr("cx", d => xScale(d.temperature))
};
// update x-axis
svg.select("#xaxis")
.call(xAxis);
}) // end .on
}) // end d3.json
.catch(function(error){
d3.select("svg")
.append("text")
.style("font-size", "24px")
.attr("x", "100")
.attr("y", "100")
.text("Error loading data");
});
</script>