-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatrix.html
233 lines (194 loc) · 8.72 KB
/
matrix.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>LSST Dark Matter Graphic: Matrix View</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.min.css" type="text/css">
<link rel="stylesheet" href="static/style_common.css" type="text/css">
<style>
.cat_label {
text-anchor: start;
}
.cell,
.label {
cursor: pointer;
}
.label {
text-anchor: end;
font-size: 13px;
dominant-baseline: middle;
}
.background {
fill: #eee;
}
.background_diag {
fill: #ddd;
}
.line {
fill: #fff;
}
.label.locked {
fill: #9467bd;
}
.label.active {
fill: #d62728;
}
</style>
</head>
<body>
<div class="footer">
<a href="index.html">Home</a> |
<a href="javascript:void load_instruction('matrix');">Instructions</a> |
<a href="network.html">Network diagram</a> |
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfkUCE7o8cqQQV9PFki484sSqRzelTDEk1SXtwb7I2d4gxxTw/viewform">Submit idea</a> |
<a href="https://github.com/lsstdarkmatter/dark-matter-graph">GitHub Repo</a>
</div>
<div>
<svg id="canvas"></svg>
</div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="static/script_common.js"></script>
<script>
load_instruction("matrix");
d3.json('data/data.json', function (error, d) {
if (error) throw error;
d.categories.forEach(c => { c.count = 0; });
d.nodes.forEach(n => { d.categories[n.category].count += 1; });
const cat_boundary = d.categories.reduce((acc, cur, i) => {
if (i) acc.push(d.categories[i - 1].count + acc[acc.length - 1]); return acc;
}, [0]);
const dims = { 'cell_side': 18, 'label_top': 170, 'label_left': 200, 'margin_top': 30, 'label_pad': 6, 'lw': 2, 'lw_cat': 8 };
dims.side = dims.cell_side * d.nodes.length + dims.lw * d.nodes.length + dims.lw_cat * (d.categories.length - 1);
dims.cell_step = dims.cell_side + dims.lw;
dims.cell_half_side = dims.cell_side / 2;
dims.width = dims.side + dims.label_left + dims.label_pad;
dims.height = dims.side + dims.label_top + dims.label_pad + dims.margin_top;
let get_color_index = l => ((l.direct ? d.nodes[l.left].category : 7) * 2 + 1);
const cells = d.links
.map(l => ({ 'x': l.left, 'y': l.right, 'c': get_color_index(l), 'paths': l.paths }))
.concat(d.links
.map(l => ({ 'x': l.right, 'y': l.left, 'c': get_color_index(l), 'paths': l.paths })));
const lines = d.nodes
.map((n, i) => ({ 'index': i, 'vertical': true, 'cat_boundary': (i && cat_boundary[n.category + 1] == i + 1) }))
.concat(d.nodes
.map((n, i) => ({ 'index': i, 'vertical': false, 'cat_boundary': (i && cat_boundary[n.category + 1] == i + 1) })));
const highlight = (type, d_this, i, to_lock = false) => {
var cell, row_label, col_label;
if (type == 'row_label') {
row_label = row_labels.filter((_, j) => j == i);
cell = cell_objs.filter(c => c.y == i);
col_label = col_labels.filter((_, j) => cells.some(c => (c.y == i) && (c.x == j)));
}
else if (type == 'col_label') {
col_label = col_labels.filter((_, j) => j == i);
cell = cell_objs.filter(c => c.x == i);
row_label = row_labels.filter((_, j) => cells.some(c => (c.x == i) && (c.y == j)));
}
else if (type == 'cell') {
row_label = row_labels.filter((_, j) => j == d_this.y);
col_label = col_labels.filter((_, j) => j == d_this.x);
cell = cell_objs.filter(c => c == d_this);
}
row_label.classed(to_lock ? " locked" : "active", true);
col_label.classed(to_lock ? " locked" : "active", true);
cell.attr("fill", c => d3.schemeCategory20[c.c - 1]);
if (to_lock) cell.classed("locked", true);
};
const clearall = function (to_unlock = false) {
row_labels.classed("active" + (to_unlock ? " locked" : ""), false);
col_labels.classed("active" + (to_unlock ? " locked" : ""), false);
if (to_unlock) cell_objs.classed("locked", false);
cell_objs.attr("fill", function (c) { return d3.schemeCategory20[c.c - (d3.select(this).classed("locked"))]; });
}
const itemclick = function (type, d_this, i) {
const ctrl_pressed = d3.event.ctrlKey || d3.event.metaKey;
clearall(!ctrl_pressed);
highlight(type, d_this, i, true);
if (type != "cell") show_sidebar(d_this, false, ctrl_pressed); else hide_sidebar();
d3.event.stopPropagation();
}
const svg = d3.select("svg#canvas")
.attr("width", dims.width)
.attr("height", dims.height)
.on("click", () => { hide_sidebar(); clearall(true); });
const matrix = svg.append("g")
.attr("transform", "translate(" + (dims.width - dims.side) + "," + (dims.height - dims.side) + ")");
matrix.append("rect")
.attr("class", "background")
.attr("width", dims.side)
.attr("height", dims.side);
matrix.selectAll(".background_diag")
.data(d.categories)
.enter().append("rect")
.attr("class", "background_diag")
.attr("width", c => dims.cell_step * c.count)
.attr("height", c => dims.cell_step * c.count)
.attr("x", (_, i) => dims.cell_step * cat_boundary[i] + dims.lw_cat * i)
.attr("y", (_, i) => dims.cell_step * cat_boundary[i] + dims.lw_cat * i);
const calc_shift = index => index * dims.cell_step + cat_boundary.reduce((acc, cur) => acc + (index >= cur), -1) * dims.lw_cat;
const cell_objs = matrix.selectAll('.cell')
.data(cells)
.enter().append("rect")
.attr("class", "cell")
.attr("width", dims.cell_side)
.attr("height", dims.cell_side)
.attr("x", l => calc_shift(l.x))
.attr("y", l => calc_shift(l.y))
.attr("fill", l => d3.schemeCategory20[l.c])
.on("mouseover", (l, i) => { highlight("cell", l, i); })
.on("click", (l, i) => { itemclick("cell", l, i); })
.on("mouseout", l => { clearall(); });
matrix.selectAll(".line")
.data(lines)
.enter().append("rect")
.attr("class", "line")
.attr("x", l => l.vertical ? (calc_shift(l.index) + dims.cell_side) : 0)
.attr("y", l => l.vertical ? 0 : (calc_shift(l.index) + dims.cell_side))
.attr("width", l => l.vertical ? (l.cat_boundary ? (dims.lw + dims.lw_cat) : dims.lw) : dims.side)
.attr("height", l => l.vertical ? dims.side : (l.cat_boundary ? (dims.lw + dims.lw_cat) : dims.lw));
const row_padding = dims.cell_half_side + dims.height - dims.side;
const row_group = svg.append("g")
.attr("transform", "translate(" + dims.label_left + "," + row_padding + ")");
const row_labels = row_group.selectAll('.row_label')
.data(d.nodes)
.enter().append("text")
.attr("class", "label row_label")
.attr("y", (_, i) => calc_shift(i))
.text(n => n.label)
.on("mouseover", (l, i) => { highlight("row_label", l, i); })
.on("click", (l, i) => { itemclick("row_label", l, i); })
.on("mouseout", l => { clearall(); });
const col_padding = dims.cell_half_side + dims.width - dims.side + 1;
const col_group = svg.append("g")
.attr("transform", "translate(" + col_padding + "," + (dims.label_top + dims.margin_top) + ")");
const col_labels = col_group.selectAll('.col_label')
.data(d.nodes)
.enter().append("text")
.attr("class", "label col_label")
.text(n => n.label)
.attr("transform", (_, i) => "translate(" + calc_shift(i) + ")rotate(60)")
.on("mouseover", (l, i) => { highlight("col_label", l, i); })
.on("click", (l, i) => { itemclick("col_label", l, i); })
.on("mouseout", l => { clearall(); });
const cat_label_g = svg.append("g")
.attr("transform", "translate(" + (dims.label_left + dims.label_pad) + "," + (dims.margin_top - 5) + ")");
cat_label_g.selectAll(".cat_label")
.data(d.categories)
.enter().append("text")
.attr("class", "cat_label")
.attr("x", (_, i) => calc_shift(cat_boundary[i]))
.text(c => c.label)
.on("mouseover", c => { show_tooltip(c.description); })
.on("mouseout", hide_tooltip);
cat_label_g.selectAll(".cat_label")
.each(function () {
const bbox = this.getBBox();
const yy = bbox.y + bbox.height + 1;
cat_label_g.append("line").attr("class", "cat_underline")
.attr("x1", bbox.x).attr("x2", bbox.x + bbox.width)
.attr("y1", yy).attr("y2", yy);
});
});
</script>
</body>
</html>