-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscatterplot.html
192 lines (170 loc) · 5.75 KB
/
scatterplot.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
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: SVG bar chart with value labels</title>
<script type="text/javascript" src="../d3/d3.js"></script>
<style type="text/css">
.axis path, .axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
text {
fill: olive;
}
</style>
</head>
<body>
<p>Click on this text to update the chart with new data values as many times as you like!</p>
<script type="text/javascript">
var w = 500;
var h = 300;
var padding = 30;
// Static Data Set
// var dataset = [
// [ 5, 20 ],
// [ 480, 90 ],
// [ 250, 50 ],
// [ 100, 33 ],
// [ 330, 95 ],
// [ 410, 12 ],
// [ 475, 44 ],
// [ 25, 67 ],
// [ 85, 21 ],
// [ 220, 88 ],
// [ 600, 150]
// ];
// Dynamic Data Set
var dataset = [];
var numDataPoints = 50;
var maxRange = Math.random() * 1000;
for (var i = 0; i < numDataPoints; i++) {
var newNumber1 = Math.floor(Math.random() * maxRange);
var newNumber2 = Math.floor(Math.random() * maxRange);
dataset.push([newNumber1, newNumber2]);
}
// Creating scale functions
var xScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[0]; })])
.range([padding, w - padding * 2]);
var yScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[1]; })])
.range([h - padding, padding]);
var rScale = d3.scale.linear()
.domain([0, d3.max(dataset, function(d) { return d[1]; })])
.range([2, 5]);
var formatAsPercentage = d3.format(".1%");
// Setting up an Axis
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("button")
.ticks(5);
// .tickFormat(formatAsPercentage);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
// .tickFormat(formatAsPercentage);
// Creating SVG elements
var svg = d3.select("body").append("svg").attr("width", w).attr("height", h);
//Defining clipping path
svg.append("clipPath")
.attr("id", "chart-area")
.append("rect")
.attr("x", padding)
.attr("y", padding)
.attr("width", w - padding * 3)
.attr("height", h - padding * 2);
//Creating circles
svg.append("g")
.attr("id", "circles")
.attr("clip-path", "url(#chart-area)")
.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
})
.attr("r", function(d) {
return rScale(d[1]);
});
// Commented this to remove the labels
// Labels on the data points
// *
// svg.selectAll("text").data(dataset).enter().append("text").text(function(d) {
// return d[0] + "," + d[1];
// }).attr("x", function(d) {
// return xScale(d[0]);
// }).attr("y", function(d) {
// return yScale(d[1]);
// }).attr("font-family", "sans-serif").attr("font-size", "11px").attr("fill", "red");
// *
//Creating X axis and Y axis
svg.append("g")
.attr("class", "x axis") // Assiging an 'axis' class
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.attr("transform", "translate(" + padding + ",0)")
.call(yAxis);
d3.select("p")
.on("click", function() {
//New values for dataset
var numValues = dataset.length; //Count original length of dataset
var maxRange = Math.random() * 1000; //Max range of new values
dataset = []; //Initialize empty array
for (var i = 0; i < numValues; i++) { //Loop numValues times
var newNumber1 = Math.floor(Math.random() * maxRange); //New random integer
var newNumber2 = Math.floor(Math.random() * maxRange); //New random integer
dataset.push([newNumber1, newNumber2]); //Add new number to array
}
//Update scale domains
xScale.domain([0, d3.max(dataset, function(d) { return d[0]; })]);
yScale.domain([0, d3.max(dataset, function(d) { return d[1]; })]);
//Update all circles
svg.selectAll("circle")
.data(dataset)
.transition()
.duration(1000)
.each("start", function() {
d3.select(this)
.attr("fill", "magenta")
.attr("r", 7);
})
.each("end", function() {
d3.select(this)
.transition()
.duration(500)
.attr("fill", "black")
.attr("r", 2);
})
.attr("cx", function(d) {
return xScale(d[0]);
})
.attr("cy", function(d) {
return yScale(d[1]);
});
//Update X axis
svg.select(".x.axis")
.transition()
.duration(1000)
.call(xAxis);
//Update Y axis
svg.select(".y.axis")
.transition()
.duration(1000)
.call(yAxis);
});
</script>
</body>
</html>