-
Notifications
You must be signed in to change notification settings - Fork 47
/
bar-chart.js
166 lines (141 loc) · 4.17 KB
/
bar-chart.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
/* global d3 */
// eslint-disable-next-line no-unused-vars
const projectName = "bar-chart";
// coded by @Christian-Paul
var width = 800,
height = 400,
barWidth = width / 275;
var tooltip = d3
.select(".visHolder")
.append("div")
.attr("id", "tooltip")
.style("opacity", 0);
var overlay = d3
.select(".visHolder")
.append("div")
.attr("class", "overlay")
.style("opacity", 0);
var svgContainer = d3
.select(".visHolder")
.append("svg")
.attr("width", width + 100)
.attr("height", height + 60);
d3.json(
"https://raw.githubusercontent.com/FreeCodeCamp/ProjectReferenceData/master/GDP-data.json"
)
.then((data) => {
svgContainer
.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -200)
.attr("y", 80)
.text("Gross Domestic Product");
svgContainer
.append("text")
.attr("x", width / 2 + 120)
.attr("y", height + 50)
.text("More Information: http://www.bea.gov/national/pdf/nipaguid.pdf")
.attr("class", "info");
var years = data.data.map(function (item) {
var quarter;
var temp = item[0].substring(5, 7);
if (temp === "01") {
quarter = "Q1";
} else if (temp === "04") {
quarter = "Q2";
} else if (temp === "07") {
quarter = "Q3";
} else if (temp === "10") {
quarter = "Q4";
}
return item[0].substring(0, 4) + " " + quarter;
});
var yearsDate = data.data.map(function (item) {
return new Date(item[0]);
});
var xMax = new Date(d3.max(yearsDate));
xMax.setMonth(xMax.getMonth() + 3);
var xScale = d3
.scaleTime()
.domain([d3.min(yearsDate), xMax])
.range([0, width]);
var xAxis = d3.axisBottom().scale(xScale);
svgContainer
.append("g")
.call(xAxis)
.attr("id", "x-axis")
.attr("transform", "translate(60, 400)");
var GDP = data.data.map(function (item) {
return item[1];
});
var scaledGDP = [];
var gdpMax = d3.max(GDP);
var linearScale = d3.scaleLinear().domain([0, gdpMax]).range([0, height]);
scaledGDP = GDP.map(function (item) {
return linearScale(item);
});
var yAxisScale = d3.scaleLinear().domain([0, gdpMax]).range([height, 0]);
var yAxis = d3.axisLeft(yAxisScale);
svgContainer
.append("g")
.call(yAxis)
.attr("id", "y-axis")
.attr("transform", "translate(60, 0)");
d3.select("svg")
.selectAll("rect")
.data(scaledGDP)
.enter()
.append("rect")
.attr("data-date", function (d, i) {
return data.data[i][0];
})
.attr("data-gdp", function (d, i) {
return data.data[i][1];
})
.attr("class", "bar")
.attr("x", function (d, i) {
return xScale(yearsDate[i]);
})
.attr("y", function (d) {
return height - d;
})
.attr("width", barWidth)
.attr("height", function (d) {
return d;
})
.attr("index", (d, i) => i)
.style("fill", "#33adff")
.attr("transform", "translate(60, 0)")
.on("mouseover", function (event, d) {
// d or datum is the height of the
// current rect
var i = this.getAttribute("index");
overlay
.transition()
.duration(0)
.style("height", d + "px")
.style("width", barWidth + "px")
.style("opacity", 0.9)
.style("left", i * barWidth + 0 + "px")
.style("top", height - d + "px")
.style("transform", "translateX(60px)");
tooltip.transition().duration(200).style("opacity", 0.9);
tooltip
.html(
years[i] +
"<br>" +
"$" +
GDP[i].toFixed(1).replace(/(\d)(?=(\d{3})+\.)/g, "$1,") +
" Billion"
)
.attr("data-date", data.data[i][0])
.style("left", i * barWidth + 30 + "px")
.style("top", height - 100 + "px")
.style("transform", "translateX(60px)");
})
.on("mouseout", function () {
tooltip.transition().duration(200).style("opacity", 0);
overlay.transition().duration(200).style("opacity", 0);
});
})
.catch((e) => console.log(e));