-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht5_script.js
178 lines (160 loc) · 6.12 KB
/
t5_script.js
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
// Set dimensions and margins for the graph
const margin = { top: 50, right: 30, bottom: 50, left: 150 },
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
// Append the svg object to the body of the page
const svg = d3.select("#chart")
.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})`);
// Data set URI
const temperature_csvUrl = 'https://raw.githubusercontent.com/Cropdata5320/CropData_Visualizations/main/temperature.csv';
const rainfall_csvurl = 'https://raw.githubusercontent.com/Cropdata5320/CropData_Visualizations/main/rainfall.csv'
// Read the data
d3.csv(temperature_csvUrl).then(tempData => {
// Format your temperature data here
tempData.forEach(d => {
d.YEAR = +d.YEAR;
d.ANNUAL = +d.ANNUAL;
});
d3.csv(rainfall_csvurl).then(rainData => {
// Format your temperature data here
rainData.forEach(d => {
d.YEAR = +d.YEAR;
d.ANN = +d.ANN;
});
// Combine the temperature and rainfall data
const combinedData = tempData.map(td => {
const rd = rainData.find(rd => rd.YEAR === td.YEAR);
return {
YEAR: td.YEAR,
ANNUAL: td.ANNUAL,
ANN: rd ? rd.ANN : null
};
});
// X axis
const x = d3.scaleBand()
.range([0, width])
.domain(combinedData.map(d => d.YEAR))
.padding(0.2);
svg.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(x).tickFormat(d3.format("d")))
.selectAll("text")
.attr("class", "axis-label")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-65)");
// Y axis
const yTemp = d3.scaleLinear()
.domain([0, d3.max(combinedData, d => d.ANNUAL)])
.range([height, 0]);
svg.append("g")
.attr("class", "y-axis-temp")
.call(d3.axisLeft(yTemp));
const yRainfall = d3.scaleLinear()
.domain([0, d3.max(combinedData, d => d.ANN)])
.range([height, 0]);
svg.append("g")
.attr("class", "y-axis-rain")
.attr("transform", `translate(${width}, 0)`)
.call(d3.axisLeft(yRainfall));
// Line for temperature
svg.append("path")
.datum(combinedData)
.attr("class", "temp-line")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(d => x(d.YEAR))
.y(d => yTemp(d.ANNUAL))
);
// Line for rainfall
svg.append("path")
.datum(combinedData)
.attr("class", "rain-line")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("d", d3.line()
.x(d => x(d.YEAR))
.y(d => yRainfall(d.ANN))
);
// Title
svg.append("text")
.attr("class", "title")
.attr("x", width / 2)
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.text("Task 5 - Annual Temperature & Rainfall Trends Over Time");
// Tooltip
const tooltip = d3.select("#chart")
.append("div")
.attr("class", "tooltip");
svg.selectAll(".temp-dot")
.data(combinedData)
.enter().append("circle")
.attr("class", "temp-dot")
.attr("cx", d => x(d.YEAR))
.attr("cy", d => yTemp(d.ANNUAL))
.attr("r", 3)
.attr("fill", "green")
.on("mouseover", (event, d) => {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(`Year: ${d.YEAR}<br>Temperature: ${d.ANNUAL}°C`)
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", d => {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
svg.selectAll(".rain-dot")
.data(combinedData)
.enter().append("circle")
.attr("class", "rain-dot")
.attr("cx", d => x(d.YEAR))
.attr("cy", d => yRainfall(d.ANN))
.attr("r", 3)
.attr("fill", "green")
.on("mouseover", (event, d) => {
tooltip.transition()
.duration(200)
.style("opacity", .9);
tooltip.html(`Year: ${d.YEAR}<br>Rainfall: ${d.ANN}mm`)
.style("left", (event.pageX) + "px")
.style("top", (event.pageY - 28) + "px");
})
.on("mouseout", d => {
tooltip.transition()
.duration(500)
.style("opacity", 0);
});
// Add legend
const legend = svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 12)
.attr("text-anchor", "end")
.selectAll("g")
.data(["Temperature", "Rainfall"])
.enter().append("g")
.attr("transform", (d, i) => `translate(-10,${20 + i * 20})`);
legend.append("rect")
.attr("x", -36)
.attr("width", 12)
.attr("height", 12)
.attr("fill", d => d === "Temperature" ? "#ff6347" : "#4682b4");
legend.append("text")
.attr("x", -45)
.attr("y", 6)
.attr("dy", "0.32em")
.text(d => d);
});
});