-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaidagraph.htm
More file actions
296 lines (240 loc) · 9.22 KB
/
aidagraph.htm
File metadata and controls
296 lines (240 loc) · 9.22 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<html>
<head>
<title> Bar graph </title>
<script src="https://d3js.org/d3.v6.min.js"></script>
<style>
body {
margin: 50px;
}
.gridlines line {
stroke: #bbb;
}
.gridlines .domain {
stroke: none;
}
.text {
color: white;
}
</style>
</head>
<body>
<svg id="barchart" height="500" width="1000"></svg>
<svg id="barchart2" height="500" width="1000"></svg>
<script>
//creating a bar graph for FEMALE contestants for the bachelor
console.log( d3.csv("../count_data_bachelor.csv") );
//creating space for second graph
const svg2 = d3.select("svg#barchart2");
const width2 = svg2.attr("width");
const height2 = svg2.attr("height");
const margin2 = {top: 10, right: 10, bottom: 70, left: 50};
const chartWidth2 = width2 - margin2.left - margin2.right;
const chartHeight2 = height2 - margin2.top - margin2.bottom;
let annotations2 = svg2.append("g").attr("id","annotations");
let chartArea2 = svg2.append("g").attr("id","points")
.attr("transform",`translate(${margin2.left},${margin2.top})`);
d3.csv("../count_data_bachelor.csv").then( data => { console.log("data", data);
//making sure each value is a number and not a string
data.forEach((d, i) => {
d['id'] = Number(d['id']);
d['season'] = Number(d['season']);
d['totalcast'] = Number(d['totalcast']);
d['minority'] = Number(d['minority']);
d['white'] = Number(d['white']);
d['year'] = Number(d['year']);
});
console.log(data)
//creating scales
const countScale = d3.scaleLinear().domain([0,45]).range([chartHeight2, 0]);
const years = d3.map(data, d => d.year)
console.log(years)
const yearScale = d3.scaleBand().domain(years).range([0, chartWidth2])
.padding(0.3);
//axis and gridlines
let leftAxis = d3.axisLeft(countScale);
let leftGridlines = d3.axisLeft(countScale)
.tickSize(-chartWidth2-10)
.tickFormat("")
annotations2.append("g")
.attr("class", "y axis")
.attr("transform",`translate(${margin2.left-10},${margin2.top})`)
.call(leftAxis)
annotations2.append("g")
.attr("class", "y gridlines")
.attr("transform",`translate(${margin2.left-10},${margin2.top})`)
.call(leftGridlines);
let bottomAxis = d3.axisBottom(yearScale)
annotations2.append("g")
.attr("class", "x axis")
.attr("transform",`translate(${margin2.left},${chartHeight2+margin2.top+10})`)
.call(bottomAxis);
//axis labels
chartArea2.append("text").attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width/2)
.attr("y", height - 30)
.text("Years");
chartArea2.append("text")
.attr("class", "y label")
.attr("text-anchor", "middle")
.attr("y", -50)
.attr("x", -200)
.attr("dy", "0.75em")
.attr("transform", "rotate(-90)")
.text("Number of People");
//creating bars
var bar = chartArea2.selectAll("rect.bar")
.data(data);
bar.enter().append("rect").attr("class","bar")
.attr("fill", "#FFC5D0")
.attr("x", d => yearScale(d.year))
.attr("y", d => countScale(d.totalcast))
.attr("height", d => countScale(d.minority) - countScale(d.totalcast))
.attr("width", yearScale.bandwidth());
bar.enter().append("rect").attr("class","bar")
.attr("fill", "#821D30")
.attr("x", d => yearScale(d.year))
.attr("y", d => countScale(d.minority))
.attr("height", d => countScale(0) - countScale(d.minority))
.attr("width", yearScale.bandwidth());
//adding numbers to the bars
data.forEach((d, i) => {
svg2.append("text")
.attr("text-anchor","hanging")
.attr("font-size","15px")
.attr("x",yearScale(d.year)+77)
.attr('y',countScale(d.minority))
.text(d.minority);
svg2.append("text")
.attr("text-anchor","hanging")
.attr("font-weight", "bold")
.attr("font-size","17px")
.attr("x",yearScale(d.year)+77)
.attr('y',countScale(d.totalcast))
.text(d.white);
});
//creating legends
const legend2 = chartArea2.append("g")
.attr("class", "legend")
.attr("transform","translate(10,10)");
legend2.append("rect").attr("x", 20).attr("y", 10).attr("width", 230).attr("height", 70).attr("fill", "white").attr("stroke", "black");
legend2.append("circle").attr("cx",40).attr("cy",30).attr("r", 8).style("fill", "#FFC5D0")
legend2.append("circle").attr("cx",40).attr("cy",60).attr("r", 8).style("fill", "#821D30")
legend2.append("text").attr("x", 60).attr("y", 30).text("Caucasian Contestants").style("font-size", "17px").attr("alignment-baseline","middle")
legend2.append("text").attr("x", 60).attr("y", 60).text("Minority Constestans").style("font-size", "17px").attr("alignment-baseline","middle")
//ending
},
//in case of error
error => {console.log("error", error);
});
</script>
<script>
//creating a bar graph for MALE contestants for the bachelorette
console.log( d3.csv("../counted_data.csv") );
//creating space for the other graph
const svg = d3.select("svg#barchart");
const width = svg.attr("width");
const height = svg.attr("height");
const margin = {top: 10, right: 10, bottom: 70, left: 50};
const chartWidth = width - margin.left - margin.right;
const chartHeight = height - margin.top - margin.bottom;
let annotations = svg.append("g").attr("id","annotations");
let chartArea = svg.append("g").attr("id","points")
.attr("transform",`translate(${margin.left},${margin.top})`);
d3.csv("../counted_data.csv").then( data => { console.log("data", data);
//making sure each value is a number and not a string
data.forEach((d, i) => {
d['id'] = Number(d['id']);
d['season'] = Number(d['season']);
d['totalcast'] = Number(d['totalcast']);
d['minority'] = Number(d['minority']);
d['white'] = Number(d['white']);
d['year'] = Number(d['year']);
});
console.log(data)
//creating scales
const leftScale = d3.scaleLinear().domain([0,45]).range([chartHeight, 0]);
const years = d3.map(data, d => d.year) // d3.map just runs a function on each item in an array
console.log(years)
const yearScale = d3.scaleBand().domain(years).range([0, chartWidth])
.padding(0.2);
//axis and gridlines
let leftAxis = d3.axisLeft(leftScale);
let leftGridlines = d3.axisLeft(leftScale)
.tickSize(-chartWidth-10)
.tickFormat("")
annotations.append("g")
.attr("class", "y axis")
.attr("transform",`translate(${margin.left-10},${margin.top})`)
.call(leftAxis)
annotations.append("g")
.attr("class", "y gridlines")
.attr("transform",`translate(${margin.left-10},${margin.top})`)
.call(leftGridlines);
let bottomAxis = d3.axisBottom(yearScale)
annotations.append("g")
.attr("class", "x axis")
.attr("transform",`translate(${margin.left},${chartHeight+margin.top+10})`)
.call(bottomAxis);
//axis labels
chartArea.append("text").attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width/2)
.attr("y", height - 30)
.text("Years");
chartArea.append("text")
.attr("class", "y label")
.attr("text-anchor", "middle")
.attr("y", -50)
.attr("x", -200)
.attr("dy", "0.75em")
.attr("transform", "rotate(-90)")
.text("Number of People");
//creating bars
var bar = chartArea.selectAll("rect.bar")
.data(data);
bar.enter().append("rect").attr("class","bar")
.attr("fill", "#B1D4E0")
.attr("x", d => yearScale(d.year))
.attr("y", d => leftScale(d.totalcast))
.attr("height", d => leftScale(d.minority) - leftScale(d.totalcast))
.attr("width", yearScale.bandwidth());
bar.enter().append("rect").attr("class","bar")
.attr("fill", "#0C2D48")
.attr("x", d => yearScale(d.year))
.attr("y", d => leftScale(d.minority))
.attr("height", d => leftScale(0) - leftScale(d.minority))
.attr("width", yearScale.bandwidth());
//adding numbers to the bars
data.forEach((d, i) => {
svg.append("text")
.attr("text-anchor","hanging")
.attr("font-size","15px")
.attr("x",yearScale(d.year)+77)
.attr('y',leftScale(d.minority))
.text(d.minority);
svg.append("text")
.attr("text-anchor","hanging")
.attr("font-weight", "bold")
.attr("font-size","17px")
.attr("x",yearScale(d.year)+75)
.attr('y',leftScale(d.totalcast))
.text(d.totalcast);
});
//making legends
const legend = chartArea.append("g")
.attr("class", "legend")
.attr("transform","translate(10,10)");
legend.append("rect").attr("x", 20).attr("y", 10).attr("width", 230).attr("height", 70).attr("fill", "white").attr("stroke", "black");
legend.append("circle").attr("cx",40).attr("cy",30).attr("r", 6).style("fill", "#B1D4E0")
legend.append("circle").attr("cx",40).attr("cy",60).attr("r", 6).style("fill", "#0C2D48")
legend.append("text").attr("x", 60).attr("y", 30).text("Caucasian Contestants").style("font-size", "17px").attr("alignment-baseline","middle")
legend.append("text").attr("x", 60).attr("y", 60).text("Minority Constestans").style("font-size", "17px").attr("alignment-baseline","middle")
//ending
},
//in case of error
error => {console.log("error", error);
});
</script>
</body>
</html>