-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_barchart.html
More file actions
76 lines (70 loc) · 1.48 KB
/
index_barchart.html
File metadata and controls
76 lines (70 loc) · 1.48 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
<!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.graph{
width:0px;
height:0px;
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
margin: auto;
}
</style>
</head>
<body>
<div class= "graph">
<script type="text/javascript">
var dataset = [5, 10, 13, 19, 21, 25, 22, 18, 15, 13, 11, 12, 15, 20, 18, 17, 16, 18, 23, 25];
var w = 500;
var h = 100;
var barPadding = 1;
var svg = d3.select(".graph")
.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 * 4;
})
.attr("width", w / dataset.length - barPadding)
.attr("height", function(d){
return d * 4;
});
svg.selectAll("rect")
.attr("fill",function(d){
return "rgb(" + (d*10) + ",0, 0)";
});
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d){
return d;
})
.attr("x",function(d,i){
return i * (w/dataset.length) + (w/dataset.length - barPadding) /2;
})
.attr("y",function(d){
return h - (d*4) + 14;
})
.attr("font-family","sans-serif")
.attr("font-size","14px")
.attr("fill", "white")
.attr("text-anchor","middle")
;
</script>
</div>
</body>
</html>