-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
executable file
·124 lines (100 loc) · 3.73 KB
/
script.js
File metadata and controls
executable file
·124 lines (100 loc) · 3.73 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
var margin = { top: 20, right: 20, bottom: 30, left: 40 },
width = 200 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
// set the ranges
var x = d3.scaleBand()
.range([0, width])
.padding(0.1);
var y = d3.scaleLinear()
.range([height, 0]);
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var chart1 = d3.select("#chart1").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("id", "chart1")
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// get the data
d3.csv("female.csv", function (error, data) {
if (error) throw error;
// format the data
data.forEach(function (d) {
d.sales = +d.sales;
});
// Scale the range of the data in the domains
x.domain(data.map(function (d) { return d.county; }));
y.domain([0, 50000/1000]);
// append the rectangles for the bar chart
chart1.selectAll(".barF")
.data(data)
.enter().append("rect")
.attr("class", "barF")
.attr("x", function (d) { return x(d.county); })
.attr("width", x.bandwidth())
.attr("y", function (d) { return y(d.value / 1000); })
.attr("height", function (d) { return height - y(d.value / 1000); });
// add the x Axis
chart1.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the y Axis
chart1.append("g")
.call(d3.axisLeft(y));
chart1.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "12px")
.style('font-family', '"Oswald", sans-serif')
.style('font-weight', '700')
.style('fill', 'SlateBlue')
.text("FEMALES in thousands");
});
// append the svg object to the body of the page
// append a 'group' element to 'svg'
// moves the 'group' element to the top left margin
var chart2 = d3.select("#chart2").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 + ")");
// get the data
d3.csv("male.csv", function (error, data) {
if (error) throw error;
// format the data
data.forEach(function (d) {
d.sales = +d.sales;
});
// Scale the range of the data in the domains
x.domain(data.map(function (d) { return d.county; }));
y.domain([0, 50]);
// append the rectangles for the bar chart
chart2.selectAll(".barM")
.data(data)
.enter().append("rect")
.attr("class", "barM")
.attr("x", function (d) { return x(d.county); })
.attr("width", x.bandwidth())
.attr("y", function (d) { return y(d.value / 1000); })
.attr("height", function (d) { return height - y(d.value / 1000); });
// add the x Axis
chart2.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
// add the y Axis
chart2.append("g")
.call(d3.axisLeft(y));
chart2.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "12px")
.style('font-family', '"Oswald", sans-serif')
.style('font-weight', '700')
.style('fill', 'SlateBlue')
.text("MALES in thousands");
});