-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
271 lines (232 loc) · 8.69 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
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
var margin = 100,
width = 600,
height = 400 - margin;
/********************************************************************
* Use D3 to write a title and a smal introduction to the data set
********************************************************************/
function draw(data) {
"use strict"; // We must declare all variables
d3.select("body")
.append("h2")
.text("Titanic DataSet")
.append("h6")
.text("Conslusions: We can see the most important data from Titanic Disaster."
+ "This data set contains basic information of 891 passengers of "
+ "the Titanic. Information such as, Spouses, Children, whether "
+ "they survived or not, class, their name, age, ticket number and so on. "
+ "We will try to display all this information in a easy way, so "
+ "you can understand easily what happened there. ");
plotChart(data);
d3.select("body")
.append("h6")
.text("In this first plot, you can compare among class/sex/Number of Siblings/"
+ "Number of Parents/Children/Embarked port and the porcentage of deceased/"
+ "survived people. It seems clear that high class people and females were more "
+ "likely to survive. It is curious that beeing a big family, didn't mean you were going "
+ "to survive. Actually, bigs families were more likely to death. ");
plotBubble(data);
d3.select("body")
.append("h6")
.text("In this second plot, you can see one of the most interesting comparations."
+ "survived/deceased male against female.");
};
/********************************************************************
* plotBubble - Receiving the data set, this function will create the second
* bubble plot. It shows the most interesting from my point of view
* difference between men and women in survived or not.
********************************************************************/
function plotBubble(data){
var xvalues = ["Pclass", "Sex", "SibSp", "Parch", "Embarked", "AgeI"]
var xvaluesnames = ["Class", "Sex", "Number of Siblings/Spouse", "Number of Parents/Children", "Embarked Port", "Age"]
//Define buttons
var buttons2 = d3.select("body")
.append("div")
.attr("class", "xvalues_buttons2")
.selectAll("div")
.data(xvaluesnames)
.enter()
.append("button")
.attr("class", "btn btn-danger")
.text(function(d) {
return d;
});
//I will activate first button to facilitate user visualization
d3.select(".xvalues_buttons2").selectAll("button")[0][0].className += " active";
//Define button click behavior and chart update sequence by first removing the previous plot and then updating with the new plot
buttons2.on("click", function(d) {
d3.select(".xvalues_buttons2")
.selectAll("div")
.transition()
.duration(500)
d3.select(".xvalues_buttons2")
.selectAll("button")
.each(function(e,i) {
if(e==d){
d3.select(".xvalues_buttons2").selectAll("button")[0][i].className += " active";//Activate button
}else{
d3.select(".xvalues_buttons2").selectAll("button")[0][i].className = "btn btn-danger";//Desactivate button
}
})
d3.select(this)
.transition()
.duration(500)
svg.selectAll("*").remove();
chartUpdate2(d);
})
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class','chart');
var xvalue = "Class";
/******************************************
* This function help to update the plot information, besides we onyl use it once
* it might be useful for the future.
*******************************************/
function chartUpdate2(xvalue) {
var myChart = new dimple.chart(svg, data);
var x = myChart.addCategoryAxis("x", xvalues[xvaluesnames.indexOf(xvalue)]);
if(xvalues[xvaluesnames.indexOf(xvalue)] == "Embarked"){
x.addOrderRule(['C', 'Q', 'S']);
}else{
x.addOrderRule(xvalues[xvaluesnames.indexOf(xvalue)]);
}
x.title = xvalue;
var y = myChart.addMeasureAxis("y", "count");//If only count: addMeasureAxis
y.title = "% Deceased/Survived";
myChart.addMeasureAxis("z", "count");//Size of the bubble depending on number of passenger
//Define stacked bar chart split using survival status
var s = myChart.addSeries("PassengerStatus", dimple.plot.bar);
//Use aggregate function to calculate sum and percentage
s.aggregate = dimple.aggregateMethod.count;
s.getTooltipText = function (e) {
return [
e.aggField[0],
xvalue + ": " + e.x,
e.aggField[0] + " People: " +e.y
]
};
//Define legend position and color scheme for bars
myChart.addLegend(10, 10, 500, 30, "left");
myChart.assignColor("Deceased", "rgb(230,85,13)");
myChart.assignColor("Survived", "rgb(43,140,190)");
myChart.draw(1000);
}
chartUpdate2(xvalue);
};
/********************************************************************
* plotChart - Receiving the data set, this function will create the first
* chart plot. It shows the differents parameters against the age.
********************************************************************/
function plotChart(data){
var xvalues = ["Pclass", "Sex", "SibSp", "Parch", "Embarked", "AgeI"]
var xvaluesnames = ["Class", "Sex", "Number of Siblings/Spouse", "Number of Parents/Children", "Embarked Port", "Age"]
//Define buttons
var buttons = d3.select("body")
.append("div")
.attr("class", "xvalues_buttons")
.selectAll("div")
.data(xvaluesnames)
.enter()
.append("button")
.attr("class", "btn btn-success")
.text(function(d) {
return d;
});
//I will activate first button to facilitate user visualization
d3.select(".xvalues_buttons").selectAll("button")[0][0].className += " active";
//Define button click behavior and chart update sequence by first removing the previous plot and then updating with the new plot
buttons.on("click", function(d) {
d3.select(".xvalues_buttons")
.selectAll("div")
.transition()
.duration(500)
d3.select(".xvalues_buttons")
.selectAll("button")
.each(function(e,i) {
if(e==d){
d3.select(".xvalues_buttons").selectAll("button")[0][i].className += " active";//Activate button
}else{
d3.select(".xvalues_buttons").selectAll("button")[0][i].className = "btn btn-success";//Desactivate button
}
})
d3.select(this)
.transition()
.duration(500)
svg.selectAll("*").remove();
chartUpdate(d);
})
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class','chart nodeceased');
var xvalue = "Class";
/******************************************
* This function help to update the plot information
* everytime you change the y axis.
*******************************************/
function chartUpdate(xvalue) {
var myChart = new dimple.chart(svg, data);
var x = myChart.addCategoryAxis("x", xvalues[xvaluesnames.indexOf(xvalue)]);
if(xvalues[xvaluesnames.indexOf(xvalue)] == "Embarked"){
x.addOrderRule(['C', 'Q', 'S']);
}else{
x.addOrderRule(xvalues[xvaluesnames.indexOf(xvalue)]);
}
x.title = xvalue;
var y = myChart.addPctAxis("y", "count");//If only count: addMeasureAxis
y.title = "Survival Rate";
//Define stacked bar chart split using survival status
var s = myChart.addSeries("PassengerStatus", dimple.plot.bar);
s.addOrderRule(['Survived','Deceased']);
//Use aggregate function to calculate sum and percentage
s.aggregate = dimple.aggregateMethod.count;
s.getTooltipText = function (e) {
return [
e.aggField[0],
xvalue + ": " + e.x
]
};
//Define legend position and color scheme for bars
myChart.assignColor("Survived", "rgb(180,140,90)");
myChart.draw(1000);
d3.selectAll('.nodeceased').selectAll('.dimple-bar.dimple-deceased').attr('visibility','hidden');
}
chartUpdate(xvalue);
};
/*******************************
* Use D3 to load the csv file
*******************************/
d3.csv("data/titanic.csv", function(d) {
d['count']=1;
if(d['Survived'] == 0){
d['PassengerStatus'] = 'Deceased';
}else{
d['PassengerStatus'] = 'Survived';
}
if(d['Embarked'] == 'C' || d['Embarked'] == 'Q' || d['Embarked'] == 'S'){
} else d['Embarked'] = 'C'//Only 2 exceptions
if(+d['Age'] < 10){
d['AgeI'] = 5;
}else if(+d['Age'] >= 10 && +d['Age'] < 20){
d['AgeI'] = 15;
}else if(+d['Age'] >= 20 && +d['Age'] < 30){
d['AgeI'] = 25;
}else if(+d['Age'] >= 30 && +d['Age'] < 40){
d['AgeI'] = 35;
}else if(+d['Age'] >= 40 && +d['Age'] < 50){
d['AgeI'] = 45;
}else if(+d['Age'] >= 50 && +d['Age'] < 60){
d['AgeI'] = 55;
}else if(+d['Age'] >= 60 && +d['Age'] < 70){
d['AgeI'] = 65;
}else if(+d['Age'] >= 70 && +d['Age'] < 80){
d['AgeI'] = 75;
}else if(+d['Age'] >= 80 && +d['Age'] < 90){
d['AgeI'] = 85;
}
return d;
}, draw);