-
Notifications
You must be signed in to change notification settings - Fork 1
/
visualization.html
225 lines (199 loc) · 7.52 KB
/
visualization.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Feed Visualizer</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src='https://cdn.plot.ly/plotly-2.11.1.min.js'></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.clustnumspan {
padding-right: 2px;
padding-left: 2px;
border-right: 1px solid #fdfdfd57;
display: inline-block;
border-radius: 30px;
background-color: white;
color: #212529;
font-size: x-small;
text-align: center;
margin: 4px;
min-width: 18px;
}
#clusters {
cursor: pointer
}
body {
background-color: #dee2e6;
}
</style>
</head>
<body>
<div class="container">
<div class="jumbotron">
<p class="lead" style="padding-top:20px">
Add information about the visualization here !
</p>
</div>
<div id="chk" class="lead">Enable Hulls: <input type="checkbox" onchange="update_hulls(this.checked)"
value='true' /></div>
<div id="clusters"> </div>
<div id='myDiv' style="width:100%;height:700px"> </div>
</div>
<script>
let hulls_enabled = false;
let color = d3.scaleOrdinal(d3.schemeSet2);
let clusters = {}
let cluster_count = 1
let csv_data = null;
let selected_cluster = -1;
let trace1 = {
x: [],
y: [],
marker: {
size: [],
color: [],
line: {
color: '#eee2e6',
width: 0.5
}
},
text: [],
mode: 'markers',
textposition: 'top',
type: 'scatter',
name: "\nURL"
};
let data = [trace1]
let layout = {
showlegend: false,
xaxis: {
showgrid: false,
zeroline: false,
},
yaxis: {
showgrid: false,
zeroline: false
},
font: {
family: 'Courier New, monospace',
size: 9,
color: '#7f7f7f'
},
paper_bgcolor: 'rgba(0,0,0,0)',
plot_bgcolor: 'rgba(0,0,0,0)'
}
function draw_convex_hull(clusters) {
if (hulls_enabled == false) {
let tracesCount = document.getElementById('myDiv').data.length
for (let q = 1; q < tracesCount; q++) {
Plotly.deleteTraces('myDiv', 1);
}
return;
};
d3.json("convex_hulls.json" + '?' + Math.floor(Math.random() * 1000)).then((d) => {
d.forEach((b, idx) => {
if (selected_cluster > -1 && idx != selected_cluster) {
return;
}
let color_line = color(idx / cluster_count)
let color_hull = d3.color(color_line)
color_hull.opacity = 0.2
color_hull = d3.color(color_hull)
var convexHullTrace = {
type: 'polygon',
x: b.x,
y: b.y,
mode: 'lines',
line: {
color: "#999",
width: 1,
shape: 'spline',
dash: 'dot'
},
fillcolor: color_hull.toString(),
showlegend: false,
hoverinfo: 'skip'
};
Plotly.addTraces('myDiv', convexHullTrace);
})
})
}
function drawPlot(placeholder, data, layout) {
Plotly.newPlot(placeholder, data, layout).then(function () {
document.getElementById(placeholder).on('plotly_click', function (data) {
window.open(csv_data[data.points[0].pointIndex].url, "__blank")
});
})
}
function makeplot() {
trace1.x = [];
trace1.y = []
document.getElementById("clusters").innerHTML = ""
document.getElementById("myDiv").innerHTML = ""
d3.csv("data.csv" + '?' + Math.floor(Math.random() * 1000)).then((d) => {
csv_data = d
let clusterNumbers = []
let topics = {}
d.forEach(a => { topics[a.cluster] = a.topic; clusterNumbers.push(parseInt(a.cluster)) })
cluster_count = Math.max(...clusterNumbers) + 1
d3.select('#clusters')
.selectAll('span')
.data(d3.range(0, cluster_count))
.enter()
.append('span')
.style("background-color", function (d) { return color(d / cluster_count) })
.style("padding-left", "4px")
.style("padding-right", "4px")
.style("font-size", "small")
.style("min-width", "25px")
.style("display", "inline-block")
.style("color", "white")
.style("margin", "1px")
.style("border-radius", "4px")
.attr("data-clusterId", function (d) { return d })
.on("mouseover", function (e, d) {
let newTrace = JSON.parse(JSON.stringify(trace1));
let new_colors = newTrace.marker.color.map(function (c, idx) {
return (csv_data[idx] && csv_data[idx].cluster == d) ? color(d / cluster_count) : "#dee2e6"
})
newTrace.marker.color = new_colors
drawPlot('myDiv', [newTrace], layout)
})
.on("mouseout", function () {
drawPlot('myDiv', [trace1], layout)
draw_convex_hull(clusters)
})
.html(function (d) {
return "<span class='clustnumspan'>" + d.toString() + " </span> " + topics[d];
});
d.forEach(element => {
processData(element)
});
drawPlot('myDiv', data, layout)
}).then(a => {
draw_convex_hull(clusters)
})
};
function processData(row) {
trace1.x.push(row.x)
trace1.y.push(row.y)
trace1.text.push(row.label.replace("Bliki:", ""))
trace1.marker.size.push((parseInt(row.count) + 8) * 1.2)
trace1.marker.color.push(color(parseInt(row.cluster) / cluster_count))
let cluster_key = row.cluster.toString()
if (!clusters[cluster_key]) {
clusters[cluster_key] = []
}
clusters[cluster_key].push([row.x, row.y])
}
function update_hulls(b) {
hulls_enabled = b;
draw_convex_hull();
//makeplot();
}
makeplot()
</script>
</body>
</html>