-
Notifications
You must be signed in to change notification settings - Fork 0
/
charts.html
80 lines (64 loc) · 2.16 KB
/
charts.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.chart rect {
fill: steelblue;
}
.chart text {
fill: white;
font: 14px sans-serif;
text-anchor: end;
}
.label text {
fill: black;
font: 14px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<svg class="chart"></svg>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
var margin = {top: 20, right: 30, bottom: 30, left: 240};
var width = 1200 - margin.left - margin.right,
barHeight = 25;
var x = d3.scale.linear()
.range([0, width]);
var chart = d3.select(".chart")
.attr("width", width + margin.left + margin.right);
var g = chart.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.tsv("traits.tsv", type, function(error, data) {
x.domain([0, d3.max(data, function(d) { return d.frequency; })]);
var height = (barHeight * data.length) + margin.top + margin.bottom;
chart.attr("height", height);
var bar = g.selectAll("g")
.data(data)
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * barHeight + ")"; });
bar.append("rect")
.attr("width", function(d) { return x(d.frequency); })
.attr("height", barHeight - 1);
bar.append("text")
.attr("x", function(d) { return x(d.frequency) - 3; })
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.text(function(d) { return d.frequency; });
bar.append("g").attr("class", "label")
.append("svg:text")
.attr("class", "label")
.attr("x", -6)
.attr("y", barHeight / 2)
.attr("dy", ".35em")
.attr("text-anchor", "end")
.text(function(d) { return d.trait; });
});
function type(d) {
d.frequency = +d.frequency; // coerce to number
return d;
}
</script>