forked from thekua/d3-hacknight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
our_index.html
170 lines (135 loc) · 4.94 KB
/
our_index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<html>
<head>
<script type="text/javascript" src="lib/d3.v3.min.js"></script>
<script type="text/javascript" src="lib/underscore-min.js"></script>
<style>
#chart {
margin-left: -40px;
height: 506px;
}
text {
font: 10px sans-serif;
}
.dot {
stroke: #000;
}
.axis path, .axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.label {
fill: #777;
}
.year.label {
font: 500 196px "Helvetica Neue";
fill: #ddd;
}
.year.label.active {
fill: #aaa;
}
.overlay {
fill: none;
pointer-events: all;
cursor: ew-resize;
}
</style>
</head>
<body>
<h1>Visa Allowances</h1>
<div id="chart"></div>
<script type="text/javascript">
// var list = [{x: 1, y: 2}, {x: 4, y: 5}];
//
// var convert = function(element) {
// return element.y;
// };
// var result = _.map(list, convert);
var typesToColors = {
"Employment" : "#ff0000",
"Study" : "#00ff00",
"Family route" : "#0000ff",
"Dep. joining / acc" : "#cc0000",
"Other" : "#cccccc"
};
d3.csv("control-immigration-q1-2011-main.csv", function(unparsedData) {
var interestingData = unparsedData.slice(18);
var convert = function(quarter, value, type) {
var value = value.replace(/,/, ''); // get rid of formatting
var parsedNumber = parseInt(value);
if (isNaN(parsedNumber)) { // deal with funny characters
parsedNumber = 1;
}
return {
x: quarter,
y: parsedNumber,
radius: 10,
color: typesToColors[type]
};
};
var resultList = [];
var quarters = ["Q1 2005", "Q2 2005", "Q3 2005", "Q4 2005", "Q1 2006", "Q2 2006", "Q3 2006", "Q4 2006", "Q1 2007", "Q2 2007", "Q3 2007", "Q4 2007", "Q1 2008", "Q2 2008", "Q3 2008", "Q4 2008", "Q1 2009", "Q2 2009", "Q3 2009", "Q4 2009", "Q1 2010", "Q2 2010", "Q3 2010", "Q4 2010", "Q1 2011"];
var typeRow = interestingData["Type"];
console.log("Types", interestingData);
_.each(quarters, function(key, index) {
var result = interestingData.map(function(row) {
var type = row["Type"];
var value = row[key];
return convert(index + 1, value, type);
});
resultList = resultList.concat(result);
});
drawChart(resultList);
});
var drawChart = function(data) {
d3.csv("control-immigration-q1-2011-main.csv")
.row(function(row) { return row["Q1 2005"]; });
console.log(data);
// Chart dimensions.
var margin = {top: 19.5, right: 19.5, bottom: 19.5, left: 39.5},
width = 960 - margin.right,
height = 500 - margin.top - margin.bottom;
// Various scales. These domains make assumptions of data, naturally.
var xScale = d3.scale.linear().domain([0, 25]).range([0, width]),
yScale = d3.scale.linear().domain([1, 120000]).range([height, 0]),
radiusScale = d3.scale.sqrt().domain([0, 5e8]).range([0, 40]);
// The x & y axes.
var xAxis = d3.svg.axis().orient("bottom").scale(xScale).ticks(25),
yAxis = d3.svg.axis().scale(yScale).orient("left");
// Create the SVG container and set the origin.
var svg = d3.select("#chart").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 + ")");
// Add the x-axis.
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the y-axis.
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
// Add a dot per nation. Initialize the data at 1800, and set the colors.
// var data = [
// {color: "#ff0000", x: 10, y: 60000, radius: 100 }
// ];
var dot = svg.append("g")
.attr("class", "dots")
.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.style("fill", function(d) { return d.color; })
.call(position);
function position(dot) {
dot .attr("cx", function(d) { return xScale(d.x); })
.attr("cy", function(d) { return yScale(d.y); })
.attr("r", function(d) { return d.radius; });
}
};
</script>
<p>hello world</p>
</body>
</html>