-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbar_scatterplot.html
More file actions
126 lines (111 loc) · 4.22 KB
/
Copy pathbar_scatterplot.html
File metadata and controls
126 lines (111 loc) · 4.22 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 test</title>
<script type="text/javascript" src="d3/d3.v3.js"></script>
<style type="text/css">
div.bar {
display: inline-block;
width: 20px;
height: 75px;
background-color: teal;
}
.axis path,
.axis line {
fill: none;
stroke: black;
shape-rendering: crispEdges;
}
.axis text {
font-family: sans-serif;
font-size: 11px;
}
</style>
</head>
<body>
<script type="text/javascript">
var w = 500;
var h = 250;
var padding = 30;
var bar_padding = 1;
// bar chart
var dataset = [];
for (var i = 0; i < 20; i++){
var new_num = Math.round(Math.random() * 30);
dataset.push(new_num);
}
var svg = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
svg.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("x", function(d, i) {
return i * (w / dataset.length);
})
.attr("y", function(d) { return h - d; })
.attr("width", w / dataset.length - bar_padding)
.attr("height", function(d) { return d; })
.attr("fill", "teal");
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d) { return d; })
.attr("x", function(d, i) {
var bar_width = w / dataset.length
return i * bar_width + (bar_width - bar_padding) / 2;
})
.attr("y", function(d) {return h - d; })
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif");
// scatterplot
var dataset2 = [];
for (var i = 0; i < 20; i++){
var new_x = Math.round(Math.random() * 20);
var new_y = Math.round(Math.random() * 20);
dataset2.push([new_x, new_y]);
}
var scale_x = d3.scale.linear()
.domain([0, d3.max(dataset2, function(d) {
return d[0]; })])
.range([padding, w - padding])
.nice();
var scale_y = d3.scale.linear()
.domain([0, d3.max(dataset2, function(d) {
return d[1]; })])
.range([h - padding, padding])
.nice();
var svg2 = d3.select("body")
.append("svg")
.attr("width", w)
.attr("height", h);
var axis_x = d3.svg.axis()
.scale(scale_x)
.orient("bottom")
.ticks(5);
var axis_y = d3.svg.axis()
.scale(scale_y)
.orient("left")
.ticks(5);
svg2.selectAll("circle")
.data(dataset2)
.enter()
.append("circle")
.attr("cx", function(d) { return scale_x(d[0]); })
.attr("cy", function(d) { return scale_y(d[1]); })
.attr("r", 5);
svg2.append("g")
.attr("class", "axis")
.attr("transform", "translate(0," + (h - padding) + ")")
.call(axis_x);
svg2.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + padding + ",0)")
.call(axis_y);
</script>
</body>
</html>