-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharting_ex2.html
More file actions
58 lines (46 loc) · 1.68 KB
/
charting_ex2.html
File metadata and controls
58 lines (46 loc) · 1.68 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="description" content="Drawing Shapes"/>
<meta charset="utf-8">
<title>Bar Graph with Labels</title>
<script src="https://d3js.org/d3.v6.min.js" charset="utf-8"></script>
</head>
<body>
<script> //bar chart
var w = 200;
var h = 100;
var padding = 2;
var dataset = [5,10,15,20,25];
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){ //i is index of dataset d is the dataset being passed in
return (i*(w / dataset.length)); // use dataset and evenly divide out five bars
})
.attr("y",function(d){ //coordinate system appears in top left, to make it appear normally subtract data from height
return h-d*4;
})
.attr("width",(w / dataset.length-padding)) // five bars that take up the width of the svg
.attr("height",function(d){
return d*4;
})
.attr("fill", function(d) {
return "rgb(" + (d*10) + ", 0, 0)"; //shades of red
})
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 - padding) / 2;})
.attr("y", function(d) {return h - (d * 4) + 14;})
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "11px")
.attr("fill", "white")
</script>
</body>
</html>