-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple1.html
96 lines (76 loc) · 2.08 KB
/
simple1.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type='text/javascript' src="http://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>
<script type='text/javascript' src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<style type="text/css">
#graph {
float: left;
position: relative;
margin: 20px;
}
rect {
stroke: white;
fill: steelblue;
}
</style>
</head>
<body>
<div id='graph'></div>
<script type="text/javascript">
$(document).ready(function(){
// https://strongriley.github.io/d3/tutorial/bar-1.html
var width = 800;
var height = 1000;
var svg = d3.select("#graph").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("g").attr("class", "bars");
svg.append("g").attr("class", "xaxis");
var dataset = [ 5, 10, 13, 19, 21, 25, 22, 18, 15, 18, 23, 25 ];
var xScale = d3.scale.linear()
.domain([0, 30])
.range([0, 500]);
var xaxis = svg.select(".xaxis").selectAll("g")
.data(xScale.ticks(10))
.enter()
.append("g");
xaxis.append("line")
.attr("x1", xScale)
.attr("x2", xScale)
.attr("y1", 0)
.attr("y2", 20 * dataset.length)
.attr("stroke", "#ccc");
xaxis.append("text")
.attr("x", xScale)
.attr("y", 20 * dataset.length + 10)
.attr("dy", 5)
.attr("text-anchor", "middle")
.text(function(d) { return d;});
var bars = svg.select(".bars").selectAll("g")
.data(dataset)
.enter()
.append("g");
bars.append("rect")
.attr("y", function(d, i) { return i * 20; })
.attr("width", function(d) { return xScale(d); })
.attr("height", 20)
.attr("fill", "steelblue");
bars.append("text")
.text(function(d) { return d; })
.attr("text-anchor", "end")
.attr("x", function(d) { return xScale(d); })
.attr("y", function(d, i) { return (i+1)*20 - 5; })
.attr("dx", -5)
.attr("font-family", "sans-serif")
.attr("font-size", "10px")
.attr("fill", "white");
svg.append("line")
.attr("y1", 0)
.attr("y2", 20 * dataset.length)
.attr("stroke", "#000");
})
</script>
</body>
</html>