-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.js
More file actions
315 lines (278 loc) · 13.6 KB
/
plot.js
File metadata and controls
315 lines (278 loc) · 13.6 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
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
// This is Semascope, text visualization tool that uses Plotly JS to graphically
// represent relationship between words in a collection of texts.
// You can see a demonstration at https://textvisualization.app
// Plotly JS is available here https://cdn.plot.ly/plotly-latest.min.js
// You will need to load plotly JS for this script to work
// This JS uses text mining dataset in a tab separated format, prepared by corpus_utils
// Corpus_utils is a method of extracting keywords from a vast collection of texts
// Corpus_utils are available here: https://github.com/roverbird/corpus_utils
// Declare maxClusterValue at a global scope
let maxClusterValue;
let parsedData;
// Function to update the plot based on user-defined filters
function updatePlot() {
// Get the data source URL from the div element with id="dataUrl"
// This is the point where dataset is intup into this plotly JS app
let dataUrl = document.getElementById('dataUrl').textContent;
// let dataUrl = document.getElementById('dataUrl').getAttribute('href');
// Fetch data from the URL
fetch(dataUrl)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
// Parse the tab-separated dataset from dataUrl above
parsedData = data.split('\n').map(line => line.split('\t'));
// Extract columns and apply filters
const x_values = parsedData.map(row => parseFloat(row[1]));
const y_values = parsedData.map(row => parseFloat(row[2]));
//const sq_values = parsedData.map(row => parseInt(row[3]));
const frequency_values = parsedData.map(row => parseInt(row[4]));
const z_values = parsedData.map(row => parseInt(row[5]));
const cluster_values = parsedData.map(row => parseInt(row[7]));
// Get filter values from the form
const kFilter = parseFloat(document.getElementById('k').value);
const pFilter = parseFloat(document.getElementById('p').value);
const dfFilter = parseInt(document.getElementById('DF').value);
const frFilter = parseInt(document.getElementById('fr').value);
const clusterFilter = parseInt(document.getElementById('cluster').value);
// Apply filters
const filters = parsedData.filter(row => row[1] <= kFilter && row[2] <= pFilter && row[4] >= frFilter && row[5] <= dfFilter && row[7] >= clusterFilter);
// Extract filtered columns and labels
const filtered_x_values = filters.map(row => parseFloat(row[1]));
const filtered_y_values = filters.map(row => parseFloat(row[2]));
//const filtered_sq_values = filters.map(row => parseInt(row[3]));
const filtered_frequency_values = filters.map(row => parseInt(row[4]));
const filtered_z_values = filters.map(row => parseInt(row[5]));
const filtered_cluster_values = filters.map(row => parseInt(row[7]));
//const filtered_sq_values = filters.map(row => parseInt(row[3]));
// Calculate maxClusterValue
maxClusterValue = Math.max(...filtered_cluster_values);
maxfrValue = Math.max(...filtered_frequency_values);
maxDFValue = Math.max(...filtered_z_values);
maxpValue = Math.max(...filtered_y_values);
maxkValue = Math.max(...filtered_x_values);
reduceDFvalue = maxDFValue * 0.9; // 10 percent less than maxDFValue
//increaseFrvalue = frFilter * 1.5; // 10 percent less than maxDFValue
reducepvalue = maxpValue * 0.9;
reducekvalue = maxkValue * 0.9;
// Get text labels
const labels = filters.map(row => row[0]); // Assuming column A contains labels
function normalizeArray(arr, targetMin, targetMax) {
const minValue = Math.min(...arr);
const maxValue = Math.max(...arr);
return arr.map(value => targetMin + (targetMax - targetMin) * (value - minValue) / (maxValue - minValue));
}
const normalized_filtered_frequency_values = normalizeArray(filtered_frequency_values, 13, 70);
const scatterPlotData = [{
x: filtered_x_values,
y: filtered_y_values,
z: filtered_z_values,
mode: 'markers',
marker: {
size: normalized_filtered_frequency_values, // Use the frequency values for size
sizemode: 'diameter', // Set to 'diameter' to specify marker size in pixels
sizeref: 1.3, // Adjust as needed to control the size of markers
color: filtered_cluster_values, // Use the cluster values for color
colorscale: 'Jet', // Choose a colorscale
cmin: 1, // Set the minimum value for the colorscale
cmax: 10, // Set the maximum value for the colorscale
colorbar: {
title: 'Cluster', // Label for the colorbar
titlefont: { size: 15 },
titleside: 'top',
//x: .09,
len: 0.3,
tickvals: Array.from(new Set(filtered_cluster_values)), // Unique cluster values for tick marks
ticktext: Array.from(new Set(filtered_cluster_values)), // Labels for the tick marks
},
},
type: 'scatter3d',
text: labels, // Set text labels for each point
hoverinfo: 'text', // Show text information always
hoverlabel: {
font: {
size: 30,
},
}
//hoverinfo labels, // Include x, y, z, and color information in hover template
// hovertemplate:
// '<b>%{text}</b>' +
// '<br>X: %{x:.2f}' +
// '<br>Y: %{y:.2f}' +
// '<br>Z: %{z:.2f}', // Add additional information as needed
//textposition: 'bottom center',
}];
// Layout settings with increased width and height
const layout = {
width: 1010, // Adjust the width in pixels
height: 1300, // Adjust the height in pixels
margin: {
l: 0,
r: 0,
b: 0,
t: 0,
pad: 0
},
paper_bgcolor: '#FAFAFA',
plot_bgcolor: '#FAFAFA',
scene: {
camera: {
eye: {
x: 2,
y: 0,
z: 1
}
},
xaxis: {
title: 'k', // Label for the x-axis
titlefont: { size: 20 }
},
yaxis: {
title: 'p', // Label for the y-axis
titlefont: { size: 20 }
},
zaxis: {
title: 'Doc Frequency (DF)', // Label for the y-axis
titlefont: { size: 20 }
}
}
};
// Create 3D scatter plot
Plotly.newPlot('plot', scatterPlotData, layout);
})
.catch(error => console.error('Error fetching data:', error));
}
function removeFilters() {
// Reset filter values to their defaults
document.getElementById('k').value = '10';
document.getElementById('p').value = '1';
document.getElementById('DF').value = '1000';
document.getElementById('fr').value = '1';
document.getElementById('cluster').value = '0';
updatePlot();
}
function heros() {
document.getElementById('k').value = '0.25';
document.getElementById('p').value = '0.25';
// document.getElementById('DF').value = '10000';
document.getElementById('cluster').value = '1';
document.getElementById('fr').value = '1';
// document.getElementById('p').value = reducepvalue;
// document.getElementById('k').value = reducekvalue;
updatePlot();
}
function clue() {
document.getElementById('k').value = '1';
document.getElementById('p').value = '0.5';
// document.getElementById('DF').value = '10000';
document.getElementById('cluster').value = '5';
document.getElementById('fr').value = '1';
// document.getElementById('p').value = reducepvalue;
// document.getElementById('k').value = reducekvalue;
updatePlot();
}
// Function to handle button click Show Background
function setMaxClusterValue() {
document.getElementById('k').value = '100';
document.getElementById('p').value = '1';
document.getElementById('fr').value = '1';
//document.getElementById('DF').value = '10000';
document.getElementById('cluster').value = maxClusterValue;
updatePlot();
}
// Function to handle button click on reduce DF
function setreduceDFvalue() {
document.getElementById('DF').value = reduceDFvalue;
updatePlot();
}
// Function to handle button click on increase Fr
// function setincreaseFrvalue() {
// document.getElementById('fr').value = increaseFrvalue;
// updatePlot();
// }
// Initially load the plot
updatePlot();
// Function to update the plot based on the search word
function searchAndUpdatePlot() {
// Get the search word from the input field
const searchWord = document.getElementById('searchWord').value.trim().toLowerCase();
// Apply the search filter
const searchFilters = parsedData.filter(row => row[0].toLowerCase().includes(searchWord));
// Extract filtered columns and labels
const filtered_x_values = searchFilters.map(row => parseFloat(row[1]));
const filtered_y_values = searchFilters.map(row => parseFloat(row[2]));
const filtered_z_values = searchFilters.map(row => parseInt(row[5]));
const filtered_cluster_values = searchFilters.map(row => parseInt(row[7]));
const filtered_frequency_values = searchFilters.map(row => parseInt(row[4]));
const labels = searchFilters.map(row => row[0]);
// Update the scatter plot data
const scatterPlotData = [{
x: filtered_x_values,
y: filtered_y_values,
z: filtered_z_values,
mode: 'markers',
marker: {
size: 10,
sizemode: 'diameter',
symbol: "circle",
sizeref: 1,
color: filtered_cluster_values,
colorscale: 'Jet',
cmin: 1,
cmax: 10,
colorbar: {
title: 'Cluster',
titleside: 'top',
x: .09, len: 0.3,
tickvals: Array.from(new Set(filtered_cluster_values)),
ticktext: Array.from(new Set(filtered_cluster_values)),
},
},
type: 'scatter3d',
text: labels,
hoverlabel: {
font: {
size: 30,
},
},
textposition: 'bottom center',
}];
// Layout settings for search results
const layout = {
width: 1010, // Adjust the width in pixels
height: 1300, // Adjust the height in pixels
margin: {
l: 0,
r: 0,
b: 0,
t: 0,
pad: 0
},
paper_bgcolor: '#FAFAFA',
plot_bgcolor: '#FAFAFA',
scene: {
camera: {
eye: {
x: 2,
y: 0.88,
z: 0.9
}
},
xaxis: {
title: 'k' // Label for the x-axis
},
yaxis: {
title: 'p' // Label for the y-axis
},
zaxis: {
title: 'Doc Frequency (DF)' // Label for the y-axis
},
}
};
// Update the 3D scatter plot
Plotly.newPlot('plot', scatterPlotData, layout);
}