This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
204 lines (173 loc) · 7.96 KB
/
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// script.js
// Load the CSV data and start the slideshow
let file = "world-gdp-gross-domestic-product.csv";
// file = "http://localhost:8000/world-gdp-gross-domestic-product.csv"; // Required to run locally
const svg = d3.select("#chart-container");
const margin = { top: 20, right: 80, bottom: 50, left: 80 };
const width = svg.attr("width") - margin.left - margin.right;
const height = svg.attr("height") - margin.top - margin.bottom;
function setup() {
d3.csv(file, function(d) {
// Parse the data as needed (convert strings to numbers, dates, etc.)
return {
year: new Date(d["Date"]).getFullYear(),
gdp: +d["GDP (Billions of US $)"],
annualChange: +d["Annual % Change"]
};
}).then(function(data) {
// Data loading is complete, proceed to rendering the chart
// Create a new SVG group (g) to contain the chart elements
const chartGroup = svg.append("g").attr("transform", `translate(${margin.left}, ${margin.top})`);
// Set the ranges for the x and y axes
const xScale = d3.scaleTime().domain(d3.extent(data, d => d.year)).range([0, width]);
const yScaleGDP = d3.scaleLinear().domain([0, d3.max(data, d => d.gdp)]).range([height, 0]);
const yScaleAnnualChange = d3.scaleLinear().domain(d3.extent(data, d => d.annualChange)).range([height, 0]);
// Add x axis to the chart
chartGroup.append("g")
.attr("transform", `translate(0, ${height})`)
.call(d3.axisBottom(xScale).tickFormat(d3.format("d")))
.append("text")
.style("font", "14px arial")
.attr("x", width/2)
.attr("y", margin.bottom - 15)
.attr("fill", "black")
.text("Year (yr.)");
// Add left y axis to the chart for GDP
chartGroup.append("g")
.call(d3.axisLeft(yScaleGDP))
.append("text")
.style("font", "14px arial")
.attr("x", height/2 + margin.bottom)
.attr("y", margin.left - 20)
.attr("transform", "rotate(90)")
.attr("fill", "steelblue")
.text("Global GDP (Billion USD)");
// Add right y axis to the chart for Annual Change
chartGroup.append("g")
.attr("transform", `translate(${width}, 0)`)
.call(d3.axisRight(yScaleAnnualChange))
.append("text")
.style("font", "14px arial")
.attr("x", -1*(height/2 + margin.bottom))
.attr("y", margin.right - 40)
.attr("fill", "green")
.attr("transform", "rotate(-90)")
.text("Annual Change (%)");
// Add annotation as tooltip
let tooltip = d3.select("div#chart").select("div#annotation")
.style("opacity", 0)
.style("position", "absolute")
.style("pointer-events", "none");
// Add the legend to the chart
const legendItems = [
{ name: 'Global GDP (Bn. US$)', color: 'steelblue' },
{ name: 'Annual Change (%)', color: 'lightgreen' },
];
const legend = chartGroup
.append('g')
.attr('class', 'legend')
.attr('transform', `translate(${width - 190}, ${height - 65})`); // Position the legend in the top-right corner
const legendRectSize = 18;
const legendSpacing = 5;
const legendItem = legend
.selectAll('.legend-item')
.data(legendItems)
.enter()
.append('g')
.attr('class', 'legend-item')
.attr('transform', (d, i) => `translate(0, ${i * (legendRectSize + legendSpacing)})`);
legendItem
.append('rect')
.attr('width', legendRectSize)
.attr('height', legendRectSize)
.attr('fill', (d) => d.color);
legendItem
.append('text')
.attr('x', legendRectSize + 5)
.attr('y', legendRectSize - 5)
.text(d => d.name)
.style('font-size', '14px')
.attr('alignment-baseline', 'middle');
})
.catch(function(error) {
// Handle any error that may occur during data loading
console.error("Error loading data:", error);
});
}
function revealData(start, end) {
d3.select("#chart-container").select('path#gdp').remove();
d3.select("#chart-container").select('path#annualChange').remove();
d3.csv(file, function(d) {
// Parse the data as needed (convert strings to numbers, dates, etc.)
return {
year: new Date(d["Date"]).getFullYear(),
gdp: +d["GDP (Billions of US $)"],
annualChange: +d["Annual % Change"]
};
}).then(function(data) {
// Data loading is complete, proceed to rendering the chart
// Create a new SVG group (g) to contain the chart elements
const chartGroup = svg.select("g").attr("transform", `translate(${margin.left}, ${margin.top})`);
// Set the ranges for the x and y axes
const xScale = d3.scaleTime().domain(d3.extent(data, d => d.year)).range([0, width]);
const yScaleGDP = d3.scaleLinear().domain([0, d3.max(data, d => d.gdp)]).range([height, 0]);
const yScaleAnnualChange = d3.scaleLinear().domain(d3.extent(data, d => d.annualChange)).range([height, 0]);
// Create the line generators for GDP and Annual Change
const lineGDP = d3.line()
.x(d => xScale(d.year))
.y(d => yScaleGDP(d.gdp));
const lineAnnualChange = d3.line()
.x(d => xScale(d.year))
.y(d => yScaleAnnualChange(d.annualChange));
// Add the line for Annual Change to the chart
chartGroup.append("path")
.datum(data.slice(0,end-start+1).concat(new Array(2021-end).fill(0)))
.attr("id", "annualChange")
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "lightgreen") // You can change the color of the line for Annual Change here
.attr("stroke-width", 1.5)
.attr("d", lineAnnualChange);
// Add the line for GDP to the chart (GDP line is above Annual Change line)
chartGroup.append("path")
.datum(data.slice(0,end-start+1).concat(new Array(2021-end).fill(0)))
.attr("id", "gdp")
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("d", lineGDP);
// Add event listeners to the SVG container (not individual paths)
svg.on("click", function(event) {
showTooltip(event, data, end);
}).on("mouseout", hideTooltip);
})
.catch(function(error) {
// Handle any error that may occur during data loading
console.error("Error loading data:", error);
});
}
function showTooltip(event, d, end) {
xScaleLinear = d3.scaleLinear()
.domain([margin.left, margin.left + width])
.range([1960, 2021]);
const mouseX = d3.event.pageX;
const yearIndex = Math.round(xScaleLinear(mouseX)) - 1960;
if (yearIndex+1960 > end) {
return;
}
const tooltip = d3.select("div#chart").select("div#annotation");
tooltip.transition().duration(200).style("opacity", 0.9);
const datum = d[yearIndex];
const tooltipContent = `Year: ${datum.year}<br>Global GDP: ${datum.gdp.toFixed(2)}<br>Annual Change: ${datum.annualChange.toFixed(2)}`;
// Calculate tooltip position relative to the mouse cursor
const tooltipX = mouseX + 10;
const tooltipY = d3.event.pageY - 25;
tooltip.html(tooltipContent)
.style("left", tooltipX + "px")
.style("top", tooltipY + "px");
}
function hideTooltip() {
const tooltip = d3.select("div#chart").select("div#annotation");
tooltip.transition().duration(300).style("opacity", 0);
}