-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathline_chart.html
More file actions
167 lines (132 loc) · 4.08 KB
/
line_chart.html
File metadata and controls
167 lines (132 loc) · 4.08 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
<title></title>
</head>
<body>
<div id="paired-line-chart">
</div>
<hr>
<!-- hhmts start -->Last modified: Mon Jul 1 21:39:12 PDT 2013 <!-- hhmts end -->
<script src="http://d3js.org/d3.v3.js"></script>
<script type="text/javascript">
d3.csv("./data/us-income-inequality.csv", function(data1) {
/* Read CSV file: first row => year,top1,top5 */
var maxval = 0,
sampsize = 0;
var label_array = new Array(),
val_array1 = new Array();
sampsize = data1.length;
for (var i=0; i < sampsize; i++) {
label_array[i] = parseInt(data1[i].year);
val_array1[i] = { x: label_array[i], y: parseFloat(data1[i].p99), z: parseFloat(data1[i].p95) };
maxval = Math.max(maxval, parseFloat(data1[i].p99), parseFloat(data1[i].p95) );
}
maxval = (1 + Math.floor(maxval / 10)) * 10;
var w = 815,
h = 500,
p = 30,
x = d3.scale.linear().domain([ label_array[0], label_array[sampsize-1] ]).range([0, w]),
y = d3.scale.linear().domain([0, maxval]).range([h, 0]);
var vis = d3.select("#paired-line-chart")
.data([val_array1])
.append("svg:svg")
.attr("width", w + p * 2)
.attr("height", h + p * 2)
.append("svg:g")
.attr("transform", "translate(" + p + "," + p + ")");
var rules = vis.selectAll("g.rule")
.data(x.ticks(15))
.enter().append("svg:g")
.attr("class", "rule");
// Draw grid lines
rules.append("svg:line")
.attr("x1", x)
.attr("x2", x)
.attr("y1", 0)
.attr("y2", h - 1);
rules.append("svg:line")
.attr("class", function(d) { return d ? null : "axis"; })
.data(y.ticks(10))
.attr("y1", y)
.attr("y2", y)
.attr("x1", 0)
.attr("x2", w - 10);
// Place axis tick labels
rules.append("svg:text")
.attr("x", x)
.attr("y", h + 15)
.attr("dy", ".71em")
.attr("text-anchor", "middle")
.text(x.tickFormat(10))
.text(String);
rules.append("svg:text")
.data(y.ticks(12))
.attr("y", y)
.attr("x", -10)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(y.tickFormat(5));
// Series I
vis.append("svg:path")
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "maroon")
.attr("stroke-width", 2)
.attr("d", d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.y); }));
vis.selectAll("circle.line")
.data(val_array1)
.enter().append("svg:circle")
.attr("class", "line")
.attr("fill", "maroon" )
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.y); })
.attr("r", 1);
// Series II
vis.append("svg:path")
.attr("class", "line")
.attr("fill", "none")
.attr("stroke", "darkblue")
.attr("stroke-width", 2)
.attr("d", d3.svg.line()
.x(function(d) { return x(d.x); })
.y(function(d) { return y(d.z); }));
vis.select("circle.line")
.data(val_array1)
.enter().append("svg:circle")
.attr("class", "line")
.attr("fill", "darkblue" )
.attr("cx", function(d) { return x(d.x); })
.attr("cy", function(d) { return y(d.z); })
.attr("r", 1);
// -----------------------------
// Add Title then Legend
// -----------------------------
vis.append("svg:text")
.attr("x", w/4)
.attr("y", 20)
.text("% share of income (excluding capital gains): U.S. 1920-2008");
vis.append("svg:rect")
.attr("x", w/2 - 20)
.attr("y", 50)
.attr("stroke", "darkblue")
.attr("height", 2)
.attr("width", 40);
vis.append("svg:text")
.attr("x", 30 + w/2)
.attr("y", 55)
.text("Top 5% households");
vis.append("svg:rect")
.attr("x", w/2 - 20)
.attr("y", 80)
.attr("stroke", "maroon")
.attr("height", 2)
.attr("width", 40);
vis.append("svg:text")
.attr("x", 30 + w/2)
.attr("y", 85)
.text("Top 1% households");
});
</script>
</body> </html>