-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminiproj_barchart(spanish).html
71 lines (61 loc) · 2.01 KB
/
miniproj_barchart(spanish).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
<html>
<head>
<script src="https://d3js.org/d3.v4.min.js"></script>
</head>
<style>
.bar {
fill: cyan;
}
</style>
<body>
<h1>Barchart of average letter count of spanish</h1>
<svg width="1000" height="600"></svg>
<script>
var svg = d3.select("svg"),
margin = 200,
width = svg.attr("width") - margin,
height = svg.attr("height") - margin;
var xScale = d3.scaleBand().range ([0, width]).padding(0.2),
yScale = d3.scaleLinear().range ([height, 0]);
var g = svg.append("g")
.attr("transform", "translate(" + 100 + "," + 100 + ")");
d3.csv("miniproj_spanish.csv", function(error, data) {
if (error) {
throw error;
}
xScale.domain(data.map(function(d) { return d.letter; }));
yScale.domain([0, d3.max(data, function(d) { return d.average; })]);
g.append("g")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(xScale))
.append("text")
.attr("y", height - 350)
.attr("x", width - 200)
.attr("stroke", "red")
.attr("font-size", "17")
.style("fill", "black")
.text("L E T T E R");
g.append("g")
.call(d3.axisLeft(yScale).tickFormat(function(d){
return d;
}).ticks(10))
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 30)
.attr("dy", "-5.1em")
.attr("stroke", "red")
.attr("font-size", "17")
.style("fill", "black")
.text("A V E R A G E");
g.selectAll(".bar")
.data(data)
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return xScale(d.letter); })
.attr("y", function(d) { return yScale(d.average); })
.attr("width", xScale.bandwidth()) //x-scale returns a calculated bandwidth from the range and padding provided to the x-scale.
.attr("height", function(d) { return height - yScale(d.average); });
});
</script>
</body>
</html>