-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscatterplot2.html
89 lines (76 loc) · 2.34 KB
/
scatterplot2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>scatterplot second step</title>
<script type="text/javascript" src="d3.js"></script>
<style type="text/css">
p{
padding: 10px
}
</style>
</head>
<body>
<p>
Not so many changes, exepted applied scale() function which allows to...scale:
<br> to link the difference between smaller and greater x and y values to the width and height of the picture (window).
<br> When the value is too important (what if you have a billion of beans?) it allows to "keep" it within the window.
<br> The distribution is inverted: now, following some visual logic (this one is very important) the smaller values
are placed closer to beginning on <em>x</em> and <em>y</em> axis.
</p>
<script type="text/javascript">
//Width and height
var w = 600;
var h = 300;
var padding = 65;
var dataset = [
[5, 20], [480, 90], [250, 50], [100, 33], [330, 95],
[410, 12], [475, 44], [25, 67], [85, 21], [220, 88]
];
//Create scale functions
var xScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) { return d[0]; })])
.range([padding, w - padding]);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) { return d[1]; })])
.range([h - padding, padding]);
//Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
//Create circles
svg.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 Math.sqrt(h - d[1]+d[0])
})
.style ("fill", "rgb(150, 10, 150, 0.5)");
//Create labels
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d[0] + " / " + d[1];
})
.attr("x", function(d) {
return xScale(d[0]+25);
})
.attr("y", function(d) {
return yScale(d[1]);
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "red");
</script>
</body>
</html>