forked from cs480x-21c/04-Remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
180 lines (155 loc) · 5.22 KB
/
index.html
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
<script src="https://d3js.org/d3.v4.js"></script>
<h3>Click a stacked bar to get more information...</h3>
<div id="my_dataviz"></div>
<script>
// set the dimensions and margins of the graph
var margin = {top: 30, right: 30, bottom: 110, left: 50},
width = 460 - margin.left - margin.right,
height = 540 - margin.top - margin.bottom;
// append the svg object to the body of the page
var 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 + ")");
// append the svg object to the body of the page
var svg2 = 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 + ")");
// Parse the Data
d3.csv("AllMeatData.csv", function(data) {
// List of subgroups = header of the csv files = soil condition here
var subgroups = data.columns.slice(1)
// List of groups = species here = value of the first column called group -> I show them on the X axis
var groups = d3.map(data, function(d){return(d.ImpactCategory)}).keys()
// Add X axis
var x = d3.scaleBand()
.domain(groups)
.range([0, width])
.padding([0.2])
svg.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x).tickSizeOuter(0))
.selectAll("text")
.attr("transform", "translate(-10,0)rotate(-45)")
.style("text-anchor", "end");
// Add Y axis
var y = d3.scaleLinear()
.domain([0, 22])
.range([ height, 0 ]);
svg.append("g")
.call(d3.axisLeft(y));
// color palette = one color per subgroup
var color = d3.scaleOrdinal()
.domain(subgroups)
.range(['#FE5F55', '#28A828', '#2B65EC'])
//stack the data? --> stack per subgroup
var stackedData = d3.stack()
.keys(subgroups)
(data)
svg.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Environmental Impact of Beef");
// Show the bars
svg.append("g")
.selectAll("g")
// Enter in the stack data = loop key per key = group per group
.data(stackedData)
.enter().append("g")
.attr("fill", function(d) { return color(d.key); })
.selectAll("rect")
// enter a second time = loop subgroup per subgroup to add all rectangles
.data(function(d) { return d; })
.enter().append("rect")
.attr("x", function(d) { return x(d.data.ImpactCategory); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.attr("width",x.bandwidth())
.on("click", function(d) {
if(d.data.ImpactCategory == "GHG Emissions (kg CO2e)") {
updateFocusGraph("GHGData.csv", 4, "GHG Emissions (kg CO2e)");
}
if(d.data.ImpactCategory == "Energy Use (MJ)") {
updateFocusGraph("EnergyData.csv", 12, "Energy Use (MJ)");
}
if(d.data.ImpactCategory == "Land Use (m2 a-e)") {
updateFocusGraph("LandData.csv", 4, "Land Use (m2 a-e)");
}
})
})
//Function to update selected category graph
function updateFocusGraph(csv, scale, title) {
svg2.selectAll("*").remove();
// Parse the Data
d3.csv(csv, function(data) {
// X axis
var x = d3.scaleBand()
.range([ 0, width ])
.domain(data.map(function(d) { return d.TypeMeat; }))
.padding(1);
svg2.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x))
.selectAll("text")
.attr("transform", "translate(-10,0)rotate(-45)")
.style("text-anchor", "end");
// Add Y axis
var y = d3.scaleLinear()
.domain([0, scale])
.range([ height, 0]);
svg2.append("g")
.call(d3.axisLeft(y));
svg2.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text(title);
// Bars
svg2.selectAll("mybar")
.data(data)
.enter()
.append("rect")
.attr("x", function(d) { return x(d.TypeMeat) - 35; })
.attr("y", function(d) { return y(d.Value); })
.attr("width", 70)
.attr("height", function(d) { return height - y(d.Value); })
.attr("fill", "#69b3a2")
.on("click", function(d) {
var theMeat = d.TypeMeat;
var theVal = d.Value;
svg.selectAll("rect")
.attr("fill", function(d) {
var f = d3.format(".1f");
var s = d3.format(".2f");
var t = d3.format(".3f");
if(theVal == f(d[1] - d[0]) || theVal == s(d[1] - d[0]) || theVal == t(d[1] - d[0])) {
return "#000000";
}
})
})
svg2.selectAll("mybar")
.data(data)
.enter()
.append("text")
.attr("x", function(d) { return x(d.TypeMeat); })
.attr("y", function(d) { return y(d.Value) - 5; })
.attr("text-anchor", "middle")
.text(function(d) {
console.log("Hello")
return d.Value;
});
})
}
</script>