-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (110 loc) · 4.09 KB
/
index.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
const log = console.log;
d3.csv("alberta_census_subdivisions(2001-2017).csv")
.then(function(csvData) {
//Fix Náre name
//Convert Year, Sex, and Total to ints
csvData.forEach(element => {
if(element["Area Name"] === "Thabacha N�re 196A") element["Area Name"] = "Thabacha Náre 196A";
csvData.columns.forEach(d => {
if(d !== "Area Name") element[d] = parseInt(element[d]);
});
});
//Nested Area Names by Year
const years = d3.nest().key(d => d["Year"]).key(d => d["Area Name"]).entries(csvData);
//Used this and commented years for rapid prototyping
// const years = d3.range(0, 4).map(p => d3.nest().key(d => d["Year"]).key(d => d["Area Name"]).entries(csvData)[p]);
const sexesAndTotal = years[0].values[0].values.map(d => d["Sex"]);
const legendScale = d3.scaleOrdinal().domain(sexesAndTotal).range(["Female", "Male", "Total"]);
const yearsDivsHTML = years.map(d => {
return `
<div class="item">
<div class="${d.key}" style="width: 700px; height: 338px; overflow-x: hidden; overflow-y: scroll;">
<div id="${d.key}" style="height: 12000px;" class="item-content"></div>
</div>
</div>`;
}).reduce((acc, curVal) => acc + curVal, "");
//Muuri Grid
const sortAsc = document.querySelector(".sort-ascend");
const sortDesc = document.querySelector(".sort-descending");
d3.select(".grid").html(yearsDivsHTML);
const grid = new Muuri('.grid', {
layoutOnInit: false,
layout: {
horizontal: true
},
sortData: {
className: (item, element) => parseInt(element.children[0].className)
}
});
//Viz title Data Driven
d3.select("#titleID").html(`Alberta Census Subdivisions
Population Estimates (${years[0].key} - ${years[years.length - 1].key})`);
//Sort for Muuri
grid.sort("className", {layout: "instant"});
sortAsc.addEventListener("click", () => grid.sort("className"));
sortDesc.addEventListener("click", () => grid.sort("className:desc"));
//Each year echarts init and setOption
years.forEach(d => {
const year = d.key;
const myChart = echarts.init(document.getElementById(d.key), "custom-essos");
let yearValues = d.values;
yearValues = yearValues.map(d => d).sort((a, b) => a.values[0]["Total"] - b.values[0]["Total"]);
const areaNamesData = sexesAndTotal.map((p, i) => {
return {
name: legendScale(p),
type: "bar",
label: {
show: true,
position: "right",
fontSize: 9,
formatter: params => `${params.value.toLocaleString()}`
},
data:yearValues.map(v => v.values[i]["Total"])
};
});
const option = {
toolbox: {
show: true,
orient: "vertical",
showTitle: false,
feature: {
saveAsImage: {},
dataView: {
readOnly: true
}
}
},
title : {
text: `${year}`
},
legend:{},
grid: {
left: "37%",
right: "8%",
bottom: "0%",
},
xAxis: {
type: "value",
show: false
},
yAxis: {
name: `${year} Population Estimate`,
type: "category",
data: yearValues.map(v => v.key),
boundaryGap: true,
axisLabel: {
interval: 0,
showMaxLabel: true,
showMinLabel: true,
fontSize: 10
},
axisTick:{
interval: 0,
alignWithLabel: true
}
},
series: areaNamesData
};
myChart.setOption(option);
});
});