/
pop2010-scales_03.html
134 lines (118 loc) · 2.9 KB
/
pop2010-scales_03.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
body {
font-family: Helvetica, Arial;
}
svg {
width: 500px;
height: 500px;
}
.top-label {
font-size: 13px;
font-style: italic;
text-transform: uppercase;
float: left;
}
.age-label {
text-align: right;
font-weight: bold;
width: 90px;
padding-right: 10px;
}
.clearfix {
clear: both;
}
.bar {
fill: DarkSlateBlue;
}
.bar-label {
text-anchor: end;
}
.axis line {
fill: none;
stroke: #000;
}
.axis path {
display: none;
}
.axis text {
font-size: 13px;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var popData = [
{age:"80 and up", value:0.016, position:0},
{age:"75-79", value:0.015, position:1},
{age:"70-74", value:0.021, position:2},
{age:"65-69", value:0.026, position:3},
{age:"60-64", value:0.034, position:4},
{age:"55-59", value:0.045, position:5},
{age:"50-54", value:0.051, position:6},
{age:"45-49", value:0.060, position:7},
{age:"40-44", value:0.066, position:8},
{age:"35-39", value:0.071, position:9},
{age:"30-34", value:0.073, position:10},
{age:"25-29", value:0.081, position:11},
{age:"20-24", value:0.089, position:12},
{age:"15-19", value:0.088, position:13},
{age:"10-14", value:0.086, position:14},
{age:"5-9", value:0.088, position:15},
{age:"0-4", value:0.093, position:16}
];
var margin = {top: 30, right: 0, bottom: 0, left: 100},
width = 500 - margin.left - margin.right,
height = 450 - margin.top - margin.bottom;
var barHeight = 20,
barGap = 5,
barSpacing = barHeight + barGap;
var x = d3.scale.linear()
.domain([0, d3.max(popData, function(element) { return element.value; } ) ] )
.range([0, width]);
var y = d3.scale.ordinal()
.domain(popData.map(function(element) { return element.age; }))
.range(popData.map(function(element) { return barSpacing * element.position; }));
var body = d3.select("body");
body.append("h2")
.text("Age distribution of the world, 2010");
body.append("div")
.attr("class", "top-label age-label")
.style("width", margin.left + "px")
.append("p")
.text("age group");
body.append("div")
.attr("class", "top-label")
.append("p")
.text("portion of the population")
body.append("div")
.attr("class", "clearfix");
var svg = body.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var barGroup = svg.append("g")
.attr("class", "bargroup");
barGroup.selectAll("rect")
.data(popData)
.enter().append("rect")
.attr("class", "bar")
.attr("x", 0)
.attr("y", function(d) { return y(d.age); } )
.attr("width", function(d) { return x(d.value); } )
.attr("height", barHeight);
var xAxis = d3.svg.axis()
.scale(x)
.orient("top")
.ticks(5, "%")
svg.append("g")
.call(xAxis)
.attr("class", "axis")
</script>
</body>
</html>