-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathd3.html
153 lines (129 loc) · 4.75 KB
/
d3.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.group-tick line {
stroke: #000;
}
.ribbons {
fill-opacity: 0.67;
}
</style>
<svg width="960" height="960"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// see: https://bl.ocks.org/mbostock/4062006
// d16115 (dark brown)
// e0cf2c (mustard yellow)
// dbdcad (olive sand)
// 5a5f1a (dark pine)
// 95a41b (olive)
// 5d919e (prussian grey)
// 6ad6f6 (light sky)
// 21bff3 (corn flower)
// 18558a (navy)
// 11100b (almost black)
var states = [
"-",
"A",
"AB",
"ABG",
"B",
"BG",
"G",
"GM",
"M"
];
// from results/bayestraits.xlsx
// var matrix = [
// [ 0, 0.110984138, 0, 0, 0.048537373, 0, 0.165727939, 0, 1.460393911 ], // -
// [ 0.765113633, 0, 1.096692131, 0, 0, 0, 0, 0, 0 ], // A
// [ 0, 1.466857441, 0, 1.542454207, 5.211591027, 0, 0, 0, 0 ], // AB
// [ 0, 0, 17.79851693, 0, 0, 8.680574185, 0, 0, 0 ], // ABG
// [ 0.159232661, 0, 1.452145747, 0, 0, 0.432689728, 0, 0, 0 ], // B
// [ 0, 0, 0, 17.16701985, 6.466605543, 0, 1.489511162, 0, 0 ], // BG
// [ 0.220784764, 0, 0, 0, 0, 0.229683722, 0, 0.00265587, 0 ], // G
// [ 0, 0, 0, 0, 0, 0, 5.288188398, 0, 15.48081304 ], // GM
// [ 3.493255501, 0, 0, 0, 0, 0, 0, 16.32621259, 0 ] // M
// ];
// from qmatrix.tsv
var matrix = [
[ 0, 0.00172971827956989, 0, 0, 0.000478575483870968, 0, 0.00269221720430108, 0, 0.00106296688172043 ], // -
[ 0.00593668989247312, 0, 0.00380294494623656, 0, 0, 0, 0, 0, 0 ], // A
[ 0, 0.00255031612903226, 0, 0.00402177032258065, 0.0229881767741935, 0, 0, 0, 0 ], // AB
[ 0, 0, 0.0443766116129032, 0, 0, 0.0177643776344086, 0, 0, 0 ], // ABG
[ 0.000730130322580645, 0, 0.00786149161290323, 0, 0, 0.00233826021505376, 0, 0, 0 ], // B
[ 0, 0, 0, 0.0443971496774193, 0.0348801630107527, 0, 0.0390565582795699, 0, 0 ], // BG
[ 0.000785440860215053, 0, 0, 0, 0, 0.00186668688172043, 0, 4.64993548387097e-05, 0 ], // G
[ 0, 0, 0, 0, 0, 0, 0.0040559582795699, 0, 0.0142676012903226 ], // GM
[ 0.0106406787096774, 0, 0, 0, 0, 0, 0, 0.0187245049462365, 0 ] // M
];
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
outerRadius = Math.min(width, height) * 0.5 - 40,
innerRadius = outerRadius - 30;
var formatValue = d3.formatPrefix(",.0", 1e3);
var chord = d3.chord()
.padAngle(0.05)
.sortSubgroups(d3.descending);
var arc = d3.arc()
.innerRadius(innerRadius)
.outerRadius(outerRadius);
var ribbon = d3.ribbon()
.radius(innerRadius);
var color = d3.scaleOrdinal()
.domain(d3.range(9))
.range(["#d16115", "#e0cf2c", "#dbdcad", "#5a5f1a", "#95a41b", "#5d919e", "#6ad6f6", "#21bff3", "#18558a"]);
var g = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.datum(chord(matrix));
var group = g.append("g")
.attr("class", "groups")
.selectAll("g")
.data(function(chords) { return chords.groups; })
.enter().append("g");
group.append("path")
.style("fill", function(d) { return color(d.index); })
.style("stroke", function(d) { return d3.rgb(color(d.index)).darker(); })
.attr("d", arc);
var groupTick = group.selectAll(".group-tick")
.data(function(d) {
console.log(d);
return groupTicks(d, 1);
})
.enter().append("g")
.attr("class", "group-tick")
.attr("transform", function(d) { return "rotate(" + (d.angle * 180 / Math.PI - 90) + ") translate(" + outerRadius + ",0)"; });
groupTick.append("line")
.attr("x2", 6);
var i = 0;
groupTick
.filter(function(d) { return d.value % 5e3 === 0; })
.append("text")
.attr("x", 8)
.attr("dy", ".35em")
.attr("transform", function(d) { return d.angle > Math.PI ? "rotate(180) translate(-16)" : null; })
.style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; })
.text(function(d) {
var retval = states[i++];
return retval;
});
g.append("g")
.attr("class", "ribbons")
.selectAll("path")
.data(function(chords) { return chords; })
.enter().append("path")
.attr("d", ribbon)
.style("fill", function(d) { return color(d.target.index); })
.style("stroke", function(d) { return d3.rgb(color(d.target.index)).darker(); });
// Returns an array of tick angles and values for a given group and step.
function groupTicks(d, step) {
var k = (d.endAngle - d.startAngle) / d.value;
return d3.range(0, d.value, step).map(function(value) {
return {value: value, angle: value * k + d.startAngle};
});
}
</script>