-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion1.js
259 lines (223 loc) · 8.28 KB
/
version1.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
var result = []; //is the data structure final
//Start to divide the parse tree and put into a data structure
var res = string.slice(string.indexOf('('), string.lastIndexOf("'"));
var pilaParent = [];
var firstIndex = res.indexOf(':');
var j = 0;
for (i = 0; i <= res.length; i++) {
if (res[i] == '(') {
var nome = computeName(i);
i = i + nome.length;
var valore = computeValue(i + 1);
i = i + valore.length;
if (j == 0) {
result[j] = {
name: nome,
value: valore,
parent: {},
id: j,
disegnato: false
}
pilaParent.push(result[j]);
} else {
result[j] = {
name: nome,
value: valore,
parent: {
padre: pilaParent[pilaParent.length - 1]
},
id: j,
disegnato: false
}
pilaParent.push(result[j]);
}
j++;
}
if (res[i] == ' ' && res[i + 1] != '(') {
var nome = computeName(i);
i = i + nome.length;
var valore = computeValue(i + 1);
i = i + valore.length;
result[j] = {
name: nome,
value: valore,
parent: {
padre: pilaParent[pilaParent.length - 1]
},
id: j,
disegnato: false
}
j++;
}
if (res[i] == ')') {
pilaParent.pop();
}
}
for (i = 0; i < result.length; i++) {
var nodo = result[i].id;
var figli = [];
for (j = i; j < result.length; j++) {
if (j == 0) {
j++;
}
if (nodo == result[j].parent.padre.id) {
figli.push(result[j]);
}
}
result[i].children = figli;
}
function computeName(index) { //function that computes the name of each node
var primaDuePunti = res.indexOf(":", index)
if (res[primaDuePunti + 1] == res[primaDuePunti]) {
return res.slice(index + 1, primaDuePunti + 1);
} else {
return res.slice(index + 1, primaDuePunti);
}
}
function computeValue(index) { //function that computes the value of each node
var inizioNumeri = index + 1;
var number = '';
while ((res[inizioNumeri] != ' ') && (res[inizioNumeri] != ')') && (inizioNumeri < res.length)) {
number = number + res[inizioNumeri];
inizioNumeri++;
}
if (isNaN(number)) {
var ultimiDuePunti = number.lastIndexOf(":") + 1;
number = number.slice(ultimiDuePunti);
}
return number;
}
var livello = 0;
function calcolaAltezza(dataStructure, level) { //function that computes the height of the tree
var numChild = dataStructure.children.length;
dataStructure.livello = level;
if (numChild == 0) {
return;
}
for (let i = 0; i < dataStructure.children.length; i++) {
calcolaAltezza(dataStructure.children[i], level + 1)
}
level = level + 1;
if (level > livello) {
livello = level;
}
}
calcolaAltezza(result[0], 1)
var arraySpazioLivelli = [];
for (let i = 0; i < livello; i++) {
arraySpazioLivelli[i] = 0;
}
var spazioOccupato = 0;
//initialize the canvas
var canvas = document.getElementById('myCanvas');
canvas.height = levelLength * (livello + 1);
canvas.width = window.innerWidth;
//function that take in input the datastructure resulting from the parse tree and the level and draws the entire Tree
function drawTree(dataStructure, level) {
var daDisegnare = true;
var numChild = dataStructure.children.length;
for (let i = 0; i < numChild; i++) {
drawTree(dataStructure.children[i], level + 1);
}
if (numChild == 0) { //base case
var spazioNodo = dataStructure.name.length * (4.3 + (parseFloat(dataStructure.value) * 10))
if (level > 0 && arraySpazioLivelli[level - 2] > arraySpazioLivelli[level - 1]) {
arraySpazioLivelli[level - 1] = arraySpazioLivelli[level - 2];
}
drawNode(dataStructure, arraySpazioLivelli[level - 1], level * levelLength);
arraySpazioLivelli[level - 1] += spazioNodo + brotherDistance;
for (let i = level - 1; i < arraySpazioLivelli.length; i++) {
if (arraySpazioLivelli[i] < (arraySpazioLivelli[level - 1])) {
arraySpazioLivelli[i] = (arraySpazioLivelli[level - 1]);
}
}
daDisegnare = false;
}
for (let i = 0; i < dataStructure.children.length; i++) {
if (dataStructure.children[i].disegnato == false) {
daDisegnare = false;
}
}
if (daDisegnare == true) {
if (dataStructure.children.length == 1) {
if (dataStructure.children[0].children.length == 0) {
var spazioNodo = dataStructure.name.length * (4.3 + (parseFloat(dataStructure.value) * 10))
drawNode(dataStructure, dataStructure.children[0].x, level * levelLength);
arraySpazioLivelli[level - 1] = dataStructure.children[0].x + spazioNodo + brotherDistance;
} else {
var spazioNodo = dataStructure.name.length * (4.3 + (parseFloat(dataStructure.value) * 10))
var spazioNodoFiglio = dataStructure.children[0].name.length * (4.3 + (parseFloat(dataStructure.value) * 10))
drawNode(dataStructure, arraySpazioLivelli[level - 1], level * levelLength);
arraySpazioLivelli[level - 1] += spazioNodo + brotherDistance;
}
} else {
var XprimoFiglio = dataStructure.children[0].x;
var XultimoFiglio = dataStructure.children[dataStructure.children.length - 1].x;
var spazioNodo = dataStructure.name.length * (4.3 + (parseFloat(dataStructure.value) * 10))
drawNode(dataStructure, arraySpazioLivelli[level - 1], level * levelLength);
arraySpazioLivelli[level - 1] += spazioNodo + brotherDistance;
}
}
}
drawTree(result[0], 1);
drawBranch(result[0], 1);
canvas.width = arraySpazioLivelli[arraySpazioLivelli.length - 1];
for (let i = 0; i < livello; i++) {
arraySpazioLivelli[i] = 0;
}
drawTree(result[0], 1);
drawBranch(result[0], 1);
function drawNode(node, center, height) { //function that draws the node node in posizion center, height
node.x = center;
node.y = height;
node.disegnato = true;
var ctx = canvas.getContext("2d");
ctx.font = 10 + Math.round(node.value * 10) + "px" + " monospace";
ctx.fillStyle = perc2color(node.value);
ctx.imageSmoothingEnabled = false;
ctx.fillText(node.name, center, height);
}
function drawBranch(node, level) { //function that takes in input the node and the respective level and draws the branch to the node childrens
var numChild = node.children.length;
for (let i = 0; i < numChild; i++) {
drawBranch(node.children[i], level + 1);
var ctx = canvas.getContext("2d");
ctx.strokeStyle = perc2color(node.children[i].value);
ctx.imageSmoothingEnabled = false;
ctx.beginPath();
if (node.children[i].children.length == 0) {
ctx.moveTo(node.x + 4.3, node.y + 3);
ctx.lineTo(node.x + 4.3, node.children[i].y - 15);
} else {
var spazioNodo = node.children[i].name.length * (4.3 + (parseFloat(node.children[i].value) * 10))
var spazioNodoPadre = node.name.length * (4.3 + (parseFloat(node.value) * 10))
ctx.moveTo(node.x + spazioNodoPadre / 2, node.y + 3);
ctx.lineTo(node.children[i].x + spazioNodo / 2, node.children[i].y - 15)
}
ctx.closePath();
ctx.stroke();
}
}
function isEmpty(obj) {
return Object.keys(obj).length === 0;
}
function perc2color(perc) { //function that assigns the correct color according to the value
perc = perc * 100;
var r, g, b = 0;
if (perc < 50) {
r = Math.round(5.1 * perc);
g = Math.round(5.1 * perc);
b = Math.round(3.1 * perc);
} else {
g = Math.round(510 - 5.1 * perc);
r = 255;
}
var h = r * 0x10000 + g * 0x100 + b * 0x1;
return '#' + ('000000' + h.toString(16)).slice(-6);
}
function downloadCanvas() { //function that download the Canvas in png
var link = document.createElement('a');
link.download = "canvas.png";
link.href = document.getElementById("myCanvas").toDataURL("image/png");
link.click();
}