-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3populationpyramid.js
More file actions
55 lines (46 loc) · 1.45 KB
/
d3populationpyramid.js
File metadata and controls
55 lines (46 loc) · 1.45 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
function PeoplePyramid (data, elementID) {
this.data = data;
this.data.by = crossFilter(this.data);
this.data.by.year = this.data.by.dimension(function (d) {
return d.year;
});
this.data.by.age = this.data.by.dimension(function (d) {
return d.age;
});
this.data.by.sex = this.data.by.dimension(function (d) {
return d.sex;
});
this.data.by.people = this.data.by.dimension(function (d) {
return d.people;
});
this.data.by.age.max = this.data.by.age.top(1);
this.data.by.people.max = this.data.by.people.top(1);
this.data.by.age.groups = this.data.by.age.group();
this.width = 600;
this.height = 800;
this.center = this.width/2;
this.age = d3.scale.ordinal()
.domain(this.data.by.age.groups.size())
.rangeRoundBands([0, height]);
this.people = {};
this.people.men = d3.scale.linear()
.domain([0, this.data.by.people.max])
.rangeRound([this.center, width])
.nice(5);
this.people.women = this.people.men.copy()
.rangeRound([this.center, 0]);
this.chart = d3.select('#' + elementID)
.attr('width', width)
.attr('height', height);
// If this year is in the data, start with it. Otherwise, go back to the beginning of time.
this.year = this.data.by.year.filter(new Date().getFullYear()).top(1)[0] || this.data.by.year.filter(null).bottom(1)[0];
this.bars = d3.selectAll('g')
.data(data)
.enter().append('g')
.attr('x', this.center)
.attr('y', function (d) {
return age(d.age);
});
this.bars.append('rect')
.attr('')
};