-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
180 lines (155 loc) · 4.67 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
let quarterLookup = {
1: "Q1",
4: "Q2",
7: "Q3",
10: "Q4"
}
fetch('https://raw.githubusercontent.com/freeCodeCamp/ProjectReferenceData/master/GDP-data.json')
.then(response => {
response.json().then(data => {
processData(data);
}
);
})
.catch(err => {
console.warn("Fetch Failed: ",err)
})
// Auto Resize
// Taken from: https://benclinkinbeard.com/d3tips/make-any-chart-responsive-with-one-function/
function responsivefy(svg) {
// container will be the DOM element
// that the svg is appended to
// we then measure the container
// and find its aspect ratio
const container = d3.select(svg.node().parentNode),
width = parseInt(svg.style('width'), 10),
height = parseInt(svg.style('height'), 10),
aspect = width / height;
// set viewBox attribute to the initial size
// control scaling with preserveAspectRatio
// resize svg on inital page load
svg.attr('viewBox', `0 0 ${width} ${height}`)
.attr('preserveAspectRatio', 'xMinYMid')
.call(resize);
// add a listener so the chart will be resized
// when the window resizes
// multiple listeners for the same event type
// requires a namespace, i.e., 'click.foo'
// api docs: https://goo.gl/F3ZCFr
d3.select(window).on(
'resize.' + container.attr('id'),
resize
);
// this is the code that resizes the chart
// it will be called on load
// and in response to window resizes
// gets the width of the container
// and resizes the svg to fill it
// while maintaining a consistent aspect ratio
function resize() {
const w = parseInt(container.style('width'));
svg.attr('width', w);
svg.attr('height', Math.round(w / aspect));
}
}
function processData(dataset) {
let data = dataset.data.map(item => {
let date = new Date(item[0]);
return {
date: date,
dateString: item[0],
year: date.getUTCFullYear(),
label: date.getUTCFullYear() + " " + quarterLookup[date.getUTCMonth() + 1],
value: item[1]
}
});
console.dir(data, {depth: null});
let w = parseInt(d3.select('svg').style('width'));
let h = parseInt(d3.select('svg').style('height'));
let padding = 75;
let barWidth = (w - padding * 2) / data.length;
let tooltip = d3.select(".container")
.append("div")
.attr("class", "tooltip")
.attr("id", "tooltip")
let formatter = Intl.NumberFormat('en-US', {
style: 'currency',
currency: "USD",
minimumFractionDigits: 1,
maximumFractionDigits: 1
})
let yScale = d3.scaleLinear()
.domain([
0,
d3.max(data, d => d.value)
])
.range([ h - padding * 1.5 , padding ])
let xScale = d3.scaleTime()
.domain([
d3.min(data, d => d.date),
d3.max(data, d => d.date)
])
.range([ padding, w - padding])
let svg = d3.select('body').select('svg');
svg.call(responsivefy);
svg
.selectAll('rect')
.data(data)
.enter()
.append('rect')
.attr('x', (d, i) => xScale(d.date) )
.attr('y', (d, i) => yScale(d.value) + padding / 2 )
.attr('width', parseInt(barWidth))
.attr('height', d => h - yScale(d.value) - (padding * 1.5) )
.attr('class', 'bar')
.attr('data-date', d => d.dateString)
.attr('data-gdp', d => d.value)
.attr('data-label', d => d.label)
.on('mouseover', function (e, d) {
tooltip.transition()
.duration(200)
.style('opacity', 0.9)
tooltip
.html(`${d.label}<br>${formatter.format(d.value)} Billion`)
.style("left", e.clientX + 20 + "px")
.style("top", "30vw")
.attr('data-date', d.dateString )
})
.on('mouseout', function (e, d) {
tooltip
.transition()
.duration(200)
.style("opacity", 0);
})
// Chart Title
svg
.append('text')
.text("United States GDP")
.attr('class','title')
.attr('x', w/2)
.attr('y', padding)
.attr('id', 'title')
// Chart Footnote
let footnote = svg
.append('text')
.attr('class', 'footnote')
.attr('x', w - padding)
.attr('y', h - padding / 3)
footnote
.append("tspan")
.text("More Information: ")
footnote
.append("a")
.attr("xlink:href", "http://www.bea.gov/national/pdf/nipaguid.pdf")
.text("http://www.bea.gov/national/pdf/nipaguid.pdf")
let xAxis = d3.axisBottom(xScale);
svg.append("g")
.attr('id', 'x-axis')
.attr("transform", `translate(${0}, ${h - padding })`)
.call(xAxis)
let yAxis = d3.axisLeft(yScale);
svg.append("g")
.attr('id', 'y-axis')
.attr("transform", `translate(${padding },${padding/2})`)
.call(yAxis)
}