-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_static.js
119 lines (105 loc) · 3.03 KB
/
main_static.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
// set the dimensions and margins of the graph
let margin = { top: 10, right: 30, bottom: 50, left: 60 },
width = 460 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
let svg = d3
.select("#my_dataviz")
.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 + ")");
//Read the data
let promises = [d3.csv("data/dfPovertyAdultLit.csv")];
let allData = [];
Promise.all(promises).then(function (data) {
data.forEach(function (eachDataset) {
eachDataset.forEach(function (d) {
d["Poverty Rate (%)"] = +d["Poverty Rate (%)"];
d["Year"] = new Date(d["Year"]);
if (
d.hasOwnProperty(
"Adult literacy, 25 or more years old (% of population aged 25 or more)"
)
) {
d[
"Adult literacy, 25 or more years old (% of population aged 25 or more)"
] = +d[
"Adult literacy, 25 or more years old (% of population aged 25 or more)"
];
}
});
});
allData = data;
updateChart(allData);
});
//Function that builds the right chart depending on user choice on website:
function updateChart(someData) {
let dataAdultLit = d3
.nest()
.key(function (d) {
return d["Year"];
})
.entries(someData[0]);
let filteredData = dataAdultLit[5]["values"];
// Add X axis
let x = d3.scaleLinear().domain([0, 100]).range([0, width]);
svg
.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
//Add x-axis label:
svg
.append("text")
.attr(
"transform",
"translate(" + width / 2 + " ," + (height + margin.top + 30) + ")"
)
.style("text-anchor", "middle")
.text("Adult Literacy");
// Add Y axis
let y = d3.scaleLinear().domain([0, 100]).range([height, 0]);
svg.append("g").call(d3.axisLeft(y));
//Add y-axis label:
svg
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", -40)
.attr("x", 0 - height / 2)
.style("text-anchor", "middle")
.text("Poverty Rate");
// Color scale: input a province name, output a color
let color = d3
.scaleOrdinal()
.domain([
"Balochistan",
"Federal Capital Territory",
"Khyber Pakhtunkhwa",
"Punjab",
"Sindh",
])
.range(["#440154ff", "#21908dff", "#fde725ff", "#129490", "#CE1483"]);
// JOIN data to elements.
let circles = svg.selectAll("circle").data(filteredData, function (d) {
return d["District"];
});
// ENTER new elements present in new data.
circles
.enter()
.append("circle")
.attr("fill", function (d) {
return color(d["Province"]);
})
.attr("cy", function (d) {
return y(d["Poverty Rate (%)"]);
})
.attr("cx", function (d) {
return x(
d[
"Adult literacy, 25 or more years old (% of population aged 25 or more)"
]
);
})
.attr("r", 5);
}