-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGChart.js
More file actions
271 lines (259 loc) · 10.4 KB
/
Copy pathSVGChart.js
File metadata and controls
271 lines (259 loc) · 10.4 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
function SVGChart(container, options) {
let padding = {
left: 60,
top: 60,
bottom: 60,
right: options.width * 0.2
}
let cord = {
x: padding.left,
y: (options.height) - padding.bottom
}
let awidth = (options.width - (padding.left + padding.right))
this.init(container, options, padding, cord, awidth);
}
SVGChart.prototype = (function() {
let max_count = -1
function round(float) {
return Math.round(float)
}
function CNS(type) {
return document.createElementNS("http://www.w3.org/2000/svg", type)
}
SVGElement.prototype.set = function(options = {}) {
for (const [key, val] of Object.entries(options)) {
this.setAttributeNS(null, key, val.toString())
// this.setAttributeNS(null,'stroke-linejoin', 'round')
}
}
HTMLElement.prototype.set = function(options = {}) {
for (const [key, val] of Object.entries(options)) {
this.setAttribute(key, val.toString())
}
}
let Colors = ['#ffaa00', '#55aa00', '#00e5bf', '#e74aae', '#ff007f', '#5555ff', '#ff0000', '#00ff00', '#ebe066', '#ffaa00', '#55aa00', '#00e5bf', '#e74aae', '#ff007f', '#5555ff', '#ff0000', '#00ff00', '#ebe066']
let Colors2 = ['#ff2566', '#00d7b1', '#ffc568', '#6663e1', '#009cf1', '#93b737']
return {
init(c, o, pad, cord, aw) {
this.pad = pad
this.cord = cord
this.aw = aw
this.w = o.width
this.h = o.height
this.parent = document.getElementById(c)
this.svg = CNS('svg')
this.data = o.data
this.point = o.point
this.svg.set({
'x': 0,
'y': 0,
viewBox: `0 0 ${o.width} ${o.height}`,
'fill': 'none',
'width': this.w,
'height': this.h,
})
let rect = CNS("rect")
this.bg = o.bgColor
rect.set({
'fill': this.bg,
'x': 0,
'y': 0,
'width': o.width,
'height': o.height,
})
this.v_bufer = [] //Alohida ajratiluvchi chiziqlar id larini saqlab turish uchun
this.label = o.label
this.anim = o.animation
this.svg.appendChild(rect)
this.drawT(o.title, o.width / 2, this.pad.top / 2, 'silver', 'middle', 'middle', `font-size:${this.pad.top * 0.3}px`)
this.parent.appendChild(this.svg)
this.max = this.getMax(o.data)
this.max = this.max + (10 - this.max % 10)
this.data.forEach(e => {
max_count = e.data.length > max_count ? e.data.length : max_count
})
this.drawBg()
switch (o.type) {
case 'line':
this.drawLine()
break
case 'spline':
this.drawSpline()
break
}
},
drawT(text, x, y, color, align, balign, style) {
let t = CNS("text")
t.set({ x: x, y: y, fill: color, 'alignment-baseline': balign, 'text-anchor': align, style: style })
let m = document.createTextNode(text)
t.appendChild(m)
this.svg.appendChild(t)
},
drawC(x, y, r, c,cl) {
let circle = CNS('circle')
circle.set({
'stroke': this.bg,
'stroke-width': 5,
'cx': round(x),
'cy': round(y),
'r': r,
'fill': c,
})
if (cl) circle.set({class:cl})
this.svg.appendChild(circle)
},
drawL(x1, y1, x2, y2, color, width) {
let b = CNS('line')
b.set({
'stroke': color,
'stroke-width': width,
'x1': x1,
'y1': y1,
'x2': x2,
'y2': y2,
fill: 'none'
})
this.svg.appendChild(b)
},
getMax(data) {
let buf = []
data.forEach(e => {
buf.push(...e.data)
})
console.log(Math.max(...buf))
return Math.max(...buf)
},
drawBg() {
let color = 'silver'
let h = (this.h - (this.pad.bottom + this.pad.top)) / 5
for (let i = 0; i < 6; i++) {
let y = this.cord.y - i * h
this.drawL(this.cord.x, y, this.cord.x + this.aw, y, '#003d47', 1)
let tex = CNS('text')
tex.set({
'x': this.cord.x - 5,
'y': y,
'fill': 'silver',
'text-anchor': 'end',
'alignment-baseline': 'middle'
})
let node = document.createTextNode((i * this.max / 5).toString())
tex.appendChild(node)
this.svg.appendChild(tex)
}
this.drawL(this.cord.x, this.pad.top - 10, this.cord.x, this.cord.y, color, 1)
this.drawL(this.cord.x, this.cord.y, this.w - this.pad.right, this.cord.y, color, 1)
},
drawLegend(color) {
let g = CNS('g')
g.set({ transform: `translate(${this.cord.x + this.aw + 20},${this.h / 2 - this.data.length * 30 / 2})` })
let h = 5;
for (let i = 0; i < this.data.length; i++) {
let gg = CNS('g')
gg.set({ iden: this.data[i].label,id:'G'+this.data[i].label })
gg.addEventListener('click', (e) => {
let id = e.target.parentNode.getAttribute('iden')
e.target.parentNode.set({x:10})
if (!this.v_bufer.includes(id)) this.v_bufer.push(id)
else
this.v_bufer = this.v_bufer.filter(e => e !== id)
this.data.forEach(e => {
document.getElementById(e.label).set({style:'opacity:0.1;stroke-width:1'})
document.getElementById('G'+e.label).set({style:'opacity:0.3'})
let circle=document.getElementsByClassName('C.'+e.label)
for (let [m,a] of Object.entries(circle))
a.style.opacity='0'
})
this.v_bufer.forEach(e => {
document.getElementById(e).set({style:'opacity:1;stroke-width:3'})
document.getElementById('G'+e).set({style:'opacity:1'})
let circle=document.getElementsByClassName('C.'+e)
for (let [m,a] of Object.entries(circle))
a.style.opacity='1'
})
})
let rec = CNS('rect')
rec.set({ x: 0, y: h, width: 30, height: 15, fill: Colors[i] });
gg.appendChild(rec)
let t = CNS('text')
let n = document.createTextNode(this.data[i].label)
t.set({
x: 34,
y: h + 8,
'text-anchor': 'start',
'alignment-baseline': 'middle',
fill: 'silver',
style: 'font-size:12px'
})
t.appendChild(n)
gg.appendChild(t)
h += 25
g.appendChild(gg)
}
this.svg.appendChild(g)
},
drawLabels() {
let l = this.label
let aw = Math.round(this.aw / max_count)
let xx = this.cord.x + aw / 2
for (let i = 0; i < max_count; i++) {
this.drawT(l[i], xx + i * aw, this.cord.y + 14, 'silver', 'middle', '', 'font-size:12px')
}
},
drawSpline() {
this.data.forEach((el, index) => {
let dif = this.cord.y - this.pad.top
let pat = CNS('path')
let aw = Math.round(this.aw / max_count)
let xx = this.cord.x + aw / 2
let d = `M${xx},${round(this.cord.y - (el.data[0]) / this.max * dif)} `
let data = el.data
for (let i = 0; i < el.data.length - 1; i++) {
let y = round(this.cord.y - data[i + 1] / this.max * dif)
let y1 = round(this.cord.y - data[i] / this.max * dif)
d += `C${xx + round(i * aw + aw / 2)},${y1} `
d += `${xx + round((i + 1) * aw - aw / 2)},${y} `
d += `${xx + round((i + 1) * aw)},${y}`
// this.drawL(this.cord.x + ((i ) * aw),this.cord.y,this.cord.x + ((i ) * aw),y1,'silver',0.5)
// this.drawL(this.cord.x + ((i+1 ) * aw),this.cord.y,this.cord.x + ((i +1) * aw),y,'silver',0.5)
}
pat.set({
'stroke': Colors[index],
'stroke-width': 1.5,
'd': d,
'class': this.anim ? 'path' : '',
id: el.label
})
if (this.anim) {
pat.style.strokeDasharray = pat.getTotalLength()
pat.style.strokeDashoffset = pat.getTotalLength()
}
this.svg.appendChild(pat)
for (let i = 0; i < data.length && this.point; i++) {
this.drawC(xx + i * aw, this.cord.y - data[i] / this.max * dif, 5, Colors[index], 'C.'+el.label)
}
})
this.drawLabels()
this.drawLegend(Colors[0])
},
drawLine() {
this.data.forEach((el, index) => {
let pat = CNS('path')
let aw = Math.round(this.aw / el.data.length)
let d = `M${this.cord.x},${this.cord.y - el.data[0]} `
let data = el.data
for (let i = 0; i < data.length; i++) {
let y = this.cord.y
d += `L${this.cord.x + (i * aw)},${y - data[i]} `
this.drawL(this.cord.x + (i * aw), this.cord.y, this.cord.x + (i * aw), y - data[i], 'silver', 0.5)
}
pat.set({
'stroke': Colors[index],
'stroke-width': 2,
'd': d
})
this.svg.appendChild(pat)
})
}
}
})()