-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
338 lines (257 loc) · 10.5 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
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
// Transform JSON schema to hierarchical format
function transformSchemaToHierarchy(schema) {
let root = {
name: schema["$id"] || schema.title || "Root",
children: []
};
function processProperties(properties, parentNode) {
for (let key in properties) {
let childNode = {
name: key,
children: []
};
if (properties[key].type === "object" && properties[key].properties) {
processProperties(properties[key].properties, childNode);
} else if (properties[key].type === "array" && properties[key].items && properties[key].items.properties) {
processProperties(properties[key].items.properties, childNode);
}
if (childNode.children.length === 0) delete childNode.children;
parentNode.children.push(childNode);
}
}
processProperties(schema.properties, root);
return root;
}
// function to clear the visualization
function clearVisualization() {
d3.select('#chart svg').remove(); // Remove the existing SVG to reset the visualization
}
//this is the main function that will be called from the index.html
document.getElementById('jsonFileInput').addEventListener('change', function() {
if (this.files.length > 0) {
const file = this.files[0];
const reader = new FileReader();
reader.onload = (e) => {
clearVisualization(); // Clear the visualization here
const jsonSchema = JSON.parse(e.target.result);
const hierarchicalData = transformSchemaToHierarchy(jsonSchema);
console.log("Transformed Hierarchical Data:", hierarchicalData);
parentFunction(hierarchicalData); // Proceed to visualize the new data
// Reset the file input to allow re-uploading the same file or a new one
document.getElementById('jsonFileInput').value = '';
};
reader.readAsText(file);
} else {
console.log("No file selected!");
}
});
// function to create the tree diagram from the transformed data
function parentFunction(jsondata){
console.log("the data is")
console.log(jsondata)
let mouseX = 0;
//these global variables I should later get via closure
let buttonTracker = [];
let rootNode = d3.hierarchy(jsondata, d=>d.children);
var pathLinks = rootNode.links();
var updatePathLinks;
var circleLinks = rootNode.descendants();
var updateCircleLinks;
var textLinks = rootNode.descendants();
var updateTextLinks;
let dim = {
'width': window.screen.width,
'height':window.screen.height * 7 ,
'margin':50
};
let svg = d3.select('#chart').append('svg')
.style('background', 'black')
.attrs(dim);
document.querySelector("#chart").classList.add("center");
//let rootNode = d3.hierarchy(data);
let g = svg.append('g')
.attr('transform', 'translate(140,50)');
let layout = d3.tree().size([dim.height-50, dim.width-320]);
layout(rootNode);
console.log(rootNode.links());
console.log("----------------------");
console.log(rootNode.descendants());
//console.log(rootNode.descendants());
//first lets create a path
let lines = g.selectAll('path');
function update(data){
let group = g.selectAll('path')
.data(data, (d,i) => d.target.data.name)
.join(
function(enter){
return enter.append('path')
.attrs({
'd': d3.linkHorizontal()
.x(d => mouseX)
.y(d => d.x),
'fill':'none',
'stroke':'white'
})
},
function(update){
return update;
},
function(exit){
return exit.call(path => path.transition().duration(300).remove()
.attr('d', d3.linkHorizontal()
.x(d => mouseX)
.y(d =>d.x)));
}
)
.call(path => path.transition().duration(1000).attr('d', d3.linkHorizontal()
.x(d => d.y)
.y(d => d.x))
.attr("id", function(d,i){return "path"+i}));
}
update(pathLinks); //rootNode.links()
function updateCircles(data){
g.selectAll('circle')
.data(data, (d) => d.data.name)
.join(
function(enter){
return enter.append('circle')
.attrs({
'cx':(d)=> mouseX,
'cy':(d) => d.x,
'r':12,
'fill':(d) => {
if(d.children == undefined){
return 'red'
}
return 'green'
},
'id': (d,i) =>d.data.name,
'class':'sel'
})
},
function(update){
return update;
},
function(exit){
return exit.call(path => path.transition().duration(300).remove()
.attrs({
'cx':(d) =>mouseX,
'r':(d) => 0
}));
}
)
.call(circle => circle.transition().duration(1000).attr('cx', (d)=>d.y))
.on('mouseover', function(d){
d3.select(this)
.attrs({
'fill':'orange',
})
.transition().duration(100).attr('r', 16);
})
.on('mouseout', function(d){
d3.select(this)
.attr('fill', (d)=>{
if(d.children ==undefined){
return 'red'
}
return 'green'
})
.transition().duration(100).attr('r', 12)
})
.on('click', async function(d){
let buttonId = d3.select(this)["_groups"][0][0]["attributes"].id.value;
mouseX = d3.select(this)["_groups"][0][0]["attributes"].cx.value;
//check to see if button already exists aka has been clicked
//if it does, we need to send that data to update function
//and remove that object
let checkButtonExists = buttonTracker.filter(button => button.buttonId == buttonId);
if(checkButtonExists[0]!=undefined){
//also remove this item from button tracker
buttonTracker = buttonTracker.filter(button => button.buttonId != buttonId);
//handle path update
pathLinks = checkButtonExists[0].buttonPathData.concat(pathLinks);
update(pathLinks);
//handle circle update
circleLinks = checkButtonExists[0].buttonCircleData.concat(circleLinks);
updateCircles(circleLinks);
//handle text update
textLinks =checkButtonExists[0].buttonTextData.concat(textLinks);
updateText(textLinks);
return;
}
var valueArray = await processedlinks(d.links());
updatePathLinks = pathLinks.filter(function(item){
return !valueArray.includes(item.target.data.name);
});
//now run the filter to get unfiltered items
var clickedPathData = pathLinks.filter(function(item){
return valueArray.includes(item.target.data.name);
});
updateCircleLinks = circleLinks.filter(function(item){
return !valueArray.includes(item.data.name);
});
var clickedCircleData = circleLinks.filter(function(item){
return valueArray.includes(item.data.name);
});
updateTextLinks = textLinks.filter(function(item){
return !valueArray.includes(item.data.name);
});
var clickedTextData = textLinks.filter(function(item){
return valueArray.includes(item.data.name);
});
//now we push the circleData to an array
buttonTracker.push({
buttonId:buttonId,
buttonPathData: clickedPathData,
buttonCircleData:clickedCircleData,
buttonTextData:clickedTextData
})
update(updatePathLinks);
updateCircles(updateCircleLinks);
updateText(updateTextLinks);
async function processedlinks(dlinks) {
var valueArray = [];
return new Promise((resolve, reject)=>{
dlinks.forEach(async(element) =>{
valueArray.push(element.target.data.name);
});
resolve(valueArray);
});
}
pathLinks = updatePathLinks;
circleLinks = updateCircleLinks;
textLinks = updateTextLinks;
});
}
updateCircles(rootNode.descendants());
function updateText(data){
g.selectAll('text')
.data(data, (d) =>d.data.name)
.join(
function(enter){
return enter.append('text')
.attrs({
'x': (d) =>mouseX,
'y':(d) => d.x,
'font-size':0
})
.text((d) => d.data.name);
},
function(update){
return update;
},
function(exit){
return exit.call(text => text.transition().duration(300).remove().attrs({
'x':(d) => mouseX,
'font-size':0
}));
}
)
.call(text => text.transition().duration(1000).attrs({
'x':(d) =>d.y+20,
'font-size':15,
'fill':'yellow',
}));
}
updateText(textLinks);
}