-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscatter_plot.html
More file actions
111 lines (95 loc) · 2.84 KB
/
Copy pathscatter_plot.html
File metadata and controls
111 lines (95 loc) · 2.84 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Test</title>
<style>
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<!-- Link to D3.js library -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script type="text/javascript">
var LINES = 50;
// Width and height
var w = 500;
var h = 500;
var padding = 30;
// Get fake data
var dataset = generateData();
// Create SVG element
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var xScale = d3.scale.linear()
.domain([0, 50])
.range([padding, w - padding]);
var yScale = d3.scale.linear()
.domain([-1, 1])
.range([h - padding, padding]);
var xAxis = d3.svg.axis()
.scale(xScale)
.orient("bottom")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(yScale)
.orient("left")
.ticks(5);
svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", function(d) {
return xScale(d[0]); // position on x-axis
})
.attr("cy", function(d) {
return yScale(d[1]); // position on y-axis
})
.attr("r", 2);
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) {
return d[0] + "," + d[1]; // "x,y"
})
.attr("x", function(d) {
return xScale(d[0]); // position on x-axis
})
.attr("y", function(d) {
return yScale(d[1]); // position on x-axis
})
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "red");
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(xAxis);
svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
// .attr("fill", "red")
.call(yAxis);
// Helper function
function generateData() {
var fakeData = [];
for (var i = 0; i < LINES; i++) {
fakeData.push([i, (Math.random() * 2) - 1]);
}
return fakeData;
};
</script>
</body>
</html>