-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
201 lines (171 loc) · 6.09 KB
/
index.html
File metadata and controls
201 lines (171 loc) · 6.09 KB
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
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="600"></svg>
<div id="chart"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var PERSPECT_LINK = 20;
// original data, this will be factored out
var nodes = [
{ id: "Diversity", module: 0, level: 1, radius: 36},
{ id: "diversity perspect 1" , module: 0, level: 2, radius: 14 },
{ id: "diversity perspect 2" , module: 0, level: 2, radius: 14 },
// { id: "diversity source" , module: 0, level: 3 },
{ id: "Workplace Ethics" , module: 1, level: 1, radius: 24 },
{ id: "workplace perspect 1" , module: 1, level: 2, radius: 14 },
// { id: "workplace source 1" , module: 0, level: 3 },
// { id: "workplace source 2" , module: 0, level: 3 },
{ id: "Tech", module: 2, level: 1, radius: 36 },
{ id: "tech perspect 1" , module: 2, level: 2, radius: 14 },
{ id: "tech perspect 2" , module: 2, level: 2, radius: 14 },
{ id: "Healthcare" , module: 3, level: 1, radius: 48 },
{ id: "healthcare perspect 1" , module: 3, level: 2, radius: 14 },
{ id: "healthcare perspect 2" , module: 3, level: 2, radius: 14 },
{ id: "healthcare perspect 3" , module: 3, level: 2, radius: 14 }
]
// lines connecting each topic with their perspective,
// will be factored out
var lines = [
{ target: "Diversity", source: "diversity perspect 1" },
{ target: "Diversity", source: "diversity perspect 2" },
{ target: "Workplace Ethics", source: "workplace perspect 1" },
{ target: "Tech", source: "tech perspect 1" },
{ target: "Tech", source: "tech perspect 2" },
{ target: "Healthcare", source: "healthcare perspect 1" },
{ target: "Healthcare" , source: "healthcare perspect 2" },
{ target: "Healthcare" , source: "healthcare perspect 3" }
]
// creating the container for the bubble graph
var width = 960
var height = 600
var container = d3.select('svg')
.append("g")
.attr("class", "nodes")
// creates the line elements to link the bubbles
var lineElements = container.append('g')
.selectAll('line')
.data(lines)
.enter().append('line')
.attr('stroke-width', 1)
.attr('stroke', '#E5E5E5')
// creates the bubbles and sets their styles and functionality
var nodeElements = container.selectAll("circle")
.data(nodes)
.enter().append("circle")
.attr('r', getRadius)
.attr('fill', getNodeColor)
.on('mouseover', mouseOver)
.on('mouseout', mouseOut)
// creates the tooltip when a bubble is hovered
var tooltip = d3.select("#chart").append("div")
.style("position", "absolute")
.style("visibility", "hidden")
.style("display", "block")
.style("color", "white")
.style("padding", "8px")
.style("background-color", "#626D71")
.style("border-radius", "6px")
.style("text-align", "center")
.style("font-family", "monospace")
.style("width", "400px")
.html("")
nodes.forEach(function(node) {
node.x = width
node.y = height
})
function mouseOver(node) {
node.isHovering = true
tooltip.text(node.id)
d3.select(this).attr("r", node.radius + 10)
hoverColors(node);
simulation.alphaTarget(.3)
.force('link', linkForce)
.force('collide', forceCollide)
.restart()
tooltip.style("visibility", "visible")
.style("top", (d3.event.pageY - 10) + "px")
.style("left", (d3.event.pageX + 10) + "px")
return tooltip.style("opacity", "1")
}
function mouseOut(node) {
node.isHovering = false
d3.select(this).attr("r", node.radius);
resetColors();
simulation.alphaTarget(.3)
.force('link', linkForce)
.force('collide', forceCollide)
.restart()
tooltip.on('mouseover', function(d) {
tooltip.transition().style("opacity", "1");
}).on('mouseout', function(d) {
tooltip.transition().duration(200).style("opacity", "0");
});
return tooltip.transition().duration(200).style("opacity", "0")
}
// creating a force between the links
var linkForce = d3.forceLink()
.id(function (link) { return link.id })
.distance(getDistance)
.iterations(1)
function getDistance(link) {
return link.source.radius + link.target.radius + PERSPECT_LINK
}
// returns the color of the node based on its level
function getNodeColor(node) {
return node.level === 1 ? '#29f2ba' : '#5da5ef'
}
// returns the radius of the node based on the number
// of children it has
function getRadius(node) {
if (node.level === 1) {
return node.radius
} else {
return 14
}
}
function getModule(node) {
return node.module;
}
function isSameModule(hoveredCircle, otherCircle) {
return hoveredCircle.module == otherCircle.module;
};
function isSameLine(hoveredCircle, line) {
return hoveredCircle.module == line.source.module;
};
function hoverColors(d) {
d3.selectAll("circle").style("fill-opacity", function(o) {
return isSameModule(d, o) ? 1 : 0.2;
});
d3.selectAll("line").style("stroke-opacity", function(o) {
return isSameLine(d, o) ? 1 : 0.2;
});
};
function resetColors(d) {
d3.selectAll("circle").style("fill-opacity", 1);
d3.selectAll("line").style("stroke-opacity", 1);
};
var forceX = d3.forceX(250).strength(0.04)
var forceY = d3.forceY(250).strength(0.04)
var forceCollide = d3.forceCollide().radius(function(d) { return d.radius + 10 }).iterations(2)
// creates the forces between elements
var simulation = d3.forceSimulation(nodes)
.alpha(1.5)
.force('link', linkForce)
.force('charge', d3.forceManyBody().strength(-50))
.force('center', d3.forceCenter(width / 2, height / 2))
.force('forceX', forceX)
.force('forceY', forceY)
.force('collide', forceCollide)
simulation.nodes(nodes).on('tick', ticked)
function ticked() {
d3.selectAll("circle")
.attr('cx', function (node) { return node.x })
.attr('cy', function (node) { return node.y })
d3.selectAll("line")
.attr('x1', function (link) { return link.source.x })
.attr('y1', function (link) { return link.source.y })
.attr('x2', function (link) { return link.target.x })
.attr('y2', function (link) { return link.target.y })
}
simulation.force('link').links(lines)
</script>