-
Notifications
You must be signed in to change notification settings - Fork 32
/
flotr.js
1323 lines (1220 loc) · 37.5 KB
/
flotr.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Flotr = (function(){
var plotCnt = 0;
/**
* Function: (private) getSeries
*
* Collects dataseries from input and parses the series into the right format. It
* returns an Array of Objects each having at least the 'data' key set.
*
* Parameters:
* data - Object or array of dataseries
*
* Returns:
* Array of Objects parsed into the right format ({(...,) data: [[x1,y1], [x2,y2], ...] (, ...)})
*/
function getSeries(data){
return data.map(function(serie){
// uTorrent WebUI Patch - BEGIN
// return (serie.data) ? $extend(serie,{}) : {'data': serie};
return (serie.data) ? Object.append(serie,{}) : {'data': serie};
// uTorrent WebUI Patch - END
});
}
/**
* Function: (private) getTickSize
*
* Function calculates the ticksize and returns it.
*
* Parameters:
* noTicks - Number of ticks
* min - Lower bound integer value for the current axis.
* max - Upper bound integer value for the current axis.
* decimals - Number of decimals for the ticks.
*
* Returns:
* Returns the size of a tick.
*/
function getTickSize(noTicks, min, max, decimals){
var delta = (max - min) / noTicks;
var magn = getMagnitude(delta);
/**
* Norm is between 1.0 and 10.0.
*/
var norm = delta / magn;
var tickSize = 10;
if(norm < 1.5) tickSize = 1;
else if(norm < 2.25) tickSize = 2;
else if(norm < 3) tickSize = ((decimals == 0) ? 2 : 2.5);
else if(norm < 7.5) tickSize = 5;
return tickSize * magn;
}
/**
* Function: (private) defaultTickFormatter
*
* Formats the ticks.
*
* Parameters:
* val - Tick value integer.
*
* Returns:
* Formatted tick string.
*/
function defaultTickFormatter(val){
return val+'';
}
/**
* Function: (private) defaultTrackFormatter
*
* Formats the mouse tracker values.
*
* Parameters:
* val - Track value Object {x:..,y:..}.
*
* Returns:
* Formatted track string.
*/
function defaultTrackFormatter(obj){
return '('+obj.x+', '+obj.y+')';
}
/**
* Function: (private) getMagnitude
*
* Returns the magnitude of the input value.
*
* Parameters:
* x - Integer or float value
*
* Returns:
* Returns the magnitude of the input value.
*/
function getMagnitude(x){
return Math.pow(10, Math.floor(Math.log(x) / Math.LN10));
}
/**
* Function: (private) parseColor
*
* Parses a color string and returns a corresponding Color.
*
* Parameters:
* str - String that represents a color.
*/
function parseColor(str){
var result;
/**
* rgb(num,num,num)
*/
if((result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(str)))
return new Color(parseInt(result[1]), parseInt(result[2]), parseInt(result[3]));
/**
* rgba(num,num,num,num)
*/
if((result = /rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str)))
return new Color(parseInt(result[1]), parseInt(result[2]), parseInt(result[3]), parseFloat(result[4]));
/**
* rgb(num%,num%,num%)
*/
if((result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(str)))
return new Color(parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55);
/**
* rgba(num%,num%,num%,num)
*/
if((result = /rgba\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\s*\)/.exec(str)))
return new Color(parseFloat(result[1])*2.55, parseFloat(result[2])*2.55, parseFloat(result[3])*2.55, parseFloat(result[4]));
/**
* #a0b1c2
*/
if((result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(str)))
return new Color(parseInt(result[1],16), parseInt(result[2],16), parseInt(result[3],16));
/**
* #fff
*/
if((result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(str)))
return new Color(parseInt(result[1]+result[1],16), parseInt(result[2]+result[2],16), parseInt(result[3]+result[3],16));
/**
* Otherwise, we're most likely dealing with a named color.
*/
var name = str.trim().toLowerCase();
if(name == 'transparent'){
return new Color(255, 255, 255, 0);
}
result = lookupColors[name];
return new Color(result[0], result[1], result[2]);
}
/**
* Function: (private) extractColor
*
* Returns the background-color of the canvas container color string.
*
* Parameters:
* element - String that represents a color.
*
* Returns:
* Returns the background-color of the canvas container color string.
*/
function extractColor(element){
var color;
/**
* Loop until we find an element with a background color and stop when we hit the body element.
*/
do{
color = element.getStyle('background-color').toLowerCase();
if(!(color == '' || color == 'transparent')) break;
element = element.getParent();
}while(element.nodeName.toLowerCase() != 'body');
/**
* Catch Safari's way of signalling transparent
*/
return (color == 'rgba(0, 0, 0, 0)') ? 'transparent' : color;
}
/**
* Function: (private) Color
*
* Returns a Color object.
*
* Parameters:
* r - Red value.
* g - Green value.
* b - Blue value.
* a - Alpha value.
*
* Returns:
* void
*/
function Color(r, g, b, a){
var rgba = ['r','g','b','a'];
var x = 4;
while(-1<--x){
this[rgba[x]] = arguments[x] || ((x==3) ? 1.0 : 0);
}
this.toString = function(){
return (this.a >= 1.0) ? 'rgb('+[this.r,this.g,this.b].join(',')+')' : 'rgba('+[this.r,this.g,this.b,this.a].join(',')+')';
};
this.scale = function(rf, gf, bf, af) {
x = 4;
while(-1<--x){
if(arguments[x] != null)
this[rgba[x]] *= arguments[x];
}
return this.normalize();
};
this.adjust = function(rd, gd, bd, ad) {
x = 4;
while(-1<--x){
if(arguments[x] != null)
this[rgba[x]] += arguments[x];
}
return this.normalize();
};
this.clone = function(){
return new Color(this.r, this.b, this.g, this.a);
};
var limit = function(val,minVal,maxVal){
return Math.max(Math.min(val, maxVal), minVal);
};
this.normalize = function(){
this.r = limit(parseInt(this.r), 0, 255);
this.g = limit(parseInt(this.g), 0, 255);
this.b = limit(parseInt(this.b), 0, 255);
this.a = limit(this.a, 0, 1);
return this;
};
this.normalize();
}
var lookupColors = {
aqua:[0,255,255],
azure:[240,255,255],
beige:[245,245,220],
black:[0,0,0],
blue:[0,0,255],
brown:[165,42,42],
cyan:[0,255,255],
darkblue:[0,0,139],
darkcyan:[0,139,139],
darkgrey:[169,169,169],
darkgreen:[0,100,0],
darkkhaki:[189,183,107],
darkmagenta:[139,0,139],
darkolivegreen:[85,107,47],
darkorange:[255,140,0],
darkorchid:[153,50,204],
darkred:[139,0,0],
darksalmon:[233,150,122],
darkviolet:[148,0,211],
fuchsia:[255,0,255],
gold:[255,215,0],
green:[0,128,0],
indigo:[75,0,130],
khaki:[240,230,140],
lightblue:[173,216,230],
lightcyan:[224,255,255],
lightgreen:[144,238,144],
lightgrey:[211,211,211],
lightpink:[255,182,193],
lightyellow:[255,255,224],
lime:[0,255,0],
magenta:[255,0,255],
maroon:[128,0,0],
navy:[0,0,128],
olive:[128,128,0],
orange:[255,165,0],
pink:[255,192,203],
purple:[128,0,128],
violet:[128,0,128],
red:[255,0,0],
silver:[192,192,192],
white:[255,255,255],
yellow:[255,255,0]
};
function Plot(container, data, opts) {
/**
* Initialize variables.
*/
var options, canvas, overlay, ctx, octx;
var target = container;
this.id = id = 'flotr-' + plotCnt++;
this.series = series = getSeries(data);
this.xaxis = xaxis = {};
this.yaxis = yaxis = {};
this.plotOffset = plotOffset = {left: 0, right: 0, top: 0, bottom: 0};
var labelMaxWidth = 0;
var labelMaxHeight = 0;
var canvasWidth = 0;
var canvasHeight = 0;
var plotWidth = 0;
var plotHeight = 0;
var hozScale = 0;
var vertScale = 0;
this.options = setOptions(opts);
this.repaint = repaint;
/**
* Function: (private) setOptions
*
* Merges user-defined and default options. Also generates colors for series for which
* the user didn't specify a color, and merge user-defined series options with default options.
*
* Parameters:
* none
*
* Returns:
* void
*/
function setOptions(o){
// uTorrent WebUI Patch - BEGIN
// options = $merge({
options = Object.merge({
// uTorrent WebUI Patch - END
colors: ['#00A8F0', '#C0D800', '#cb4b4b', '#4da74d', '#9440ed'], //=> The default colorscheme. When there are > 5 series, additional colors are generated.
legend: {
show: true, // => setting to true will show the legend, hide otherwise
noColumns: 1, // => number of colums in legend table
labelFormatter: null, // => fn: string -> string
labelBoxBorderColor: '#ccc', // => border color for the little label boxes
container: null, // => container (as jQuery object) to put legend in, null means default on top of graph
position: 'ne', // => position of default legend container within plot
margin: 5, // => distance from grid edge to default legend container within plot
backgroundColor: null, // => null means auto-detect
backgroundOpacity: 0.85 // => set to 0 to avoid background, set to 1 for a solid background
},
xaxis: {
ticks: null, // => format: either [1, 3] or [[1, 'a'], 3]
noTicks: 5, // => number of ticks for automagically generated ticks
tickFormatter: defaultTickFormatter, // => fn: number -> string
tickDecimals: null, // => no. of decimals, null means auto
min: null, // => min. value to show, null means set automatically
max: null, // => max. value to show, null means set automatically
autoscaleMargin: 0 // => margin in % to add if auto-setting min/max
},
yaxis: {
ticks: null, // => format: either [1, 3] or [[1, 'a'], 3]
noTicks: 5, // => number of ticks for automagically generated ticks
tickFormatter: defaultTickFormatter, // => fn: number -> string
tickDecimals: null, // => no. of decimals, null means auto
min: null, // => min. value to show, null means set automatically
max: null, // => max. value to show, null means set automatically
autoscaleMargin: 0 // => margin in % to add if auto-setting min/max
},
points: {
show: false, // => setting to true will show points, false will hide
radius: 3, // => point radius (pixels)
lineWidth: 2, // => line width in pixels
fill: true, // => true to fill the points with a color, false for (transparent) no fill
fillColor: '#ffffff', // => fill color
fillOpacity: 0.4
},
lines: {
show: false, // => setting to true will show lines, false will hide
lineWidth: 2, // => line width in pixels
fill: false, // => true to fill the area from the line to the x axis, false for (transparent) no fill
fillColor: null, // => fill color
fillOpacity: 0.4 // => opacity of the fill color, set to 1 for a solid fill, 0 hides the fill
},
bars: {
show: false, // => setting to true will show bars, false will hide
lineWidth: 2, // => in pixels
barWidth: 1, // => in units of the x axis
fill: true, // => true to fill the area from the line to the x axis, false for (transparent) no fill
fillColor: null, // => fill color
fillOpacity: 0.4, // => opacity of the fill color, set to 1 for a solid fill, 0 hides the fill
horizontal: false
},
grid: {
color: '#545454', // => primary color used for outline and labels
backgroundColor: null, // => null for transparent, else color
tickColor: '#dddddd', // => color used for the ticks
labelMargin: 3, // => margin in pixels
verticalLines: true, // => whether to show gridlines in vertical direction
horizontalLines: true, // => whether to show gridlines in horizontal direction
outlineWidth: 2 // => width of the grid outline/border in pixels
},
selection: {
mode: null, // => one of null, 'x', 'y' or 'xy'
color: '#B6D9FF', // => selection box color
fps: 10 // => frames-per-second
},
mouse: {
track: null, // => true to track the mouse, no tracking otherwise
position: 'se', // => position of the value box (default south-east)
trackFormatter: defaultTrackFormatter, // => formats the values in the value box
margin: 3, // => margin in pixels of the valuebox
lineColor: '#ff3f19', // => line color of points that are drawn when mouse comes near a value of a series
trackDecimals: 1, // => decimals for the track values
sensibility: 2, // => the lower this number, the more precise you have to aim to show a value
radius: 3 // => radius of the tracck point
},
shadowSize: 4 // => size of the 'fake' shadow
}, o);
/**
* Collect colors assigned by the user to a serie.
*/
var neededColors = series.length, usedColors = [], assignedColors = [];
for(var i = series.length - 1; i > -1; --i){
var sc = series[i].color;
if(sc != null){
--neededColors;
// uTorrent WebUI Patch - BEGIN
// if($type(sc) == 'number') assignedColors.push(sc);
if(typeOf(sc) == 'number') assignedColors.push(sc);
// uTorrent WebUI Patch - END
else usedColors.push(parseColor(series[i].color));
}
}
/**
* Calculate the number of colors that need to be generated.
*/
for(var j = assignedColors.length - 1; j > -1; --j)
neededColors = Math.max(neededColors, assignedColors[j] + 1);
/**
* Generate colors.
*/
var colors = [], variation = 0, k = 0;
while(colors.length < neededColors){
var c = (options.colors.length == k) ? new Color(100, 100, 100) : parseColor(options.colors[k]);
/**
* Make sure each serie gets a different color.
*/
var sign = variation % 2 == 1 ? -1 : 1;
var factor = 1 + sign * Math.ceil(variation / 2) * 0.2;
c.scale(factor, factor, factor);
/**
* @todo if we're getting to close to something else, we should probably skip this one
*/
colors.push(c);
if(++k >= options.colors.length){
k = 0;
++variation;
}
}
/**
* Fill the options with the generated colors.
*/
for(var m = 0, ln = series.length, n = 0, s; m < ln; ++m){
s = series[m];
/**
* Assign the color.
*/
if(s.color == null){
s.color = colors[n++].toString();
// uTorrent WebUI Patch - BEGIN
// }else if($type(s.color) == 'number'){
}else if(typeOf(s.color) == 'number'){
// uTorrent WebUI Patch - END
s.color = colors[s.color].toString();
}
// uTorrent WebUI Patch - BEGIN
// s.lines = $extend($extend({}, options.lines), s.lines||{});
// s.points = $extend($extend({}, options.points), s.points||{});
// s.bars = $extend($extend({}, options.bars), s.bars||{});
// s.mouse = $extend($extend({}, options.mouse), s.mouse||{});
s.lines = Object.append(Object.append({}, options.lines), s.lines||{});
s.points = Object.append(Object.append({}, options.points), s.points||{});
s.bars = Object.append(Object.append({}, options.bars), s.bars||{});
s.mouse = Object.append(Object.append({}, options.mouse), s.mouse||{});
// uTorrent WebUI Patch - END
if(s.shadowSize == null) s.shadowSize = options.shadowSize;
}
return options;
}
function repaint() {
constructCanvas();
//bindEvents();
findDataRanges();
calculateRange(xaxis, options.xaxis);
extendXRangeIfNeededByBar();
calculateRange(yaxis, options.yaxis);
extendYRangeIfNeededByBar();
calculateTicks(xaxis, options.xaxis);
calculateTicks(yaxis, options.yaxis);
calculateSpacing();
draw();
insertLegend();
}
/**
* Function: (private) constructCanvas
*
* Initializes the canvas and it's overlay canvas. When the browser is IE, we make use of excanvas.
* The overlay canvas is inserted for displaying interaction.
*
* Parameters:
* none
*
* Returns:
* void
*/
function constructCanvas() {
var size = target.getSize();
canvasWidth = size.x;
canvasHeight = size.y;
//target.innerHTML = '';
/**
* For positioning labels and overlay.
*/
target.setStyles({
"position": "relative",
"cursor": "default"
});
if (canvasWidth <= 0 || canvasHeight <= 0) {
throw 'Invalid dimensions for plot, width = ' + canvasWidth + ', height = ' + canvasHeight;
}
/**
* Insert main canvas.
*/
if (canvas == null) {
// uTorrent WebUI Patch - BEGIN
// canvas = $(document.createElement('canvas')).setProperties({
// "width": canvasWidth,
// "height": canvasHeight,
// "id": id
// });
canvas = $(document.createElement('canvas')).setProperty("id", id);
// uTorrent WebUI Patch - END
target.appendChild(canvas);
// uTorrent WebUI Patch - BEGIN
// if (Browser.Engine.trident && !Browser.Engine.trident6) {
if (window.G_vmlCanvasManager) {
// uTorrent WebUI Patch - END
canvas = $(window.G_vmlCanvasManager.initElement(canvas));
}
ctx = canvas.getContext('2d');
}
canvas.setProperties({
width: canvasWidth,
height: canvasHeight
});
/**
* Insert overlay canvas for interactive features.
*/
if (overlay == null) {
// uTorrent WebUI Patch - BEGIN
// overlay = $(document.createElement('canvas')).setProperties({
// width: canvasWidth,
// height: canvasHeight
// }).setStyles({
overlay = $(document.createElement('canvas')).setStyles({
// uTorrent WebUI Patch - END
position: 'absolute',
left: '0px',
top: '0px'
});
// uTorrent WebUI Patch - BEGIN
// overlay.injectAfter(canvas);
// if (Browser.Engine.trident && !Browser.Engine.trident6) {
overlay.inject(canvas, 'after');
if (window.G_vmlCanvasManager) {
// uTorrent WebUI Patch - END
overlay = $(window.G_vmlCanvasManager.initElement(overlay));
}
octx = overlay.getContext('2d');
}
overlay.setProperties({
width: canvasWidth,
height: canvasHeight
});
}
/**
* Function: (private) bindEvents
*
*
*
* Parameters:
* none
*
* Returns:
* void
*/
function bindEvents() {
if (options.selection.mode != null)
overlay.addEvent('mousedown', mouseDownHandler);
overlay.addEvent('mousemove', mouseMoveHandler)
overlay.addEvent('click', clickHandler)
}
/**
* Function: (private) findDataRanges
*
* Function determines the min and max values for the xaxis and yaxis.
*
* Parameters:
* none
*
* Returns:
* void
*/
function findDataRanges(){
yaxis.datamin = xaxis.datamin = 0;
xaxis.datamax = yaxis.datamax = 1;
if (series.length == 0) return;
/**
* Get datamin, datamax start values
*/
var found = false;
for (var i = 0; i < series.length; ++i) {
if (series[i].data.length > 0) {
xaxis.datamin = xaxis.datamax = series[i].data[0][0];
yaxis.datamin = yaxis.datamax = series[i].data[0][1];
found = true;
break;
}
}
/**
* Return because series are empty.
*/
if (!found) return;
/**
* then find real datamin, datamax
*/
for (var j = series.length - 1; j > -1; --j) {
var data = series[j].data;
for (var h = data.length - 1; h > -1; --h) {
var x = data[h][0];
var y = data[h][1];
if(x < xaxis.datamin) xaxis.datamin = x;
else if(x > xaxis.datamax) xaxis.datamax = x;
if(y < yaxis.datamin) yaxis.datamin = y;
else if(y > yaxis.datamax) yaxis.datamax = y;
}
}
}
/**
* Function: (private) calculateRange
*
*
* Parameters:
* none
*
* Returns:
* void
*/
function calculateRange(axis, axisOptions) {
var min = axisOptions.min != null ? axisOptions.min : axis.datamin;
// uTorrent WebUI Patch - BEGIN
// var max = axisOptions.max != null ? axisOptions.max : axis.datamax;
var max = axisOptions.max != null ? axisOptions.max : Math.max(min + (axisOptions.minMaxTickSize || 0), axis.datamax);
// uTorrent WebUI Patch - END
if(max - min == 0.0){
var widen = (max == 0.0) ? 1.0 : 0.01;
min -= widen;
max += widen;
}
axis.tickSize = getTickSize(axisOptions.noTicks, min, max, axisOptions.tickDecimals);
/**
* Autoscaling.
*/
var margin;
if (axisOptions.min == null) {
/**
* Add a margin.
*/
margin = axisOptions.autoscaleMargin;
if (margin != 0) {
min -= axis.tickSize * margin;
/**
* Make sure we don't go below zero if all values are positive.
*/
if (min < 0 && axis.datamin >= 0)
min = 0;
min = axis.tickSize * Math.floor(min / axis.tickSize);
}
}
if (axisOptions.max == null) {
margin = axisOptions.autoscaleMargin;
if (margin != 0) {
max += axis.tickSize * margin;
if (max > 0 && axis.datamax <= 0)
max = 0;
max = axis.tickSize * Math.ceil(max / axis.tickSize);
}
}
axis.min = min;
axis.max = max;
}
/**
* Function: (private) extendXRangeIfNeededByBar
*
* Bar series autoscaling in x direction.
*
* Parameters:
* none
*
* Returns:
* void
*/
function extendXRangeIfNeededByBar() {
if (options.xaxis.max == null) {
/**
* Autoscaling.
*/
var newmax = xaxis.max;
for (var i = series.length - 1; i > -1; --i) {
if (series[i].bars.show&& !series[i].bars.horizontal && series[i].bars.barWidth + xaxis.datamax > newmax) {
newmax = xaxis.max + series[i].bars.barWidth;
}
}
xaxis.max = newmax;
}
}
/**
* Function: (private) extendYRangeIfNeededByBar
*
* Bar series autoscaling in y direction.
*
* Parameters:
* none
*
* Returns:
* void
*/
function extendYRangeIfNeededByBar(){
if(options.yaxis.max == null){
/**
* Autoscaling.
*/
var newmax = yaxis.max;
for(var i = series.length - 1; i > -1; --i){
if(series[i].bars.show && series[i].bars.horizontal && series[i].bars.barWidth + yaxis.datamax > newmax){
newmax = yaxis.max + series[i].bars.barWidth;
}
}
yaxis.max = newmax;
}
}
/**
* Function: (private) calculateTicks
*
*
*
* Parameters:
* none
*
* Returns:
* void
*/
function calculateTicks(axis, axisOptions) {
axis.ticks = [];
if (axisOptions.ticks) {
var ticks = axisOptions.ticks;
// uTorrent WebUI Patch - BEGIN
// if ($type(ticks) == 'function') {
if (typeof(ticks) == 'function') {
// uTorrent WebUI Patch - END
ticks = ticks({ min: axis.min, max: axis.max });
}
/**
* Clean up the user-supplied ticks, copy them over.
*/
for (var i = 0, v, label; i < ticks.length; ++i) {
var t = ticks[i];
if (typeof(t) == 'object') {
v = t[0];
label = (t.length > 1) ? t[1] : axisOptions.tickFormatter(v);
}else{
v = t;
label = axisOptions.tickFormatter(v);
}
axis.ticks[i] = { v: v, label: label };
}
} else {
/**
* Round to nearest multiple of tick size.
*/
var start = axis.tickSize * Math.ceil(axis.min / axis.tickSize);
/**
* Then spew out all possible ticks.
*/
for (i = 0; start + i * axis.tickSize <= axis.max; ++i) {
v = start + i * axis.tickSize;
/**
* Round (this is always needed to fix numerical instability).
*/
var decimals = axisOptions.tickDecimals;
if(decimals == null) decimals = 1 - Math.floor(Math.log(axis.tickSize) / Math.LN10);
if(decimals < 0) decimals = 0;
v = v.toFixed(decimals);
axis.ticks.push({ v: v, label: axisOptions.tickFormatter(v) });
}
}
}
/**
* Function: (private) calculateSpacing
*
* Calculates axis label sizes.
*
* Parameters:
* none
*
* Returns:
* void
*/
function calculateSpacing() {
var max_label = '';
for (var i = 0; i < yaxis.ticks.length; ++i) {
var l = yaxis.ticks[i].label.length;
if(l > max_label.length){
max_label = yaxis.ticks[i].label;
}
}
var dummyDiv = new Element('div', {
"styles": {
'position': 'absolute',
'top': '-10000px',
'font-size': 'small'
},
"html": max_label,
"class": "gridLabel"
// uTorrent WebUI Patch - BEGIN
// }).injectInside(target);
}).inject(target);
// uTorrent WebUI Patch - END
var size = dummyDiv.getSize();
labelMaxWidth = size.x;
labelMaxHeight = size.y;
dummyDiv.destroy();
/**
* Grid outline line width.
*/
var maxOutset = 2;
if (options.points.show) {
maxOutset = Math.max(maxOutset, options.points.radius + options.points.lineWidth/2);
}
for (var j = 0; j < series.length; ++j) {
if (series[j].points.show) {
maxOutset = Math.max(maxOutset, series[j].points.radius + series[j].points.lineWidth/2);
}
}
plotOffset.left = plotOffset.right = plotOffset.top = plotOffset.bottom = maxOutset;
plotOffset.left += labelMaxWidth + options.grid.labelMargin;
plotOffset.bottom += labelMaxHeight + options.grid.labelMargin;
plotWidth = canvasWidth - plotOffset.left - plotOffset.right;
plotHeight = canvasHeight - plotOffset.bottom - plotOffset.top;
hozScale = plotWidth / (xaxis.max - xaxis.min);
vertScale = plotHeight / (yaxis.max - yaxis.min);
}
/**
* Function: (private) draw
*
* Draws grid, labels and series.
*
* Parameters:
* none
*
* Returns:
* void
*/
function draw() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
drawGrid();
drawLabels();
if (series.length) {
for (var i = 0, j = series.length; i < j; i++)
drawSeries(series[i]);
}
}
/**
* Function: (private) tHoz
*
* Translates absolute horizontal x coordinates to relative coordinates.
*
* Parameters:
* x - Absolute integer x coordinate.
*
* Returns:
* Translated relative x coordinate.
*/
function tHoz(x) {
return (x - xaxis.min) * hozScale;
}
/**
* Function: (private) tVert
*
* Translates absolute vertical x coordinates to relative coordinates.
*
* Parameters:
* y - Absolute integer y coordinate.
*
* Returns:
* Translated relative y coordinate.
*/
function tVert(y) {
return plotHeight - (y - yaxis.min) * vertScale;
}
/**
* Function: (private) drawGrid
*
* Draws a grid for the graph
*
* Parameters:
* none
*
* Returns:
* void
*/
function drawGrid() {
ctx.save();
ctx.translate(plotOffset.left, plotOffset.top);
/**
* Draw grid background, when defined.
*/
if (options.grid.backgroundColor != null) {
ctx.fillStyle = options.grid.backgroundColor;
ctx.fillRect(0, 0, plotWidth, plotHeight);
}
/**
* Draw grid lines in vertical direction.
*/
ctx.lineWidth = 1;
ctx.strokeStyle = options.grid.tickColor;
ctx.beginPath();
if (options.grid.verticalLines) {
for (var i = 0, v = null; i < xaxis.ticks.length; ++i) {
v = xaxis.ticks[i].v;
/**
* Don't show lines on upper and lower bounds.
*/
if ((v == xaxis.min || v == xaxis.max) && options.grid.outlineWidth != 0) continue;
ctx.moveTo(Math.floor(tHoz(v)) + ctx.lineWidth/2, 0);
ctx.lineTo(Math.floor(tHoz(v)) + ctx.lineWidth/2, plotHeight);
}
}
/**
* Draw grid lines in horizontal direction.
*/
if (options.grid.horizontalLines) {
for(var j = 0, v = null; j < yaxis.ticks.length; ++j){
v = yaxis.ticks[j].v;
/**
* Don't show lines on upper and lower bounds.
*/
if ((v == yaxis.min || v == yaxis.max) && options.grid.outlineWidth != 0) continue;
ctx.moveTo(0, Math.floor(tVert(v)) + ctx.lineWidth/2);
ctx.lineTo(plotWidth, Math.floor(tVert(v)) + ctx.lineWidth/2);
}
}
ctx.stroke();
/**
* Draw axis/grid border.
*/
if (options.grid.outlineWidth != 0) {
ctx.lineWidth = options.grid.outlineWidth;
ctx.strokeStyle = options.grid.color;
ctx.lineJoin = 'round';
ctx.strokeRect(0, 0, plotWidth, plotHeight);
}