/
ch9-1.html
154 lines (128 loc) · 4.23 KB
/
ch9-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
<html>
<head>
<title>Mind the Gap</title>
<meta name="description" content="Practical D3">
<meta name="author" content="Tarek Amr, @gr33ndata">
</head>
<body>
</body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript">
/**
The Global Gender Gap Index examines the gap between men and women in four fundamental categories (sub-indexes): Economic Participation and Opportunity, Educational Attainment, Health and Survival and Political Empowerment.
http://reports.weforum.org/global-gender-gap-report-2014/part-1/the-global-gender-gap-index-2014/
Alice Corona from Silk cleaned the data and put it in machine readable format, i.e. CSV
https://github.com/ali-ce/datasets/blob/master/Global-Gender-Gap-Index-2014/Country%20Main%20Indices.csv
https://raw.githubusercontent.com/ali-ce/datasets/master/Global-Gender-Gap-Index-2014/Country%20Main%20Indices.csv
*/
var w = 1200, h = 620, margin = 20;
var svg = d3.select('body').append('svg')
.attr('width', w).attr('height', h);
var url = 'https://raw.githubusercontent.com/ali-ce/datasets/master/Global-Gender-Gap-Index-2014/Country%20Main%20Indices.csv'
var colours = d3.scale.category10();
var draw = function(rows){
var mode = 0;
var xScale = d3.scale.linear()
.domain([0, 1])
.range([margin, w-margin]);
var yScale = d3.scale.ordinal()
.domain(rows.map(function(d,i){ return i; }))
.rangePoints([margin, h-margin]);
var bars_widths = 0.85 * (h - 2*margin) / rows.length;
bar = svg.selectAll('bar')
.data(rows)
.enter()
.append('rect')
.attr('class', 'bar')
.attr('x', function(d,i){
return xScale(0);
})
.attr('y', function(d,i){
return yScale(i);
})
.attr('width', function(d,i){
return xScale(d.score) - xScale(0);
})
.attr('height', function(d,i){
return bars_widths;
})
.attr("fill", function(d,i){
return colours(d.region);
});
lable = svg.selectAll('lable')
.data(rows)
.enter()
.append('text')
.attr('class', 'lable')
.attr('x', function(d,i){
return xScale(d.score) + 20;
})
.attr('y', function(d,i){
return yScale(i) + 10;
})
.text(function(d){ return d.country; })
.style("text-anchor", "begin")
.attr('font-size', function(d){
return 1.4*bars_widths;
})
.attr("fill", function(d,i){
return colours(d.region);
});
bar.on('click',function(){
mode = (mode == 0)? 1 : 0;
bar.sort(function(a,b){
if (mode == 0){
return (a.score == b.score)? 0 : ((a.score > b.score)? 1 : -1);
} else {
return (a.score == b.score)? 0 : ((a.score > b.score)? -1 : 1);
}
})
.transition()
.duration(function(d,i){ return 80 * i; })
/**.attr('x', function(d,i){
return xScale(0);
})*/
.attr('y', function(d,i){
return yScale(i);
});
lable.sort(function(a,b){
if (mode == 0){
return (a.score == b.score)? 0 : ((a.score > b.score)? 1 : -1);
} else {
return (a.score == b.score)? 0 : ((a.score > b.score)? -1 : 1);
}
})
.transition()
.duration(function(d,i){ return 50 * i; })
/**.attr('x', function(d,i){
return xScale(d.score) + 20;
})*/
.attr('y', function(d,i){
return yScale(i) + 10;
});
});
}
d3.csv(url, function(d){
return {
country: d['Country'],
// We convert income group from '1 (High Income)' to 1
income: d['Income Group'].split(' ')[0],
region: d['Region'],
score: d['Overall - Score'],
};
}, function(error, rows) {
// Let's only plot high income countris.
if (error){
console.log(error.responseText);
return;
}
draw(
rows.filter(function(d,i){
return (d.income == '1');
}).sort(function(a,b){
return (a.score == b.score)? 0 : ((a.score > b.score)? -1 : 1);
})
);
});
</script>
<html>