-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.html
More file actions
146 lines (116 loc) · 3.83 KB
/
Copy pathproject1.html
File metadata and controls
146 lines (116 loc) · 3.83 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
140
141
142
143
144
145
146
<!doctype html>
<html lang="en">
<head>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<meta charset="UTF-8">
<title>Project 1</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="title">
<h1>Cornell Engineering Students Enrollment</h1>
<h3>By Peter Blanco, Lily Gao, and Xinyi Wang</h3>
<h4>CS 3300 - Project 1 - March 2, 2014</h2>
</div>
<div id="legend">
<table id = "table_Legend">
<tr>
<td>
<svg height="10" width="10">
<rect width="10" height="10" style="fill:#3498db"/>
</svg>
</td>
<td>Male Students</td>
</tr>
<tr>
<td>
<svg height="10" width="10">
<rect width="10" height="10" style="fill:#c0392b"/>
</svg>
</td>
<td>Female Students</td>
</tr>
</table>
</div>
<script>
var margin = {top: 20, right: 20, bottom: 40, left: 50},
width = 480 - margin.left - margin.right,
height = 250 - margin.top - margin.bottom;
var yScale, xScale;
var majors;
d3.json("majors.json", function (data) {
majors = data;
majors.forEach(function (mjr, i){
//Set area up for svg
var div = d3.select("body").append('div');
var header = div.append('h4').text(mjr.major);
var svg = div.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("align", "center");
//Convert objects to usable data
var maleData = [];
var femaleData = [];
var dates = [];
mjr.count.forEach(function (m,i) {
maleData[i] = m.male;
femaleData[i]= m.female;
dates[i] = (m.sem + " " + m.year);
});
//Scales
yScale = d3.scale.linear()
.domain([0, d3.max(mjr.count, function(s){ return Math.max(s.female, s.male); })])
.range([height,margin.top]);
xScale = d3.scale.ordinal()
.domain(dates)
.rangePoints([margin.left, width]);
//Axis
var xAxis = d3.svg.axis().scale(xScale).orient("bottom");
var yAxis = d3.svg.axis().scale(yScale).orient("left").ticks(5);
svg.append('g').call(yAxis)
.attr('class', 'axis')
.attr("transform", "translate( 50, 0)")
.call(yAxis);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.style("font-size", "12")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", function(d) {
return "rotate(-65)"
});
svg.append("text")
.attr("x",10)
.attr("y",10)
.style("font-size","10px")
.text("Enrollment")
.attr("transform", "translate(0,150)rotate(270)");
//Line function
var line = d3.svg.line()
.x(function (x, i){return xScale(dates[i]);})
.y(function (y){return yScale(y);});
//Append the lines for male and female data
svg.append('path').attr('d', line(maleData))
.attr('class', 'male');
svg.append('path').attr('d', line(femaleData))
.attr('class', 'female');
//Append circles for the lines
svg.selectAll('circle1').data(maleData).enter().append('circle')
.attr('cx', function (d, i) {return xScale(dates[i]);})
.attr('cy', function (d) {return yScale(d);})
.attr('r', 2)
.attr('fill','#3498db');
svg.selectAll('circle2').data(femaleData).enter().append('circle')
.attr('cx', function (d, i) {return xScale(dates[i]);})
.attr('cy', function (d) {return yScale(d);})
.attr('r', 2)
.attr('fill','#c0392b');
});
});
</script>
</body>
</html>