forked from Rayraegah/bias-viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
302 lines (265 loc) · 8.3 KB
/
main.js
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import './style.css';
import brain from './brain.png';
import data from './data.json';
import * as d3 from 'd3';
// define helper functions
// get the angle between two points
const getAngle = (anchor, point) => {
let a = Math.atan2(point.y - anchor.y, point.x - anchor.x);
if (a < 0) a += 2 * Math.PI; //angle is now in radians
a -= Math.PI / 2; //shift by 90deg
//restore value in range 0-2pi instead of -pi/2-3pi/2
if (a < 0) a += 2 * Math.PI;
if (a < 0) a += 2 * Math.PI;
a = Math.abs(Math.PI * 2 - a); //invert rotation
a = (a * 180) / Math.PI; //convert to deg
return a;
};
// rotate a point
const radialPoint = (x, y) => [
(y = +y) * Math.cos((x -= Math.PI / 2)),
y * Math.sin(x)
];
// rotate a point scaling the radius
const nodePoint = (x, y, scale) => [
y * Math.cos((x -= Math.PI / 2)) * scale,
y * Math.sin(x) * scale
];
// get the distance between two points
const getHypot = (p1, p2) => Math.hypot(p2.x - p1.x, p2.y - p1.y);
// define variables
const w = window.innerWidth;
const h = window.innerHeight;
const margin = { top: 25, right: 80, bottom: 20, left: 80 };
const width = w - margin.right - margin.left;
const height = h - margin.top - margin.bottom;
const radius = (height / 2) * 0.8;
const center = { x: (w / 2) * 0.95, y: h / 2 };
const scale = 2.3;
const biasWidth = 210;
let biasTitleWidth = 150;
const biasTitlesPos = [
{ x: width - width / 4 + biasTitleWidth, y: height / 4 - 10 },
{
x: width - width / 4 + biasTitleWidth + 30,
y: height - height / 4 + 15
},
{ x: width / 4 - biasTitleWidth - 30, y: height - height / 4 },
{ x: width / 4 - biasTitleWidth - 50, y: height / 4 - 30 }
];
// const footerText = ``;
// initialize layout items
// initialize bias tree object
const tree = d3
.tree()
.size([2 * Math.PI, (radius / scale) * 1.6])
.separation((a, b) => (a.parent == b.parent ? 1 : 2) / a.depth);
// initialize svg object
const svg = d3
.select('.main')
.append('svg')
.attr('width', width + margin.right + margin.left)
.attr('height', height + margin.top + margin.bottom);
// insert header
const header = d3
.select('.main')
.append('h1')
.text('Cognitive Bias Codex');
// place brain image
const brainImage = d3
.select('.main')
.append('img')
.attr('src', brain)
.attr('class', 'brain-image')
.attr('style', () => {
const width = 50 * scale * (radius / (radius * 0.8));
const height = 40 * scale * (radius / (radius * 0.8));
const left = center.x - width / 2;
const top = center.y - height / 2;
return `left: ${left}px; top: ${top}px; width: ${width}px; height: ${height}px`;
});
/*const footer = d3
.select('.main')
.append('div')
.html(footerText)
.attr('class', 'footer');
*/
// initialize svg group object
const g = svg
.append('g')
.attr('transform', `translate(${center.x},${center.y})`);
// load the external data
/*d3.json("data.json", function(error, data) {
root = data;
update(root);
});*/
const root = data;
update(root);
function update(source) {
// update data array to include id
Array.from(root.children).forEach((problem, ix) => {
problem['id'] = ix;
problem.children.forEach(bias => {
bias['id'] = ix;
bias.children.forEach(value => {
value['id'] = ix;
});
});
});
// declares a tree layout and assigns the size
let nodes = d3.hierarchy(root, ({ children }) => children);
nodes = tree(nodes);
// define the tree links
const link = g
.selectAll('.link')
.data(nodes.links())
.enter()
.append('path')
.attr('class', ({ target }) => `link link-${target.data.id}`)
.attr(
'd',
d3
.linkRadial()
.angle(({ x }) => x)
.radius(({ y }) => y)
);
g.append('circle')
.attr('r', () => {
const nodes2Tier = nodes
.descendants()
.filter(({ depth }) => depth == 2);
const p1 = nodes2Tier[0];
const p2 = nodePoint(p1.x, p1.y, scale);
const rad = getHypot({ x: 0, y: 0 }, { x: p2[0], y: p2[1] });
return rad;
})
.style('fill', 'none')
.attr('class', 'main-circle');
// create node groups
const node = g
.selectAll('.node')
.data(nodes.descendants())
.enter()
.append('g')
.attr('class', d => `node node-${d.data.id}`)
.attr('transform', ({ depth, x, y }) => {
if (depth == 3) {
return `translate(${radialPoint(x, y)})`;
}
});
node.append('circle')
.attr('r', ({ depth }) => {
if (depth == 3) {
return 2;
} else if (depth == 2) {
return 5;
} else {
return 0;
}
})
.attr('cx', ({ depth, x, y }) => {
if (depth == 2) {
const point = nodePoint(x, y, scale);
return point[0];
}
})
.attr('cy', ({ depth, x, y }) => {
if (depth == 2) {
const point = nodePoint(x, y, scale);
return point[1];
}
})
.attr('stroke', 'white')
.attr('stroke-width', 1);
node.append('text')
.attr('dy', '0.31em')
.attr('x', ({ x, children }) => (x < Math.PI === !children ? 6 : -6))
.attr('text-anchor', ({ x, children }) =>
x < Math.PI === !children ? 'start' : 'end'
)
.attr(
'transform',
({ x }) =>
`rotate(${((x < Math.PI ? x - Math.PI / 2 : x + Math.PI / 2) *
180) /
Math.PI})`
)
.text(d => {
if (d.depth == 3) {
return d.data.name;
}
})
.style('font-size', () => {
const size = radius / (radius * scale);
return `${size.toFixed(2)}em`;
});
d3.select('.biases').attr(
'style',
() => `left: ${center.x}px; top: ${center.y}px;`
);
// insert bias text
const bias = d3
.select('.biases')
.selectAll('.bias')
.data(() => {
const nodesBiases = nodes
.descendants()
.filter(({ depth }) => depth == 2);
return nodesBiases;
})
.enter()
.append('div')
.attr('class', 'bias')
.attr('style', d => {
const point = nodePoint(d.x, d.y, scale);
const stringLen = d.data.name.length;
const angle = getAngle(
{ x: 0, y: 0 },
{ x: point[0], y: point[1] }
);
let align;
let left;
let top;
if (angle > 180 && angle < 360) {
align = 'right';
left = point[0] - biasWidth - 15;
} else {
align = 'left';
left = point[0] + 15;
}
if (stringLen > 50 && stringLen < 80) {
top = point[1] - 10;
} else if (stringLen > 80) {
top = point[1] - 15;
} else {
top = point[1] - 8;
}
return `left: ${left}px; top: ${top}px; text-align: ${align}; width: ${biasWidth}px;`;
})
.append('span')
.attr('class', d => `text text-${d.data.id}`)
.text(d => d.data.name);
// insert bias titles
const biasTitle = d3
.select('.main')
.selectAll('.bias-title')
.data(() => {
const nodeTitles = nodes
.descendants()
.filter(({ depth }) => depth == 1);
return nodeTitles;
})
.enter()
.append('div')
.attr('class', d => `bias-title node-${d.data.id}`)
.attr('style', (d, ix) => {
const stringLen = d.data.name.length;
if (stringLen < 20) {
biasTitleWidth = 90;
} else if (stringLen > 25) {
biasTitleWidth = 200;
}
return `left: ${biasTitlesPos[ix].x}px; top: ${biasTitlesPos[ix].y}px; width: ${biasTitleWidth}px;`;
})
.text(d => d.data.name);
}