-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_v0.1.html
215 lines (189 loc) · 6.4 KB
/
index_v0.1.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://dimplejs.org/dist/dimple.v2.0.0.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<style>
/* Title */
h2 {
text-align: center;
}
/* Normal text */
div {
text-align: justify;
}
/* Warning about browsers */
p {
text-align: center;
font-style: italic;
}
body {
text-align: center;
}
div.xvalues_buttons {
position: relative;
text-align: center;
width: 100%;
}
.btn{
margin: 1px;
}
</style>
<script type="text/javascript">
var margin = 100,
width = 600,
height = 400 - margin;
function draw(data) {
"use strict"; // We must declare all variables
d3.select("body")
.append("h2")
.text("Titanic DataSet")
.append("h6")
.text("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 "
+ "he 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);
plotBubble(data);
};
function plotBubble(data){
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class','chart');
var xvalue = "Age";
function chartUpdate(xvalue) {
var myChart = new dimple.chart(svg, data);
var x = myChart.addCategoryAxis("x", "AgeI");//, "Embarked"]);
//x.title = "Class";
x.timeInterval = 2;
//x.addOrderRule(xvalue);
x.title = "Passenger's " + xvalue;
var y = myChart.addMeasureAxis("y", "count");
myChart.addMeasureAxis("z", "count");
//Define stacked bar chart split using survival status
var s = myChart.addSeries("PassengerStatus", dimple.plot.bubble);
//Use aggregate function to calculate sum and percentage
s.aggregate = dimple.aggregateMethod.count;
/*myChart.addSeries("count", dimple.plot.line).aggregate =
dimple.aggregateMethod.count;
myChart.assignColor("count", "red");*/
s.getTooltipText = function (e) {
return [
e.aggField[0],
"Age between " + (e.x-5) + " and " + (e.x+5),
"Numer of Passerngers: " + 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);
}
chartUpdate(xvalue);
};
function plotChart(data){
var xvalues = ["Pclass", "Sex", "SibSp", "Parch", "Embarked"]
//Define buttons
var buttons = d3.select("body")
.append("div")
.attr("class", "xvalues_buttons")
.selectAll("div")
.data(xvalues)
.enter()
.append("button")
.attr("class", "btn btn-warning")
.text(function(d) {
return d;
});
//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(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');
var xvalue = "Pclass";
function chartUpdate(xvalue) {
var myChart = new dimple.chart(svg, data);
console.log(xvalue)
var x = myChart.addCategoryAxis("x", xvalue);//, "Embarked"]);
if(xvalue == "Embarked"){
x.addOrderRule(['C', 'Q', 'S']);
}else{
x.addOrderRule(xvalue);
}
x.title = "Passenger's " + xvalue;
var y = myChart.addPctAxis("y", "count");//If only count: addMeasureAxis
y.title = "%";
//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;
//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);
}
chartUpdate(xvalue);
};
</script>
</head>//http://bl.ocks.org/cvillere/2124c3ec603c2cfbdc96715f2f11d840
<body>
<script type="text/javascript">
/*
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);
</script>
</body>
</html>