-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold_version_index_1.html
More file actions
139 lines (123 loc) · 2.95 KB
/
old_version_index_1.html
File metadata and controls
139 lines (123 loc) · 2.95 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
h2 {
text-align: center;
}
.chart rect {
fill: steelblue;
}
</style>
<script type="text/javascript">
function draw(data) {
"use strict"
var margin = 75,
width = 1400 - margin,
height = 600 - margin;
d3.select("body")
.append("h2")
.text("Titanic Passenger Survival Rate");
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin)
.attr("height", height + margin)
.append('g')
.attr('class','chart');
var rate_scale = d3.scale.linear()
.range([height, margin])
.domain([0, 1]);
var rate_axis = d3.svg.axis()
.scale(rate_scale)
.orient("left");
d3.select("svg")
.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + margin + ", 0)")
.call(rate_axis);
var nested = d3.nest()
.key(function(d) {
return d['Sex'];
})
.rollup(function(leaves) {
var totalSurvived = d3.sum(leaves, function(d) {
return d['Survived'];
});
debugger;
var total = data.length;
var rate = totalSurvived/total;
return {
'rate': rate
};
})
.entries(data);
var elementWidth = 240;
var category_scale = d3.scale.ordinal()
.rangeRoundBands([0, elementWidth]);
//.domain(nested);
debugger;
var category_axis = d3.svg.axis()
.scale(category_scale)
.orient("bottom");
debugger;
d3.select("svg")
.append("g")
.attr("class", "x axis")
.attr("transform", "translate(" + margin + ", " + height + ")")
.call(category_axis);
debugger;
var svg = d3.select(".chart")
.attr("width", elementWidth)
.attr("height", height);
var chart = svg.append("g");
debugger;
var b1 = chart.append("g.bar-group");
var b2 = b1
.data(nested);
var b3 = b2
.enter();
var b4 = b3
.append("g");
var bar = b4
.attr("class", "bar-group");
debugger;
var bar_rect = bar.append("rect")
.attr("y", height)
.attr("width", elementWidth)
.attr("height", 0)
.attr("class", "bar")
.transition()
.duration(1500)
.attr("x", margin)
.attr("y", function(d) {
debugger;
/*if (d.key === "female") {*/
//return d.values["rate"];
return rate_scale(0.55);
/*}*/
})
.attr("height", function(d) {
//return height - d.values["rate"];
return height - rate_scale(0.55);
})
.attr("textContent", function(d) { return d.values["rate"];});
/*
debugger;
var bar_text = bar.append("text")
.attr("x", margin)
.attr("y", rate_scale - 10)
.attr("dx", ".35em")
.text(function(d) { return d["rate"];});
*/
debugger;
};
</script>
</head>
<body>
<script type="text/javascript">
d3.csv("titanic_data.csv", draw);
</script>
</body>
</html>