-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdivdraw.js
352 lines (310 loc) · 14 KB
/
divdraw.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
class DivDraw {
constructor(el, w = 300, h = 300, col = '#f1f1f1') {
this.elem = el;
this.width = w;
this.height = h;
this.back = col;
this.initCanvas();
}
initCanvas() {
this.setBackground();
this.setSize();
this.initStyle();
}
initStyle() {
this.elem.style.position = 'relative';
this.elem.style.overflow = 'hidden';
}
setSize(w = this.width, h = this.height) {
this.elem.style.width = `${w}px`;
this.elem.style.height = `${h}px`;
this.elem.setAttribute("width", w);
this.elem.setAttribute("height", h);
}
setBackground(col = this.back) {
this.elem.style.background = col;
}
rect(x, y, w = 1, h = 1, col = 'red', hoverid = false, ret = false) {
var pixel = document.createElement("div");
pixel.style.position = 'absolute';
pixel.style.left = `${x}px`;
pixel.style.top = `${y}px`;
pixel.style.width = `${w}px`;
pixel.style.height = `${h}px`;
pixel.style.background = col;
if(hoverid) {
pixel.style.zIndex = '9';
pixel.style.cursor = 'pointer';
pixel.addEventListener("mouseover", function(e) {
var lab = document.getElementById(hoverid);
lab.style.opacity = '1';
lab.style.visibility = 'visible';
pixel.style.outline = `1px solid ${col}`;
pixel.style.outlineOffset = '3px';
});
pixel.addEventListener("mouseleave", function(e) {
var lab = document.getElementById(hoverid);
lab.style.opacity = '0';
lab.style.visibility = 'hidden';
pixel.style.outline = 'none';
});
}
if(ret) {
return pixel;
} else {
this.elem.appendChild(pixel);
}
}
ellipse(x, y, w, h = w, col = 'red', hoverid = false, ret = false) {
var kreis = document.createElement("div");
kreis.style.position = 'absolute';
kreis.style.left = `${x-(w/2)}px`;
kreis.style.top = `${y-(h/2)}px`;
kreis.style.width = `${w}px`;
kreis.style.height = `${h}px`;
kreis.style.background = col;
kreis.style.zIndex = hoverid?'10':'9';
kreis.style.borderRadius = '50%';
if(hoverid) {
kreis.style.cursor = 'pointer';
kreis.addEventListener("mouseover", function(e) {
var lab = document.getElementById(hoverid);
lab.style.opacity = '1';
lab.style.visibility = 'visible';
kreis.style.outline = `1px solid ${col}`;
kreis.style.outlineOffset = '3px';
});
kreis.addEventListener("mouseleave", function(e) {
var lab = document.getElementById(hoverid);
lab.style.opacity = '0';
lab.style.visibility = 'hidden';
kreis.style.outline = 'none';
});
}
if(ret) {
return kreis;
} else {
this.elem.appendChild(kreis);
}
}
line(x1, y1, x2, y2, col = 'red', stroke = 1, ret = false) {
let w = Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
let angle = Math.asin((y2-y1)/w);
let line = document.createElement("div");
line.style.position = 'absolute';
line.style.left = `${x1}px`;
line.style.top = `${y1}px`;
line.style.width = `${w}px`;
line.style.height = `${stroke}px`;
line.style.transform = `rotate(${angle}rad)`;
line.style.transformOrigin = '0';
line.style.background = col;
if(ret) {
return line;
} else {
this.elem.appendChild(line);
}
}
triangle(x, y, w, h, r, col = 'red', ret = false) {
var drei = document.createElement("div");
drei.style.position = 'absolute';
if(r == 1) drei.style.borderLeft = `${w}px solid transparent`;
else if(r == -1) drei.style.borderRight = `${w}px solid transparent`;
drei.style.borderBottom = `${h}px solid ${col}`;
drei.style.width = '0';
drei.style.left = `${x}px`;
drei.style.top = `${y-h}px`;
if(ret) {
return drei;
} else {
this.elem.appendChild(drei);
}
}
label(x, y, head, data, id, col = 'red', ret = false) {
var label = document.createElement("div");
label.style.position = 'absolute';
let tx = x;
label.style.top = `${y}px`;
label.style.background = 'rgba(0,0,0,01)';
label.style.padding = '5px 10px';
label.style.borderRadius = '6px';
label.style.minWidth = '50px';
label.style.zIndex = '12';
label.style.opacity = '0';
label.style.visibility = 'hidden';
var arrow = document.createElement("div");
arrow.style.position = 'absolute';
arrow.style.width = '12px';
arrow.style.height = '12px';
arrow.style.top = '13px';
arrow.style.transform = 'rotate(45deg)';
arrow.style.background = 'rgba(0,0,0,1)';
var title = document.createElement("h4");
title.innerHTML = head;
title.style.margin = '0';
title.style.color = '#fff';
var dat = document.createElement("span");
dat.innerHTML = data;
dat.style.color = '#fff';
var legend = document.createElement("span");
legend.style.display = 'inline-block';
legend.style.marginRight = '6px';
legend.style.width = '10px';
legend.style.height = '10px';
legend.style.border = '1px solid #fff';
legend.style.backgroundColor = col;
label.appendChild(arrow);
label.appendChild(title);
label.appendChild(legend);
label.appendChild(dat);
label.setAttribute("id", id);
this.elem.appendChild(label);
if(x+label.offsetWidth > this.width) tx-=(label.offsetWidth+28);
label.style.left = `${tx}px`;
if(x+label.offsetWidth > this.width) arrow.style.right = '-6px';
else arrow.style.left = '-6px';
}
text(x, y, t, breit = 'fit-content', size = 16, col = '#000', bold = false, ret = false) {
var satz = document.createElement("h3");
satz.innerHTML = t;
satz.style.position = 'absolute';
satz.style.margin = '0';
satz.style.width = `${breit}px`;
if(breit != 'fit-content') satz.style.textAlign = 'center';
satz.style.left = `${x}px`;
satz.style.top = `${y}px`;
satz.style.fontSize = `${size}px`;
satz.style.color = col;
if(bold) satz.style.fontWeight = `${bold}`;
if(ret) {
return satz;
} else {
this.elem.appendChild(satz);
}
}
clear() {
this.elem.innerHTML = "";
}
legend(x, y, t, col, size = 16, ret = false, graphs, index, obj) {
var legende = document.createElement("div");
legende.style.cursor = 'pointer';
var legrect = this.rect(x, y, 20, 14, col, false, true);
var txt = this.text(x+25, y, t, 'fit-content', 12, '#000', false, true);
if(graphs[index].dontDraw) txt.style.textDecoration = 'line-through';
legende.appendChild(legrect);
legende.appendChild(txt);
legende.onclick = (e) => {
obj.clear();
if(graphs[index].dontDraw) graphs[index].dontDraw = false;
else graphs[index].dontDraw = true;
obj.graph(graphs[0]!=null?graphs[0]:null,
graphs[1]!=null?graphs[1]:null,
graphs[2]!=null?graphs[2]:null,
graphs[3]!=null?graphs[3]:null,
graphs[4]!=null?graphs[4]:null,
graphs[5]!=null?graphs[5]:null,
graphs[6]!=null?graphs[6]:null);
};
this.elem.appendChild(legende);
}
graph() {
if(arguments.length > 7) console.log("Graph(p1, p2,...,p7) cannot have more than 7 Input Parameters!");
else {
let start = 30;
let breite, teiler;
let graphheight = this.height - 30;
let arglen = 0;
for(let k = 0; k < arguments.length; ++k) if(arguments[k] != null) ++arglen;
let maxelem = 0;
for(let k = 0; k < arguments.length; ++k) {
if(arguments[k] != null) {
let grap = arguments[k];
if(Math.max.apply(null, grap.data) > maxelem) maxelem = Math.max.apply(null, grap.data);
breite = (this.width-start) / grap.data.length;
teiler = Math.round(60*grap.data.length/(this.width-breite));
this.legend(k*this.width/arglen +start+10, 3, grap.label, grap.color, 12, false, arguments, k, this);
}
}
maxelem = parseInt(maxelem * 1.15);
while(++maxelem%4 != 0);
let anzmax = (this.height*1.3) / 100;
let anzahlschritte = parseInt(maxelem>=anzmax? anzmax:maxelem)>10? 10:parseInt(maxelem>=anzmax? anzmax:maxelem);
//y-axis
this.line(start, 0, start, graphheight, '#000', 2);
//x-axis
this.line(start, graphheight, this.width, graphheight, '#000', 2);
let ylabel;
for(let i = 0; i < anzahlschritte; ++i) {
//long lines parallel to the x-axis
this.line(start, i*graphheight/anzahlschritte, this.width, i*graphheight/anzahlschritte, '#bbb', 1);
//marks on the y-axis
this.line(start-3, i*graphheight/anzahlschritte, start+3, i*graphheight/anzahlschritte, '#000', 2);
//labels for the y-axis
ylabel = maxelem - (i*maxelem/anzahlschritte);
this.text(start-30, i*graphheight/anzahlschritte-3, Number.isInteger(ylabel)?parseInt(ylabel):ylabel.toFixed(1), 'fit-content', 13, '#555', 900);
}
for(let k = 0; k < arguments.length; ++k) {
if(arguments[k] != null) {
let graphic = arguments[k];
if(graphic.type == 'bar' && !graphic.dontDraw) {
let x, y, xn, yn;
for(let i = 0; i < graphic.data.length; ++i) {
x = Math.round(i*breite +start);
y = Math.round(graphheight-(graphic.data[i]*graphheight/maxelem));
xn = Math.round((i+1)*breite +start);
yn = Math.round(graphheight-(graphic.data[i+1]*graphheight/maxelem));
//drawing the bars
this.rect(x, y, breite*0.85, graphheight-y, graphic.color, `${graphic.label}${k}${i}`);
this.label(x+(breite/2), y-20, graphic.labels[i], graphic.label+': '+graphic.data[i], `${graphic.label}${k}${i}`, graphic.color);
if(i % teiler == 0) {
//labels for the x-axis
this.text(x, graphheight+3, graphic.labels[i], 'fit-content', 12, '#555');
}
}
} else if(graphic.type == 'line' && !graphic.dontDraw) {
let x, y, xn, yn;
for(let i = 0; i < graphic.data.length; ++i) {
x = Math.round(i*breite +start);
y = Math.round(graphheight-(graphic.data[i]*graphheight/maxelem));
xn = Math.round((i+1)*breite +start);
yn = Math.round(graphheight-(graphic.data[i+1]*graphheight/maxelem));
if(i+2 <= graphic.data.length) {
//drawing the lines between points
this.line(x, y, xn, yn, graphic.lineColor, graphic.lineStroke);
//Fill area under curve
if(graphic.fill != null) {
if(y > yn) {
this.rect(x, y, xn-x, graphheight-y, graphic.fill);
this.triangle(x, y, xn-x, y-yn, 1, graphic.fill);
} else if (yn > y) {
this.rect(x, yn, xn-x, graphheight-yn, graphic.fill);
this.triangle(x, yn, xn-x, yn-y, -1, graphic.fill);
} else if(yn == y) {
this.rect(x, yn, xn-x, graphheight-yn, graphic.fill);
}
}
}
//drawing the points
this.ellipse(x, y, graphic.stroke, graphic.stroke, graphic.color, `${k}${i}`);
this.label(x+12, y-20, graphic.labels[i], graphic.label+': '+graphic.data[i],`${k}${i}`, graphic.color);
if(i % teiler == 0) {
//labels for the x-axis
this.text(x, graphheight+3, graphic.labels[i], 'fit-content', 12, '#555');
}
}
}
}
}
}
}
}
function map(val, lower, upper, tolower, toupper) {
let mapped;
if(tolower > toupper) {
mapped = ((Math.abs(lower - val) / (upper - lower)) * Math.abs(tolower - toupper)) + toupper;
mapped = tolower - mapped;
} else {
mapped = ((Math.abs(lower - val) / (upper - lower)) * Math.abs(toupper - tolower)) + tolower;
}
return mapped;
}