Newer
Older
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<template>
<section id="beeswarm">
<div id="toggle-container" >
<div id="checkbox1-container">
<input type="checkbox" id="checkbox1" v-model="checkboxStates[0]" @change="handleCheckboxChange(0)">
<label for="checkbox1">Evidence</label>
</div>
<div id="checkbox2-container">
<input type="checkbox" id="checkbox2" v-model="checkboxStates[1]" @change="handleCheckboxChange(1)">
<label for="checkbox2">Agreement</label>
</div>
<div id="checkbox3-container">
<input type="checkbox" id="checkbox3" v-model="checkboxStates[2]" @change="handleCheckboxChange(2)">
<label for="checkbox3">Direction</label>
</div>
</div>
<div id="beeswarm-chart-container"></div>
</section>
</template>
<script setup>
import { onMounted, ref, watch } from "vue";
import * as d3 from 'd3';
// global variables
const publicPath = import.meta.env.BASE_URL;
const dataSet1 = ref([]);
const dataSet2 = ref([]);
const selectedDataSet = ref('dataSet1');
const checkboxStates = ref([false, false, false]);
const checkboxFunctions = [sizeByEvidence, positionByAgreement, sortByDirection];
const categoryCenters = {}
let data = dataSet1;
let simulation;
// set up svg
let svg;
const height = 800;
const width = 800;
let margin = {top: 30, right: 20, bottom: 20, left: 30}
// set colors for bubble charts
const dimensionColors = {
Demographiccharacteristics: "#625D0B",
Landtenure: "#5C0601",
Livingconditions: "#0B4E8B",
Socioeconomicstatus: "#DC8260",
Health: "#7F4A89",
Riskperception: "#249CB1",
Exposure: "#B47D83"
}
// load data and then make chart
onMounted(() => {
console.log("component mounted");
loadDatasets().then(() => {
if (selectedDataSet.value.length > 0) {
createBeeswarmChart(selectedDataSet);
} else {
console.error('Error loading data:', error)
}
});
});
async function loadDatasets() {
dataSet1.value = await loadData('determinant_uncertainty.csv');
dataSet2.value = await loadData('indicator_uncertainty.csv')
}
async function loadData(fileName) {
return await d3.csv(publicPath + fileName, d => {
d.level_agreement = +(+d.level_agreement).toFixed(2);
d.evidence_val = +d.evidence_val;
d.sig_value = +d.sig_value;
return d;
});
};
// helper function to set up mouse events
function createMouseEvents() {
const tooltip = d3.selectAll('#beeswarm-chart-container')
// const tooltip = d3.select("body")
.append("div")
.attr("class", "tooltip")
.style("opacity", 0);
function handleMouseOver(event, d) {
tooltip
.transition().duration(200)
.style("opacity", 1);
}
function handleMouseMove(event, d) {
const [x, y] = d3.pointer(event);
// console.log(x, y);
tooltip
.html(`${d.determinant}, Level of Agreement: ${d.level_agreement}`)
.style("left", x + "px")
.style("top", y + "px");
// console.log("left/X: ", x, " right/Y: ", y);
d3.selectAll(".bubble")
.filter(function(e) { return e.dimension !== d.dimension; }) // change the opacity of bubbles that do not have the same dimension as the selected bubble
.style("opacity", 0.3);
}
function handleMouseOut(d) {
tooltip
.transition().duration(200)
.style("opacity", 0);
d3.selectAll(".bubble")
.filter(function(e) { return e.dimension !== d.dimension; })
.style("opacity", 1);
}
return {handleMouseOver, handleMouseMove, handleMouseOut}
}
function handleCheckboxChange(index) {
// Call the corresponding function based on the index
checkboxFunctions[index](checkboxStates.value[index]);
}
// call helper function
const { handleMouseOver, handleMouseMove, handleMouseOut } = createMouseEvents();
function createScales(data) {
// set x axis scale based on level of agreement
const xScale = d3.scaleLinear()
.domain([40, d3.max(data.value, d => d.level_agreement)])
.range([margin.left+ 125, width-100]);
// set radius based on evidence value
const radiusScale = d3.scaleLinear()
.domain([d3.min(data.value, d => d.evidence_val), d3.max(data.value, d => d.evidence_val)])
.range([10, 50]);
return { xScale, radiusScale}
}
function createBeeswarmChart() {
svg = d3
.selectAll('#beeswarm-chart-container')
.append('svg')
.attr('class', 'beeswarmSvg')
.attr('width', width)
.attr('height', height)
const radius = 10
const categories = Array.from(new Set(data.value.map((d) => d.dimension)))
const numRows = 2 //Math.ceil(Math.sqrt(categories.length)); // Number of rows for grid layout
const numCols = 4 //Math.ceil(categories.length / numRows); // Number of columns
const colWidth = width / numCols
const rowHeight = height / numRows
categories.forEach((category, index) => {
let col = index % numCols // Column index
let row = Math.floor(index / numCols) // Row index
// Offset columns in the second row
if (row === 1) {
col += 0.5 // Offset by half a column width
}
// Calculate the center position
categoryCenters[category] = {
x: colWidth * col + colWidth / 2,
y: rowHeight * row + rowHeight / 2
}
})
data.value.forEach((d) => {
// Add randomness to the initial positions
const center = categoryCenters[d.dimension]
d.x = center.x + (Math.random() - 0.5) * 50 // Adjust randomness factor (50 here) as needed
d.y = center.y + (Math.random() - 0.5) * 50
})
// define forces that control the bubbles
const forceX = d3.forceX((d) => categoryCenters[d.dimension].x).strength(0.2)
const forceY = d3.forceY((d) => categoryCenters[d.dimension].y).strength(0.1)
const forceCollide = d3.forceCollide(radius + 2)
const forceManyBody = d3.forceManyBody().strength(-15) // Adjust the strength as needed
// add bubbles to the page
const bubbles = svg
.selectAll('.bubble')
.data(data.value)
.enter()
.append('circle')
.attr('class', 'bubble')
.attr('r', radius)
.style('fill', (d) => dimensionColors[d.dimension.replace(' ', '')])
const simulation = d3
.forceSimulation()
.force('x', forceX)
.force('y', forceY)
.force('collide', forceCollide)
.force('charge', forceManyBody)
.nodes(data.value)
.on('tick', ticked)
.alpha(0.2)
.restart()
bubbles
.on('mouseover', handleMouseOver)
.on('mousemove', handleMouseMove)
.on('mouseout', handleMouseOut)
function ticked() {
bubbles.attr('cx', (d) => d.x).attr('cy', (d) => d.y)
}
}
function sizeByEvidence(checked) {
if (checked){
const { radiusScale } = createScales(data)
const forceCollide = d3.forceCollide((d) => radiusScale(d.evidence_val) + 2)
const forceManyBody = d3.forceManyBody().strength(15) // Adjust the strength as needed
const bubbles = svg.selectAll('.bubble')
.attr('r', d => radiusScale(d.evidence_val)) // size bubbles based on evidence_val
const simulation = d3
.forceSimulation()
// .force('x', forceX)
// .force('y', forceY)
.force('collide', forceCollide)
.force('charge', forceManyBody)
.nodes(data.value)
.on('tick', ticked)
.alpha(0.2)
.restart()
function ticked() {
bubbles.attr('cx', (d) => d.x).attr('cy', (d) => d.y)
}
} else {
const radius = 10
const forceX = d3.forceX((d) => categoryCenters[d.dimension].x).strength(0.2)
const forceY = d3.forceY((d) => categoryCenters[d.dimension].y).strength(0.1)
const forceCollide = d3.forceCollide(radius + 2)
const forceManyBody = d3.forceManyBody().strength(-15) // Adjust the strength as needed
const bubbles = svg.selectAll('.bubble')
.attr('r', radius)
const simulation = d3
.forceSimulation()
.force('x', forceX)
.force('y', forceY)
.force('collide', forceCollide)
.force('charge', forceManyBody)
.nodes(data.value)
.on('tick', ticked)
.alpha(0.2)
.restart()
function ticked() {
bubbles.attr('cx', (d) => d.x).attr('cy', (d) => d.y)
}
}
}
function positionByAgreement(checked) {
if (checked) {
const { xScale, radiusScale} = createScales(data);
const xAxis = svg.append('g')
.attr("transform", "translate(0," + (height - 50) + ")")
.call(d3.axisBottom(xScale))
// add label to x axis
svg.append('text')
.attr("class", "xLabel")
.attr("text-anchor", "middle")
.attr("x", width/2)
.attr("y", height-10)
.text("Level of Agreement");
const forceX = d3.forceX(d => xScale(d.level_agreement)).strength(1);
const forceY = d3.forceY((height/1.5) - (margin.bottom/2))//.strength(0.1);
const forceCollide = d3.forceCollide(d => radiusScale(d.evidence_val) + 2); //forceCollide for bubbles sized by evidence_val
const forceManyBody = d3.forceManyBody().strength(-15); // Adjust the strength as needed
const bubbles = svg.selectAll(".bubble")
simulation = d3.forceSimulation()
.force("x", forceX)
.force("y", forceY)
.force("collide", forceCollide)
.force("charge", forceManyBody)
.nodes(data.value)
.on("tick", ticked)
.alpha(.2)
.restart();
function ticked() {
bubbles
.attr("cx", d => d.x)
.attr("cy", d => d.y);
}
} else {
}
}
function sortByDirection() {
const { xScale, radiusScale} = createScales(data);
var yScale = d3.scaleBand()
.domain(data.value.map(function(d) { return d.sig_name; }))
.range([margin.top, height])
const bubbles = svg.selectAll(".bubble")
// const forceX = d3.forceX(d => xScale(d.level_agreement)).strength(.4);
const forceY = d3.forceY(d => yScale(d.sig_name)).strength(.4)
const forceCollide = d3.forceCollide(d => radiusScale(d.evidence_val) + 1); //forceCollide for bubbles sized by evidence_val
const forceManyBody = d3.forceManyBody().strength(-15); // Adjust the strength as needed
simulation = d3.forceSimulation()
// .force("x", forceX)
.force("y", forceY)
.force("collide", forceCollide)
.force("charge", forceManyBody)
.nodes(data.value)
.on("tick", ticked)
.alpha(.2)
.restart();
function ticked() {
bubbles
.attr("cx", d => d.x)
.attr("cy", d => d.y);
}
}
</script>
<style scoped lang="scss">
$switchWidth: 12rem;
#toggle-container {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
margin: 20px 0;
}
#checkbox1-container,
#checkbox2-container,
#checkbox3-container {
display: flex;
align-items: center;
margin: 0 10px;
}
#checkbox1-container label,
#checkbox2-container label,
#checkbox3-container label {
margin-left: 5px;
font-size: 2rem;
}
#checkbox1,
#checkbox2,
#checkbox3 {
transform: scale(1.5); /* Adjust the scale to make checkboxes bigger */
margin-right: 10px; /* Add some space between checkbox and label */
}
.graph-buttons-switch {
display: flex;
height: 2.8rem;
width: $switchWidth * 2.03;
border-radius: 0.2rem;
position: relative;
margin: 0rem 0.5rem 0rem 0.5rem;
background: rgba(0, 0, 0, 0.3);
-webkit-box-shadow: inset 0 0.1rem 0.3rem rgba(0, 0, 0, 0.1), 0 0.1remx rgba(255, 255, 255, 0.1);
box-shadow: inset 0 0.1rem 0.3rem rgba(0, 0, 0, 0.1), 0 0.1rem rgba(255, 255, 255, 0.1);
-webkit-touch-callout: none; /* iOS Safari */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */
user-select: none; /* Non-prefixed version, currently supported by Chrome and Opera */
@media screen and (max-width: 600px) {
height: 2.6rem;
}
}
.graph-buttons-switch-label {
position: relative;
z-index: 2;
float: left;
width: $switchWidth;
line-height: 2.4rem;
text-align: center;
cursor: pointer;
@media screen and (max-width: 600px) {
line-height: 2.2rem;
width: $switchWidth * 1.02;
}
}
.graph-buttons-switch-label-off {
padding-left: 0.2rem;
padding-right: 0.2rem;
}
.graph-buttons-switch-label-on {
padding-left: 0.2rem;
padding-right: 0.2rem;
}
.graph-buttons-switch-input {
display: none;
}
.graph-buttons-switch-input:checked + .graph-buttons-switch-label {
font-weight: bold;
-webkit-transition: 0.3s ease-out;
-moz-transition: 0.3s ease-out;
-o-transition: 0.3s ease-out;
transition: 0.3s ease-out;
}
.graph-buttons-switch-input:checked + .graph-buttons-switch-label-on ~ .graph-buttons-switch-selection {
left: $switchWidth;
}
.graph-buttons-switch-selection {
display: block;
position: absolute;
z-index: 1;
top: 0.2rem;
left: 0.2rem;
width: $switchWidth;
height: 2.4rem;
background: rgba(255, 255, 255, 1);
border-radius: 0.2rem;
-webkit-box-shadow: inset 0 0.1rem rgba(255, 255, 255, 0.6), 0 0 0.2rem rgba(0, 0, 0, 0.3);
box-shadow: inset 0 0.1rem rgba(255, 255, 255, 0.6), 0 0 0.2rem rgba(0, 0, 0, 0.3);
-webkit-transition: left 0.3s ease-out, background 0.3s;
-moz-transition: left 0.3s ease-out, background 0.3s;
-o-transition: left 0.3s ease-out, background 0.3s;
transition: left 0.3s ease-out, background 0.3s;
@media screen and (max-width: 600px) {
height: 2.2rem;
}
}
#beeswarm-chart-container {
text-align: center;
position: relative;
}
#beeswarm-chart-container svg {
max-width: 100%;
max-height: 100%;
height: auto; /* Maintain aspect ratio */
display: inline-block;
}
.bubble {
stroke: black;
stroke-width: 2px;
fill-opacity: 0.8;
}
.chart-text {
user-select: none;
}
.tooltip {
font-size: 16px;
background-color: white;
border: solid;
border-width: 2px;
border-radius: 5px;
padding: 5px;
position: absolute;
}
text.xLabel {
font-weight: 600;
}
</style>