1 | /** ---------------------------------------------------------------------------- |
---|
2 | * file : CanvasDraw.GraphClasses.js |
---|
3 | * file version : 1.0 |
---|
4 | * date : 2011-10-22 |
---|
5 | * ----------------------------------------------------------------------------- |
---|
6 | * author: grum at grum.fr |
---|
7 | * << May the Little SpaceFrog be with you >> |
---|
8 | * |
---|
9 | * This program is free software and is published under the terms of the GNU GPL |
---|
10 | * Please read CanvasDraw.ReadMe.txt file for more information |
---|
11 | * |
---|
12 | * ----------------------------------------------------------------------------- |
---|
13 | * |
---|
14 | * dependencies : none |
---|
15 | * |
---|
16 | * ----------------------------------------------------------------------------- |
---|
17 | * |
---|
18 | * - CDMargins() |
---|
19 | * . top |
---|
20 | * . bottom |
---|
21 | * . left |
---|
22 | * . right |
---|
23 | * |
---|
24 | * - CDAxis() |
---|
25 | * . set() |
---|
26 | * . setValues() |
---|
27 | * . get() |
---|
28 | * . getXYHPos() |
---|
29 | * . getVPos() |
---|
30 | * . getXYVStepPos() |
---|
31 | * . getXYAxisValues() |
---|
32 | * . getXYAxisValues() |
---|
33 | * |
---|
34 | * - CDSerie() |
---|
35 | * . |
---|
36 | * |
---|
37 | * ----------------------------------------------------------------------------- |
---|
38 | */ |
---|
39 | |
---|
40 | |
---|
41 | /** |
---|
42 | * margins |
---|
43 | */ |
---|
44 | function CDMargins(values) |
---|
45 | { |
---|
46 | var _this=this, |
---|
47 | margins={ |
---|
48 | left:0, |
---|
49 | right:0, |
---|
50 | bottom:0, |
---|
51 | top:0 |
---|
52 | }, |
---|
53 | |
---|
54 | init = function (values) |
---|
55 | { |
---|
56 | _this.set(values); |
---|
57 | }; |
---|
58 | |
---|
59 | this.setLeft = function(value) |
---|
60 | { |
---|
61 | if(value>=0) margins.left=value; |
---|
62 | return(margins.left); |
---|
63 | }; |
---|
64 | |
---|
65 | this.setRight = function(value) |
---|
66 | { |
---|
67 | if(value>=0) margins.right=value; |
---|
68 | return(margins.right); |
---|
69 | }; |
---|
70 | |
---|
71 | this.setTop = function(value) |
---|
72 | { |
---|
73 | if(value>=0) margins.top=value; |
---|
74 | return(margins.top); |
---|
75 | }; |
---|
76 | |
---|
77 | this.setBottom = function(value) |
---|
78 | { |
---|
79 | if(value>=0) margins.bottom=value; |
---|
80 | return(margins.bottom); |
---|
81 | }; |
---|
82 | |
---|
83 | this.get = function () |
---|
84 | { |
---|
85 | return(margins); |
---|
86 | }; |
---|
87 | |
---|
88 | this.set = function (value) |
---|
89 | { |
---|
90 | if(value instanceof CDMargins) |
---|
91 | { |
---|
92 | tmp=value.get(); |
---|
93 | margins.left=tmp.left; |
---|
94 | margins.top=tmp.top; |
---|
95 | margins.bottom=tmp.bottom; |
---|
96 | margins.right=tmp.right; |
---|
97 | return(margins); |
---|
98 | } |
---|
99 | if($.isPlainObject(value)) |
---|
100 | { |
---|
101 | if(value.left && value.left>0) margins.left=value.left; |
---|
102 | if(value.right && value.right>0) margins.right=value.right; |
---|
103 | if(value.top && value.top>0) margins.top=value.top; |
---|
104 | if(value.bottom && value.bottom>0) margins.bottom=value.bottom; |
---|
105 | } |
---|
106 | return(margins); |
---|
107 | }; |
---|
108 | |
---|
109 | this.equals = function (value) |
---|
110 | { |
---|
111 | if(value instanceof CDMargins) |
---|
112 | { |
---|
113 | tmp=value.get(); |
---|
114 | if(margins.left==tmp.left && |
---|
115 | margins.top==tmp.top && |
---|
116 | margins.bottom==tmp.bottom && |
---|
117 | margins.right==tmp.right) return(true); |
---|
118 | return(false); |
---|
119 | } |
---|
120 | if(value.left && value.right && value.top && value.bottom) |
---|
121 | { |
---|
122 | if(value.left!=margins.left || |
---|
123 | value.right!=margins.right || |
---|
124 | value.top!=margins.top || |
---|
125 | value.bottom!=margins.bottom) return(false); |
---|
126 | return(true); |
---|
127 | } |
---|
128 | return(false); |
---|
129 | }; |
---|
130 | |
---|
131 | init(values); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * axis properties |
---|
136 | */ |
---|
137 | function CDAxis(options, values) |
---|
138 | { |
---|
139 | var _this=this, |
---|
140 | properties={ |
---|
141 | mode:'XY', // 'XY', 'pie' |
---|
142 | series:[], |
---|
143 | size:{ |
---|
144 | width:0, |
---|
145 | height:0 |
---|
146 | }, |
---|
147 | display:{ |
---|
148 | color:'rgba(128,128,128,1)', |
---|
149 | visible: |
---|
150 | { |
---|
151 | XY:{ |
---|
152 | H:true, |
---|
153 | V:true, |
---|
154 | cursorH:true, |
---|
155 | cursorV:true |
---|
156 | }, |
---|
157 | pie:true |
---|
158 | } |
---|
159 | }, |
---|
160 | XY:{ |
---|
161 | ticks:{ |
---|
162 | H:{ |
---|
163 | size:{ |
---|
164 | small:0, |
---|
165 | big:3 |
---|
166 | }, |
---|
167 | visible:{ |
---|
168 | small:false, |
---|
169 | big:true |
---|
170 | }, |
---|
171 | minSpaceLast:5, |
---|
172 | frequency:{ |
---|
173 | small:0, |
---|
174 | big:1 |
---|
175 | }, |
---|
176 | value:{ |
---|
177 | rotate:90, |
---|
178 | visible:true, |
---|
179 | fontName:'arial', |
---|
180 | fontSize:8 |
---|
181 | }, |
---|
182 | offset0:false |
---|
183 | }, |
---|
184 | V:{ |
---|
185 | unit:'', |
---|
186 | size:{ |
---|
187 | small:1, |
---|
188 | big:3 |
---|
189 | }, |
---|
190 | visible:{ |
---|
191 | small:true, |
---|
192 | big:true |
---|
193 | }, |
---|
194 | minSpaceLast:5, |
---|
195 | steps:100, |
---|
196 | frequency:{ |
---|
197 | small:5, |
---|
198 | big:10 |
---|
199 | }, |
---|
200 | value:{ |
---|
201 | rotate:0, |
---|
202 | visible:true, |
---|
203 | fontName:'arial', |
---|
204 | fontSize:8 |
---|
205 | } |
---|
206 | } |
---|
207 | }, |
---|
208 | values:{ |
---|
209 | H:[], |
---|
210 | minV:0, |
---|
211 | maxV:100 |
---|
212 | } |
---|
213 | }, |
---|
214 | pie:{ |
---|
215 | unit:'', |
---|
216 | innerRadius:0.6, // between 0 and 1 |
---|
217 | outerRadius:1.5, // greater than 1 |
---|
218 | fontName:'arial', |
---|
219 | fontSize:8 |
---|
220 | } |
---|
221 | |
---|
222 | }, |
---|
223 | data={ |
---|
224 | XY:{ |
---|
225 | H:{ |
---|
226 | space:0, |
---|
227 | size:0, |
---|
228 | center:0 |
---|
229 | }, |
---|
230 | V:{ |
---|
231 | space:0, |
---|
232 | factor:1, |
---|
233 | size:0, |
---|
234 | center:0 |
---|
235 | } |
---|
236 | }, |
---|
237 | pie:{ |
---|
238 | H:{center:0}, |
---|
239 | V:{center:0} |
---|
240 | } |
---|
241 | }, |
---|
242 | |
---|
243 | // functions |
---|
244 | init = function (options) |
---|
245 | { |
---|
246 | if($.isPlainObject(options)) _this.set(options); |
---|
247 | }, |
---|
248 | |
---|
249 | computeData = function () |
---|
250 | { |
---|
251 | data.XY.H.size=properties.size.width-properties.XY.ticks.H.minSpaceLast; |
---|
252 | data.XY.H.center=Math.round(properties.size.width/2); |
---|
253 | data.XY.V.size=properties.size.height-properties.XY.ticks.V.minSpaceLast; |
---|
254 | data.XY.V.center=Math.round(properties.size.height/2); |
---|
255 | |
---|
256 | if(properties.XY.values.H.length>0) |
---|
257 | { |
---|
258 | data.XY.H.space=Math.round((data.XY.H.size-properties.XY.values.H.length)/properties.XY.values.H.length)+1; |
---|
259 | } |
---|
260 | else |
---|
261 | { |
---|
262 | data.XY.H.space=0; |
---|
263 | } |
---|
264 | |
---|
265 | |
---|
266 | data.XY.V.space=Math.floor(10000*data.XY.V.size/properties.XY.ticks.V.steps)/10000; |
---|
267 | data.XY.V.factor=Math.abs(data.XY.V.size)/properties.XY.ticks.V.steps; |
---|
268 | |
---|
269 | data.pie.H.center=Math.round(properties.size.width/2); |
---|
270 | data.pie.V.center=Math.round(properties.size.height/2); |
---|
271 | }, |
---|
272 | |
---|
273 | serieFind = function (name) |
---|
274 | { |
---|
275 | for(var i=0;i<properties.series.length;i++) |
---|
276 | { |
---|
277 | if(properties.series[i].get().name==name) return(i); |
---|
278 | } |
---|
279 | return(-1); |
---|
280 | }, |
---|
281 | |
---|
282 | serieSort = function () |
---|
283 | { |
---|
284 | properties.series.sort(serieCompareOrder); |
---|
285 | |
---|
286 | // calculate cumulative values |
---|
287 | for(var i=properties.series.length-1;i>=0;i--) |
---|
288 | { |
---|
289 | var current=properties.series[i].get().options.get(); |
---|
290 | if(current.mode!=null && current.mode=='cumulative') |
---|
291 | { |
---|
292 | properties.series[i].get().options.cumulativeInit(properties.series[i].get().values); |
---|
293 | for(var j=i-1;j>=0;j--) |
---|
294 | { |
---|
295 | var next=properties.series[j].get().options.get(); |
---|
296 | if(next.mode!=null && next.mode=='cumulative') |
---|
297 | { |
---|
298 | properties.series[i].get().options.cumulativeAdd(properties.series[j].get().values); |
---|
299 | } |
---|
300 | } |
---|
301 | } |
---|
302 | } |
---|
303 | }, |
---|
304 | |
---|
305 | serieCompareOrder = function (a, b) |
---|
306 | { |
---|
307 | aO=a.get().order; |
---|
308 | bO=b.get().order; |
---|
309 | if(aO==bO) return(0); |
---|
310 | return((aO<bO)?-1:1); |
---|
311 | }; |
---|
312 | |
---|
313 | this.set = function (value) |
---|
314 | { |
---|
315 | if(value.mode!=null && |
---|
316 | (value.mode=='XY' || value.mode=='pie') |
---|
317 | ) properties.mode=value.mode; |
---|
318 | |
---|
319 | if(value.size!=null) |
---|
320 | { |
---|
321 | if(value.size.width!=null) properties.size.width=value.size.width; |
---|
322 | if(value.size.height!=null) properties.size.height=value.size.height; |
---|
323 | } |
---|
324 | |
---|
325 | if(value.display!=null) |
---|
326 | { |
---|
327 | if(value.display.color!=null) properties.display.color=value.display.color; |
---|
328 | if(value.display.visible!=null) |
---|
329 | { |
---|
330 | if(value.display.visible.XY!=null) |
---|
331 | { |
---|
332 | if(value.display.visible.XY.H==true || value.display.visible.XY.H==false) properties.display.visible.XY.H=value.display.visible.XY.H; |
---|
333 | if(value.display.visible.XY.V==true || value.display.visible.XY.V==false) properties.display.visible.XY.V=value.display.visible.XY.V; |
---|
334 | if(value.display.visible.XY.cursorH==true || value.display.visible.XY.cursorH==false) properties.display.visible.XY.cursorH=value.display.visible.XY.cursorH; |
---|
335 | if(value.display.visible.XY.cursorV==true || value.display.visible.XY.cursorV==false) properties.display.visible.XY.cursorV=value.display.visible.XY.cursorV; |
---|
336 | } |
---|
337 | } |
---|
338 | } |
---|
339 | |
---|
340 | if(value.XY!=null) |
---|
341 | { |
---|
342 | if(value.XY.ticks!=null) |
---|
343 | { |
---|
344 | if(value.XY.ticks.H!=null) |
---|
345 | { |
---|
346 | if(value.XY.ticks.H.size!=null && !$.isPlainObject(value.XY.ticks.H.size)) |
---|
347 | { |
---|
348 | properties.XY.ticks.H.size.small=value.XY.ticks.H.size; |
---|
349 | properties.XY.ticks.H.size.big=value.XY.ticks.H.size; |
---|
350 | } |
---|
351 | else if(value.XY.ticks.H.size!=null && $.isPlainObject(value.XY.ticks.H.size)) |
---|
352 | { |
---|
353 | if(value.XY.ticks.H.size.small) properties.XY.ticks.H.size.small=value.XY.ticks.H.size.small; |
---|
354 | if(value.XY.ticks.H.size.big) properties.XY.ticks.H.size.big=value.XY.ticks.H.size.big; |
---|
355 | } |
---|
356 | |
---|
357 | if(value.XY.ticks.H.visible==true || value.XY.ticks.H.visible==false) |
---|
358 | { |
---|
359 | properties.XY.ticks.H.visible.small=value.XY.ticks.H.visible; |
---|
360 | properties.XY.ticks.H.visible.big=value.XY.ticks.H.visible; |
---|
361 | } |
---|
362 | else if(value.XY.ticks.H.visible && $.isPlainObject(value.XY.ticks.H.visible)) |
---|
363 | { |
---|
364 | if(value.XY.ticks.H.visible.small==true || value.XY.ticks.H.visible.small==false) properties.XY.ticks.H.visible.small=value.XY.ticks.H.visible.small; |
---|
365 | if(value.XY.ticks.H.visible.big==true || value.XY.ticks.H.visible.big==false) properties.XY.ticks.H.visible.big=value.XY.ticks.H.visible.big; |
---|
366 | } |
---|
367 | |
---|
368 | if(value.XY.ticks.H.frequency!=null) |
---|
369 | { |
---|
370 | if(value.XY.ticks.H.frequency.small!=null) properties.XY.ticks.H.frequency.small=value.XY.ticks.H.frequency.small; |
---|
371 | if(value.XY.ticks.H.frequency.big!=null) properties.XY.ticks.H.frequency.big=value.XY.ticks.H.frequency.big; |
---|
372 | } |
---|
373 | |
---|
374 | if(value.XY.ticks.H.value!=null) |
---|
375 | { |
---|
376 | if(value.XY.ticks.H.value.rotate!=null) properties.XY.ticks.H.value.rotate=value.XY.ticks.H.value.rotate%180; |
---|
377 | if(value.XY.ticks.H.value.visible==true || value.XY.ticks.H.value.visible==false) properties.XY.ticks.H.value.visible=value.XY.ticks.H.value.visible; |
---|
378 | if(value.XY.ticks.H.value.fontName!=null) properties.XY.ticks.H.value.fontName=value.XY.ticks.H.value.fontName; |
---|
379 | if(value.XY.ticks.H.value.fontSize!=null) properties.XY.ticks.H.value.fontSize=value.XY.ticks.H.value.fontSize; |
---|
380 | } |
---|
381 | |
---|
382 | if(value.XY.ticks.H.minSpaceLast!=null && value.XY.ticks.minSpaceLast>=0) properties.XY.ticks.H.minSpaceLast=value.XY.ticks.H.minSpaceLast; |
---|
383 | if(value.XY.ticks.H.offset0==true || value.XY.ticks.H.offset0==false) properties.XY.ticks.H.offset0=value.XY.ticks.H.offset0; |
---|
384 | } |
---|
385 | |
---|
386 | if(value.XY.ticks.V!=null) |
---|
387 | { |
---|
388 | if(value.XY.ticks.V.size!=null && !$.isPlainObject(value.XY.ticks.V.size)) |
---|
389 | { |
---|
390 | properties.XY.ticks.V.size.small=value.XY.ticks.V.size; |
---|
391 | properties.XY.ticks.V.size.big=value.XY.ticks.V.size; |
---|
392 | } |
---|
393 | else if(value.XY.ticks.V.size!=null) |
---|
394 | { |
---|
395 | if(value.XY.ticks.V.size.small!=null) properties.XY.ticks.V.size.small=value.XY.ticks.V.size.small; |
---|
396 | if(value.XY.ticks.V.size.big!=null) properties.XY.ticks.V.size.big=value.XY.ticks.V.size.big; |
---|
397 | } |
---|
398 | |
---|
399 | if(value.XY.ticks.V.visible==true || value.XY.ticks.V.visible==false) |
---|
400 | { |
---|
401 | properties.XY.ticks.V.visible.small=value.XY.ticks.V.visible; |
---|
402 | properties.XY.ticks.V.visible.big=value.XY.ticks.V.visible; |
---|
403 | } |
---|
404 | else if(value.XY.ticks.V.visible!=null && $.isPlainObject(value.XY.ticks.V.visible)) |
---|
405 | { |
---|
406 | if(value.XY.ticks.V.visible.small==true || value.XY.ticks.V.visible.small==false) properties.XY.ticks.V.visible.small=value.XY.ticks.V.visible.small; |
---|
407 | if(value.XY.ticks.V.visible.big==true || value.XY.ticks.V.visible.big==false) properties.XY.ticks.V.visible.big=value.XY.ticks.V.visible.big; |
---|
408 | } |
---|
409 | |
---|
410 | if(value.XY.ticks.V.frequency!=null) |
---|
411 | { |
---|
412 | if(value.XY.ticks.V.frequency.small!=null) properties.XY.ticks.V.frequency.small=value.XY.ticks.V.frequency.small; |
---|
413 | if(value.XY.ticks.V.frequency.big!=null) properties.XY.ticks.V.frequency.big=value.XY.ticks.V.frequency.big; |
---|
414 | } |
---|
415 | |
---|
416 | if(value.XY.ticks.V.value!=null) |
---|
417 | { |
---|
418 | if(value.XY.ticks.V.value.rotate!=null) properties.XY.ticks.V.value.rotate=value.XY.ticks.V.value.rotate%180; |
---|
419 | if(value.XY.ticks.V.value.visible==true || value.XY.ticks.V.value.visible==false) properties.XY.ticks.V.value.visible=value.XY.ticks.V.value.visible; |
---|
420 | if(value.XY.ticks.V.value.fontName!=null) properties.XY.ticks.V.value.fontName=value.XY.ticks.V.value.fontName; |
---|
421 | if(value.XY.ticks.V.value.fontSize!=null) properties.XY.ticks.V.value.fontSize=value.XY.ticks.V.value.fontSize; |
---|
422 | } |
---|
423 | |
---|
424 | if(value.XY.ticks.V.minSpaceLast!=null && value.XY.ticks.V.minSpaceLast>=0) properties.XY.ticks.V.minSpaceLast=value.XY.ticks.V.minSpaceLast; |
---|
425 | if(value.XY.ticks.V.steps!=null && value.XY.ticks.V.steps>=0) properties.XY.ticks.V.steps=value.XY.ticks.V.steps; |
---|
426 | if(value.XY.ticks.V.unit!=null) properties.XY.ticks.V.unit=value.XY.ticks.V.unit; |
---|
427 | } |
---|
428 | } |
---|
429 | |
---|
430 | if(value.XY.values) this.setValues(value.XY.values); |
---|
431 | } |
---|
432 | |
---|
433 | if(value.pie!=null) |
---|
434 | { |
---|
435 | if(value.pie.unit!=null) properties.pie.unit=value.pie.unit; |
---|
436 | |
---|
437 | if(value.pie.innerRadius!=null && value.pie.innerRadius>=0 && value.pie.innerRadius<=1) properties.pie.innerRadius=value.pie.innerRadius; |
---|
438 | if(value.pie.outerRadius!=null && value.pie.outerRadius>=1) properties.pie.outerRadius=value.pie.outerRadius; |
---|
439 | |
---|
440 | if(value.pie.fontName!=null) properties.pie.fontName=value.pie.fontName; |
---|
441 | if(value.pie.fontSize!=null) properties.pie.fontSize=value.pie.fontSize; |
---|
442 | } |
---|
443 | |
---|
444 | if(value.series!=null && $.isArray(value.series)) |
---|
445 | for(var i=0;i<value.series.length;i++) |
---|
446 | { |
---|
447 | this.serieAdd(value.series[i]); |
---|
448 | } |
---|
449 | |
---|
450 | computeData(); |
---|
451 | }; |
---|
452 | |
---|
453 | this.setValues = function (value) |
---|
454 | { |
---|
455 | if(value.H && $.isArray(value.H)) properties.XY.values.H=value.H; |
---|
456 | value.minV?tmpMin=value.minV:tmpMin=properties.XY.values.minV; |
---|
457 | value.maxV?tmpMax=value.maxV:tmpMax=properties.XY.values.maxV; |
---|
458 | |
---|
459 | if(tmpMin>0 && tmpMin<tmpMax) properties.XY.values.minV=tmpMin; |
---|
460 | if(tmpMax>0 && tmpMin<tmpMax) properties.XY.values.maxV=tmpMax; |
---|
461 | |
---|
462 | computeData(); |
---|
463 | }; |
---|
464 | |
---|
465 | this.get = function () |
---|
466 | { |
---|
467 | return({properties:properties, data:data}); |
---|
468 | }; |
---|
469 | |
---|
470 | this.getXYHPos = function (index, applyOffset) |
---|
471 | { |
---|
472 | var offset=0; |
---|
473 | |
---|
474 | if(applyOffset==null) applyOffset=true; |
---|
475 | |
---|
476 | if(applyOffset==true && properties.XY.ticks.H.offset0) offset=0.5; |
---|
477 | return(Math.round(data.XY.H.space*(index+offset))); |
---|
478 | }; |
---|
479 | |
---|
480 | this.getXYVPos = function (value) |
---|
481 | { |
---|
482 | return(data.XY.V.factor*value); |
---|
483 | }; |
---|
484 | |
---|
485 | this.getXYVStepPos = function (value) |
---|
486 | { |
---|
487 | return(Math.round(data.XY.V.space*value)); |
---|
488 | }; |
---|
489 | |
---|
490 | this.getXYAxisValues = function (x, y) |
---|
491 | { |
---|
492 | var H=0, |
---|
493 | V=0; |
---|
494 | |
---|
495 | //if(properties.XY.ticks.H.offset0) x+=data.XY.H.space/2; |
---|
496 | H=(data.XY.H.space==0)?0:Math.floor(x/data.XY.H.space); |
---|
497 | V=(data.XY.V.space==0)?0:y/data.XY.V.space; |
---|
498 | |
---|
499 | return({H:H, V:V}); |
---|
500 | }; |
---|
501 | |
---|
502 | /** |
---|
503 | * Add a serie to the axis; the serie type must be compatible with the axis mode |
---|
504 | * Note: the serie name is unique for an axis; if the serie name already exist, |
---|
505 | * she won't be added |
---|
506 | * |
---|
507 | * @param CDSerie serie: the serie to Add |
---|
508 | * @return boolean: true if serie was added otherwise false |
---|
509 | */ |
---|
510 | this.serieAdd = function (serie) |
---|
511 | { |
---|
512 | var index=-1, |
---|
513 | serieType=''; |
---|
514 | |
---|
515 | if(serie instanceof CDSerie) |
---|
516 | { |
---|
517 | serieType=serie.get().options.get().type; |
---|
518 | |
---|
519 | if( |
---|
520 | (properties.mode=='XY' && |
---|
521 | (serieType=='bar' || |
---|
522 | serieType=='area' || |
---|
523 | serieType=='line') |
---|
524 | ) || |
---|
525 | (properties.mode=='pie' && serieType=='pie') |
---|
526 | ) |
---|
527 | { |
---|
528 | index=serieFind(serie.get().name); |
---|
529 | if(index==-1) |
---|
530 | { |
---|
531 | properties.series.push(serie); |
---|
532 | serieSort(); |
---|
533 | return(true); |
---|
534 | } |
---|
535 | } |
---|
536 | } |
---|
537 | return(false); |
---|
538 | }; |
---|
539 | |
---|
540 | this.serieGet = function (serieName) |
---|
541 | { |
---|
542 | var index=-1; |
---|
543 | |
---|
544 | if(serieName instanceof CDSerie) |
---|
545 | { |
---|
546 | serieName=serieName.get().name; |
---|
547 | } |
---|
548 | if(serieName!=null) |
---|
549 | { |
---|
550 | index=serieFind(serieName); |
---|
551 | if(index>=0) return(properties.series[index]); |
---|
552 | } |
---|
553 | return(properties.series); |
---|
554 | }; |
---|
555 | |
---|
556 | this.serieDelete = function (serie) |
---|
557 | { |
---|
558 | var index=-1; |
---|
559 | |
---|
560 | if(serieName instanceof CDSerie) |
---|
561 | { |
---|
562 | serieName=serieName.get().name; |
---|
563 | } |
---|
564 | if(serieName!=null) |
---|
565 | { |
---|
566 | index=serieFind(serieName); |
---|
567 | if(index>=0) |
---|
568 | { |
---|
569 | properties.series.splice(index, 1); |
---|
570 | serieSort(); |
---|
571 | return(true); |
---|
572 | } |
---|
573 | } |
---|
574 | return(false); |
---|
575 | }; |
---|
576 | |
---|
577 | init(options, values); |
---|
578 | } |
---|
579 | |
---|
580 | |
---|
581 | function CDSerie(initProperties) |
---|
582 | { |
---|
583 | var _this=this, |
---|
584 | properties={ |
---|
585 | name:'', |
---|
586 | order:0, |
---|
587 | values:[], |
---|
588 | valuesLabels:[], |
---|
589 | options:{ } |
---|
590 | }, |
---|
591 | |
---|
592 | init = function (values) |
---|
593 | { |
---|
594 | _this.set(values); |
---|
595 | }; |
---|
596 | |
---|
597 | |
---|
598 | this.set = function (values) |
---|
599 | { |
---|
600 | if(!$.isPlainObject(values)) return(false); |
---|
601 | |
---|
602 | if(values.name!=null) properties.name=values.name; |
---|
603 | if(values.order!=null && values.order>=0) properties.order=values.order; |
---|
604 | |
---|
605 | if(values.values!=null && $.isArray(values.values)) properties.values=values.values; |
---|
606 | |
---|
607 | if(values.valuesLabels!=null && $.isArray(values.valuesLabels)) |
---|
608 | { |
---|
609 | properties.valuesLabels=values.valuesLabels; |
---|
610 | } |
---|
611 | |
---|
612 | if(properties.valuesLabels.length>properties.values.length) |
---|
613 | { |
---|
614 | properties.valuesLabels.splice(properties.values.length); |
---|
615 | } |
---|
616 | else if(properties.valuesLabels.length<properties.values.length) |
---|
617 | { |
---|
618 | var nb=properties.values.length-properties.valuesLabels.length; |
---|
619 | |
---|
620 | for(var i=0;i<nb;i++) |
---|
621 | { |
---|
622 | properties.valuesLabels.push(''); |
---|
623 | } |
---|
624 | } |
---|
625 | |
---|
626 | if(values.options!=null && |
---|
627 | (values.options instanceof CDSerieOptionsBar || |
---|
628 | values.options instanceof CDSerieOptionsArea || |
---|
629 | values.options instanceof CDSerieOptionsLine || |
---|
630 | values.options instanceof CDSerieOptionsPie) |
---|
631 | ) |
---|
632 | { |
---|
633 | delete properties.options; |
---|
634 | properties.options=values.options; |
---|
635 | } |
---|
636 | |
---|
637 | return(true); |
---|
638 | }; |
---|
639 | |
---|
640 | this.get = function () |
---|
641 | { |
---|
642 | return(properties); |
---|
643 | } |
---|
644 | |
---|
645 | init(initProperties); |
---|
646 | } |
---|
647 | |
---|
648 | function CDSerieOptionsBar(initProperties) |
---|
649 | { |
---|
650 | var _this=this, |
---|
651 | properties={ |
---|
652 | type:'bar', |
---|
653 | mode:'normal', // normal, cumulative |
---|
654 | backgroundColor:'#ccccff', |
---|
655 | borderColor:'#ffffff', |
---|
656 | borderWidth:1, |
---|
657 | width:1, // bar width in percentage; 1=100% |
---|
658 | offset:0, // x offset applied to display the bar |
---|
659 | cumulativesValues:[] |
---|
660 | }, |
---|
661 | |
---|
662 | init = function (values) |
---|
663 | { |
---|
664 | _this.set(values); |
---|
665 | }; |
---|
666 | |
---|
667 | this.set = function (values) |
---|
668 | { |
---|
669 | if(!$.isPlainObject(values)) return(false); |
---|
670 | |
---|
671 | if(values.mode=='normal' || values.mode=='cumulative') properties.mode=values.mode; |
---|
672 | if(values.backgroundColor!=null) properties.backgroundColor=values.backgroundColor; |
---|
673 | if(values.borderColor!=null) properties.borderColor=values.borderColor; |
---|
674 | if(values.borderWidth!=null && values.borderWidth>=0) properties.borderWidth=values.borderWidth; |
---|
675 | |
---|
676 | if(values.width) properties.width=values.width; |
---|
677 | if(values.offset) properties.offset=values.offset; |
---|
678 | |
---|
679 | return(true); |
---|
680 | }; |
---|
681 | |
---|
682 | this.cumulativeInit = function (values) |
---|
683 | { |
---|
684 | if(!$.isArray(values)) return(properties.cumulativesValues); |
---|
685 | properties.cumulativesValues=[]; |
---|
686 | for(var i=0;i<values.length;i++) |
---|
687 | { |
---|
688 | properties.cumulativesValues.push(values[i]); |
---|
689 | } |
---|
690 | return(properties.cumulativesValues); |
---|
691 | }; |
---|
692 | this.cumulativeAdd = function (values) |
---|
693 | { |
---|
694 | if(values.length!=properties.cumulativesValues.length) return(properties.cumulativesValues); |
---|
695 | for(var i=0;i<properties.cumulativesValues.length;i++) |
---|
696 | { |
---|
697 | properties.cumulativesValues[i]+=values[i]; |
---|
698 | } |
---|
699 | return(properties.cumulativesValues); |
---|
700 | }; |
---|
701 | this.cumulativeGet = function () |
---|
702 | { |
---|
703 | return(properties.cumulativesValues); |
---|
704 | }; |
---|
705 | |
---|
706 | this.get = function () |
---|
707 | { |
---|
708 | return(properties); |
---|
709 | }; |
---|
710 | |
---|
711 | init(initProperties); |
---|
712 | } |
---|
713 | |
---|
714 | function CDSerieOptionsArea(initProperties) |
---|
715 | { |
---|
716 | var _this=this, |
---|
717 | properties={ |
---|
718 | type:'area', |
---|
719 | mode:'normal', // normal, cumulative |
---|
720 | backgroundColor:'#ccccff', |
---|
721 | borderColor:'#ffffff', |
---|
722 | borderWidth:1, |
---|
723 | cumulativesValues:[] |
---|
724 | }, |
---|
725 | |
---|
726 | init = function (values) |
---|
727 | { |
---|
728 | _this.set(values); |
---|
729 | }; |
---|
730 | |
---|
731 | this.set = function (values) |
---|
732 | { |
---|
733 | if(!$.isPlainObject(values)) return(false); |
---|
734 | |
---|
735 | if(values.mode=='normal' || values.mode=='cumulative') properties.mode=values.mode; |
---|
736 | if(values.backgroundColor!=null) properties.backgroundColor=values.backgroundColor; |
---|
737 | if(values.borderColor!=null) properties.borderColor=values.borderColor; |
---|
738 | if(values.borderWidth!=null && values.borderWidth>=0) properties.borderWidth=values.borderWidth; |
---|
739 | |
---|
740 | return(true); |
---|
741 | }; |
---|
742 | |
---|
743 | this.cumulativeInit = function (values) |
---|
744 | { |
---|
745 | if(!$.isArray(values)) return(properties.cumulativesValues); |
---|
746 | properties.cumulativesValues=values; |
---|
747 | return(properties.cumulativesValues); |
---|
748 | }; |
---|
749 | this.cumulativeAdd = function (values) |
---|
750 | { |
---|
751 | if(values.length!=properties.cumulativesValues.length) return(properties.cumulativesValues); |
---|
752 | for(var i=0;i<properties.cumulativesValues.length;i++) |
---|
753 | { |
---|
754 | properties.cumulativesValues[i]+=values[i]; |
---|
755 | } |
---|
756 | return(properties.cumulativesValues); |
---|
757 | }; |
---|
758 | this.cumulativeGet = function () |
---|
759 | { |
---|
760 | return(properties.cumulativesValues); |
---|
761 | }; |
---|
762 | |
---|
763 | this.get = function () |
---|
764 | { |
---|
765 | return(properties); |
---|
766 | }; |
---|
767 | |
---|
768 | init(initProperties); |
---|
769 | } |
---|
770 | |
---|
771 | function CDSerieOptionsLine(initProperties) |
---|
772 | { |
---|
773 | var _this=this, |
---|
774 | properties={ |
---|
775 | type:'line', |
---|
776 | color:'#ffff00', |
---|
777 | width:1, |
---|
778 | dot:'none', // none, circle, square, diamong |
---|
779 | dotSize:3 |
---|
780 | }, |
---|
781 | |
---|
782 | init = function (values) |
---|
783 | { |
---|
784 | _this.set(values); |
---|
785 | }; |
---|
786 | |
---|
787 | this.set = function (values) |
---|
788 | { |
---|
789 | if(!$.isPlainObject(values)) return(false); |
---|
790 | |
---|
791 | if(values.mode=='normal' || values.mode=='cumulative') properties.mode=values.mode; |
---|
792 | if(values.color!=null) properties.color=values.color; |
---|
793 | if(values.width!=null) properties.width=values.width; |
---|
794 | if(values.dot=='none' || |
---|
795 | values.dot=='square' || |
---|
796 | values.dot=='circle' || |
---|
797 | values.dot=='diamond' |
---|
798 | ) properties.dot=values.dot; |
---|
799 | |
---|
800 | if(values.dotSize!=null && values.dotSize>1) properties.dotSize=values.dotSize; |
---|
801 | |
---|
802 | return(true); |
---|
803 | } |
---|
804 | |
---|
805 | this.get = function () |
---|
806 | { |
---|
807 | return(properties); |
---|
808 | } |
---|
809 | |
---|
810 | init(initProperties); |
---|
811 | } |
---|
812 | |
---|
813 | function CDSerieOptionsPie(initProperties) |
---|
814 | { |
---|
815 | var _this=this, |
---|
816 | properties={ |
---|
817 | type:'pie', |
---|
818 | backgroundColors: |
---|
819 | ['#FF96A1', |
---|
820 | '#A796FF', |
---|
821 | '#96FF97', |
---|
822 | '#FFFE96', |
---|
823 | '#FF96DA', |
---|
824 | '#96C8FF', |
---|
825 | '#AEFF96', |
---|
826 | '#FF9896', |
---|
827 | '#C096FF', |
---|
828 | '#96FFFD', |
---|
829 | '#D4FF96'], |
---|
830 | borderColor:'#ffffff', |
---|
831 | borderWidth:2, |
---|
832 | outerRadius:50, |
---|
833 | innerRadius:0, |
---|
834 | offset:0 // exploded pie or not.. |
---|
835 | }, |
---|
836 | |
---|
837 | init = function (values) |
---|
838 | { |
---|
839 | _this.set(values); |
---|
840 | }; |
---|
841 | |
---|
842 | this.set = function (values) |
---|
843 | { |
---|
844 | var tmpIRadius=0, |
---|
845 | tmpORadius=0; |
---|
846 | |
---|
847 | if(!$.isPlainObject(values)) return(false); |
---|
848 | |
---|
849 | if(values.backgroundColor!=null && $.isArray(values.backgroundColors)) properties.backgroundColors=values.backgroundColors; |
---|
850 | if(values.borderColor!=null) properties.borderColor=values.borderColor; |
---|
851 | if(values.borderWidth!=null && values.borderWidth>=0) properties.borderWidth=values.borderWidth; |
---|
852 | |
---|
853 | tmpORadius=(values.outerRadius!=null)?values.outerRadius:properties.outerRadius; |
---|
854 | tmpIRadius=(values.innerRadius!=null)?values.innerRadius:properties.innerRadius; |
---|
855 | |
---|
856 | if(tmpORadius>0 && tmpORadius>tmpIRadius) properties.outerRadius=tmpORadius; |
---|
857 | if(tmpIRadius>=0 && tmpIRadius<tmpORadius) properties.innerRadius=tmpIRadius; |
---|
858 | if(values.offset!=null && values.offset>0) properties.offset=values.offset; |
---|
859 | |
---|
860 | return(true); |
---|
861 | } |
---|
862 | |
---|
863 | this.get = function () |
---|
864 | { |
---|
865 | return(properties); |
---|
866 | } |
---|
867 | |
---|
868 | init(initProperties); |
---|
869 | } |
---|
870 | |
---|
871 | |
---|
872 | |
---|
873 | |
---|
874 | /** |
---|
875 | * legend |
---|
876 | */ |
---|
877 | function CDLegend(values) |
---|
878 | { |
---|
879 | var _this=this, |
---|
880 | properties={ |
---|
881 | width:0, |
---|
882 | visible:true, |
---|
883 | position:'right', // left or right |
---|
884 | backgroundColor:'#101010', |
---|
885 | borderColor:'#ffffff', |
---|
886 | borderWidth:1, |
---|
887 | fontName:'arial', |
---|
888 | fontSize:8 |
---|
889 | }, |
---|
890 | data={ |
---|
891 | realWidth:0 |
---|
892 | }; |
---|
893 | |
---|
894 | init = function (values) |
---|
895 | { |
---|
896 | _this.set(values); |
---|
897 | }; |
---|
898 | |
---|
899 | this.get = function () |
---|
900 | { |
---|
901 | return(properties); |
---|
902 | }; |
---|
903 | |
---|
904 | this.set = function (value) |
---|
905 | { |
---|
906 | if(value instanceof CDLegend) |
---|
907 | { |
---|
908 | tmp=value.get(); |
---|
909 | properties.fontName=tmp.fontName; |
---|
910 | properties.fontSize=tmp.fontSize; |
---|
911 | properties.width=tmp.width; |
---|
912 | properties.visible=tmp.visible; |
---|
913 | properties.position=tmp.position; |
---|
914 | properties.backgroundColor=tmp.backgroundColor; |
---|
915 | properties.borderColor=tmp.borderColor; |
---|
916 | properties.borderWidth=tmp.borderWidth; |
---|
917 | |
---|
918 | data.realWidth=value.width; |
---|
919 | return(properties); |
---|
920 | } |
---|
921 | if($.isPlainObject(value)) |
---|
922 | { |
---|
923 | if(value.fontName!=null && value.fontName!='') properties.fontName=value.fontName; |
---|
924 | if(value.fontSize!=null && value.fontSize>0) properties.fontSize=value.fontSize; |
---|
925 | if(value.width!=null && value.width>0) |
---|
926 | { |
---|
927 | properties.width=value.width; |
---|
928 | data.realWidth=value.width; |
---|
929 | } |
---|
930 | if(value.visible!=null) properties.visible=value.visible; |
---|
931 | if(value.position!=null && (value.position=='left' || value.position=='right')) properties.position=value.position; |
---|
932 | if(value.backgroundColor!=null) properties.backgroundColor=value.backgroundColor; |
---|
933 | if(value.borderColor!=null) properties.borderColor=value.borderColor; |
---|
934 | if(value.borderWidth!=null && value.borderWidth>=0) properties.borderWidth=value.borderWidth; |
---|
935 | } |
---|
936 | return(properties); |
---|
937 | }; |
---|
938 | |
---|
939 | this.equals = function (value) |
---|
940 | { |
---|
941 | if(value instanceof CDLegend) |
---|
942 | { |
---|
943 | tmp=value.get(); |
---|
944 | if(properties.fontName==tmp.fontName && |
---|
945 | properties.fontSize==tmp.fontSize && |
---|
946 | properties.width==tmp.width && |
---|
947 | properties.visible==tmp.visible && |
---|
948 | properties.position==tmp.position && |
---|
949 | properties.backgroundColor==tmp.backgroundColor && |
---|
950 | properties.borderColor==tmp.borderColor && |
---|
951 | properties.borderWidth==tmp.borderWidth) return(true); |
---|
952 | return(false); |
---|
953 | } |
---|
954 | if(value.fontName!=null && |
---|
955 | value.fontSize!=null && |
---|
956 | value.width!=null && |
---|
957 | value.visible!=null && |
---|
958 | value.position!=null && |
---|
959 | value.backgroundColor!=null && |
---|
960 | value.borderColor!=null && |
---|
961 | value.borderWidth!=null && |
---|
962 | |
---|
963 | value.fontName==properties.fontName && |
---|
964 | value.fontSize==properties.fontSize && |
---|
965 | value.width==properties.width && |
---|
966 | value.visible==properties.visible && |
---|
967 | value.position==properties.position && |
---|
968 | value.backgroundColor==properties.backgroundColor && |
---|
969 | value.borderColor==properties.borderColor && |
---|
970 | value.borderWidth==properties.borderWidth |
---|
971 | ) |
---|
972 | return(true); |
---|
973 | return(false); |
---|
974 | }; |
---|
975 | |
---|
976 | this.getRealWidth = function () |
---|
977 | { |
---|
978 | return(data.realWidth); |
---|
979 | }; |
---|
980 | |
---|
981 | this.setRealWidth = function (value) |
---|
982 | { |
---|
983 | if(properties.width==0 && value>=0) data.realWidth=value; |
---|
984 | return(data.realWidth); |
---|
985 | }; |
---|
986 | |
---|
987 | |
---|
988 | init(values); |
---|
989 | } |
---|
990 | |
---|