forked from arnicas/interactive-vis-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scatter_skeleton_fixed.html
186 lines (146 loc) · 4.42 KB
/
scatter_skeleton_fixed.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<!DOCTYPE html>
<!-- A modified example and dataset from Scott Murray's Knight d3 course. -->
<html lang="en">
<head>
<meta charset="utf-8">
<title>Formatting Ticks</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js"></script>
<style type="text/css">
body {
background-color: white;
font-family: Helvetica, Arial, sans-serif;
}
h1 {
font-size: 24px;
margin: 0;
}
p {
font-size: 14px;
margin: 10px 0 0 0;
}
svg {
background-color: white;
}
circle.dots {
fill: steelblue;
opacity: .7;
}
circle:hover {
fill: orange;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<h1>Life Satisfaction vs Homicide Rate</h1>
<p>Better Life Index “Life Satisfaction” scores vs. homicide rate. United States is highlighted. Source: <a href="http://stats.oecd.org/Index.aspx?DataSetCode=BLI">OECD</a>, 2014</p>
<script type="text/javascript">
// It is better to use the object style with margin.top, margin.right, etc.
var fullWidth = 700;
var fullHeight = 600;
var margin = {top:20, left:50, right:50, bottom: 50}; //Top, right, bottom, left
// what do you need to do to make the width and height for the chart?
var height = fullHeight - margin.top - margin.bottom;
var width = fullWidth - margin.left - margin.right;
var dotRadius = 4; // fix this to a nice value.
// fill in the margin values here.
var xScale = d3.scale.linear()
.range([0, width ]);
// top to bottom:
var yScale = d3.scale.linear()
.range([ height, 0]);
// Custom tick count if you want it.
// Create your axes here.
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(10); // fix this to a good number of ticks for your scale later.
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left");
var svg = d3.select("body")
.append("svg")
.attr("width", fullWidth)
.attr("height", fullHeight)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.csv("data/betterlifeindex.csv", function(data) {
// look at the data file and pick 2 columns to contrast with each other.
// if we use d3.extent here, there will be no padding -- the dots
// will appear on the axis lines.
xScale.domain([ // make an array of the min and max minus/plus some padding:
d3.min(data, function(d) {
return +d.homicideRate;
}) - 2,
d3.max(data, function(d) {
return +d.homicideRate;
}) + 2
]);
yScale.domain([
d3.min(data, function(d) {
return +d.lifeSatisfaction;
}) - 2,
d3.max(data, function(d) {
return +d.lifeSatisfaction;
}) + 2
]);
var circles = svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.classed("dots", true);
// Circles in SVG have cx, cy, and r attributes. x location, y location, radius.
circles.attr("cx", function(d) {
return xScale(+d.homicideRate);
// return the value to use for your x scale here
})
.attr("cy", function(d) {
return yScale(+d.lifeSatisfaction);
// return the value to use for your y scale here
})
.attr("r", dotRadius) // you might want to increase your dotRadius
.style("fill", function(d) {
if (d.country==="United States") {
return "red";
}
})
.append("title")
.text(function(d) {
return d.country;
});
// fix these translates so they use your margin and height width as needed
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("text")
.attr("class", "y label")
.attr("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.attr("x", -width/2)
.attr("y", 0)
.attr("dy", -30)
.text("Life Satisfaction (%)");
svg.append("text")
.attr("class", "xlabel")
.attr("transform", "translate(" + width/2 + " ," +
height + ")")
.style("text-anchor", "middle")
.attr("dy", "38")
.text("Homicide Rate");
});
</script>
</body>
</html>