-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgraphy.js
2523 lines (2186 loc) · 85.6 KB
/
graphy.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
// Copyright 2010-2012 DECK Monitoring LLC.
//
// This file is part of Graphy from DECK Monitoring LLC.
//
// Graphy is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// Graphy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
//
// You should have received a copy of the Lesser General Public License along with Graphy. If not, see
// <http://www.gnu.org/licenses/>.
//
var Graphy = {
version: "1.2012.12.18",
graphs: [],
cssIsReady: false,
//
// Creates a graph on a canvas element according to a "spec" hash. The hash may contain the following keys:
//
// canvas [required]: Selector for a canvas element or any other html element (which will be transformed
// into a canvas). If this contains text, Graphy will assign this text to plot_json in
// the spec.
// plot: Calls self.plot with value as parameter.
// plots: Calls self.plots with value as parameter.
// plot_json: Calls self.plot_json with value as parameter.
// options: Sets the default style information for plots. Plots may override this. These are renderer
// specific. Example: {"width": 2, "color": "red", "unit": "oxen/m³"}
// xAxisInterval: Specify a fixed interval. See Graphy.interval.
// xAxisLabelFormatter: Function or string name of function to format label text. See Graphy.formatters.
// yAxisLabelFormatter: Function or string name of function to format label text. See Graphy.formatters.
// xAxisRenderer: Function or string name of function to render x axis. See Graphy.renderers.axis.
// (default: "cleanX")
// yAxisRenderer: Function or string name of function to render y axis. See Graphy.renderers.axis.
// (default: "cleanY")
// drawVRule: Boolean to true vertical rules or not if necessary
// _hoverLabelRenderer: Function or string name of function to format the hover label.
//
//
createGraph: function(spec) {
var self = {};
// see: plot, plots, and plot_jsons for accessors
var _plots = [];
// have read/write accessors
var _canvas,
_yAxisRenderer,
_xAxisRenderer,
_yAxisLabelFormatter,
_xAxisLabelFormatter,
_xAxisInterval,
_vRuleLabel = false,
_drawVRule = true,
_hoverLabelRenderer,
_noHover,
_options = {};
// have read-only accessors
var _$canvas,
_ctx,
_index = Graphy.graphs.push(self) - 1,
_valueRect = false,
_graphRect = false,
_xUnits = [],
_yUnits = [],
_valueRectByUnit = {},
selectedPlotCount = 0;
// private vars
var _$hoverLine,
_hoverLine,
_hoverLineCtx;
//
// gets the canvas and context and calls any spec'd functions
//
var init = function(spec) {
if ( spec.options ) { self.options(spec.options); }
if ( spec.canvas ) { self.canvas(spec.canvas); }
self.noHover(true);
if ( spec.xAxisInterval ) { self.xAxisInterval(spec.xAxisInterval); }
if ( spec.xAxisLabelFormatter ) { self.xAxisLabelFormatter(spec.xAxisLabelFormatter); }
if ( spec.yAxisLabelFormatter ) { self.yAxisLabelFormatter(spec.yAxisLabelFormatter); }
if ( spec.xAxisRenderer ) { self.xAxisRenderer(spec.xAxisRenderer); }
if ( spec.yAxisRenderer ) { self.yAxisRenderer(spec.yAxisRenderer); }
self.hoverLabelRenderer(spec.hoverLabelRenderer);
if ( spec.vRuleLabel !== undefined) { self.vRuleLabel(spec.vRuleLabel); }
if ( spec.drawVRule !== undefined) { self.drawVRule(spec.drawVRule); }
if ( spec['plot'] ) { self.plot(spec['plot']); }
if ( spec['plots'] ) { self.plots(spec['plots']); }
if ( spec['plot_json'] ) { self.plot_json(spec['plot_json']); }
if ( _plots.length ) { self.draw(); }
}
//
// Removes all Highlighted Points from all _plots
//
self.removeHighlightPoints = function(){
if(_hoverLine) {
// cursor is outside of the graph area, so remove the hover line
_$hoverLine.remove();
_$hoverLine = _hoverLine = _hoverLineCtx = null;
}
$.each(_plots, function() {
if(this.removeHighlightPointIfExists) this.removeHighlightPointIfExists();
});
}
//
// draws it up if the css is all ready. if not, it waits until it can see it.
//
self.draw = function () {
if ( Graphy.cssIsReady ) {
drawForReal();
} else {
var $test = $("<div class='graphy_css_loaded' style='display:hidden;'></div>");
$('body').append($test);
var interval = setInterval( function() {
if( $test.css('position') == 'absolute' ) {
clearInterval(interval);
Graphy.cssIsReady = true;
drawForReal();
}
}, 10 );
}
};
//
// Position/conversion helpers
//
self.fromDocumentPixelToGraphRectPixel = function(documentPixel) {
var o = _$canvas.offset();
return {
x: documentPixel.x - o.left,
y: documentPixel.y - o.top
};
}
self.fromGraphRectPixelToDocumentPixel = function(graphRectPixel) {
var o = _$canvas.offset();
return {
x: graphRectPixel.x + o.left,
y: graphRectPixel.y + o.top
};
}
var drawForReal = function() {
if ( !_ctx ) { return; }
_ctx.save();
_ctx.clearRect(0, 0, _canvas.width, _canvas.height);
$(".graphy_axis_label_" + _index).remove();
if (_plots.length > 0) {
// draw the axis (order is important here)
if ( !_yAxisRenderer ) { _yAxisRenderer = Graphy.renderers.axis.cleanY; }
if ( !_xAxisRenderer ) { _xAxisRenderer = Graphy.renderers.axis.cleanX; }
_yAxisRenderer( "measure", self );
_xAxisRenderer( "measure", self );
_yAxisRenderer( "draw", self );
_xAxisRenderer( "draw", self );
// render the plots
var options, renderer, rendererIndexes = {},
indexByPlot = {};
$.map(_plots, function(el,i) { indexByPlot[el] = i; });
_plots = _plots.sort(function(a,b){
// both bars or neither bars
if((a.options.renderer == 'bar') == (b.options.renderer == 'bar'))
return indexByPlot[a] - indexByPlot[b];
else if(a.options.renderer == 'bar')
return -1;
else if(b.options.renderer == 'bar')
return 1;
});
for (var i = 0; i < _plots.length; i++) {
options = $.extend({}, _options, _plots[i].options);
renderer = Graphy.util.functionByNameOrFunction(options.renderer, Graphy.renderers, Graphy.renderers.plot);
if (rendererIndexes[renderer] === undefined) rendererIndexes[renderer] = 0;
rendererIndexes[renderer] += 1;
renderer(rendererIndexes[renderer], _plots[i].data, options, self);
}
}
_ctx.restore();
}
//
// accessor for canvas
//
// param set_canvas is a jQuery selector
// returns the canvas (html) object
//
self.canvas = function( set_canvas ) {
if ( set_canvas ) {
var $target = $( set_canvas ).first();
_$canvas = $target;
if(!$target.is('canvas')){
_$canvas = $target.createCanvas().find('canvas');
} else {
// For non-dynamically created canvas elements, we need to
// do some magic for IE 7 & 8 to make them usable with excanvas.
if (typeof(G_vmlCanvasManager) != 'undefined'){
G_vmlCanvasManager.initElement(_$canvas.get(0));
}
}
_canvas = _$canvas.get(0);
_$canvas.addClass("graphy_canvas");
_$canvas.data( { graph: self } );
// get the canvas context and set the graphRect initial size
if (!_canvas.getContext) { console.warn("No canvas context found."); return; }
_ctx = _canvas.getContext('2d');
_graphRect = Graphy.util.createRect();
_graphRect.right = _canvas.width - 2;
_graphRect.bottom = _canvas.height;
// each graph needs to watch mouse movement to check if cursor enters or leaves graph area
// mouseover and mouseout do not work because the hover line triggers these events continually since the mouse is over that, not the graph area
// TODO: put this code somewhere else, it should not be inlined here
$(document).on('mousemove', function(e) {
if(self.noHover()) {
return;
}
var o = _$canvas.offset();
// check if cursor is inside graph area
if(e.pageX > (o.left + _graphRect.left) && e.pageX < (o.left + _graphRect.right) && e.pageY > (o.top + _graphRect.top) && e.pageY < (o.top + _$canvas.height())) {
// draw the hoverline if it does not exist
if(!_hoverLine) {
_$hoverLine = $.createCanvas(2, _graphRect.bottom).css('position', 'absolute').css('z-index', 1500),
_$hoverLine.prependTo('body').addClass('graphy');
_hoverLine = _$hoverLine.get(0);
_hoverLineCtx = _hoverLine.getContext('2d');
// draw the base hover line
_hoverLineCtx.lineWidth = 2;
_hoverLineCtx.moveTo(0, 0);
_hoverLineCtx.lineTo(0, _$hoverLine.height());
_hoverLineCtx.strokeStyle = '#aaa';
_hoverLineCtx.stroke();
// set position of top - this will not change since the bar moves horizontally
_$hoverLine.offset({ top: _$canvas.offset().top });
}
// reposition on horizontal axis to line up with cursor
_$hoverLine.offset({ left: e.pageX });
var toHighlight = []; // array of objects with a plot and point property
$.each(_plots, function() {
// Allows you to turn hover on and off for specific lines
if(this.options.hover === false) {
return;
}
var plotPoint = this.fromDocumentPixelToPlotPoint({x: e.pageX, y:e.pageY});
// values are in-order along x-axis in this.data, so find closest point to cursor with simple binary search
var i, dataX, low = 0,
high = this.data.length-1;
while(low!=high) {
i = low+Math.floor((high-low)/2);
if(this.data && this.data[i]){
dataX = this.data[i][0];
}
if(plotPoint.x < dataX) high = i;
else if(plotPoint.x > dataX) {
// break if point is closer to the value we're throwing out than the value that will be the new low
if(plotPoint.x - dataX < this.data[i+1][0] - plotPoint.x) break;
low = ++i;
}
else break; // found exact match.
}
if(this.data.length == 1) i = 0;
var nearestPoint = [0,0];
if(this.data && this.data[i]){
nearestPoint = this.data[i];
}
var pixel = this.fromPlotPointToDocumentPixel({x:nearestPoint[0],y:nearestPoint[1]});
var closeEnoughToDraw = Math.abs(pixel.x - e.pageX) < 15;
if(closeEnoughToDraw && this.options.renderer != 'bar' && (!this.graphy.hasSelectedItems() || this.options.selected)) {
toHighlight.push({plot: this, point: nearestPoint, pixel: pixel});
} else {
this.removeHighlightPointIfExists();
}
});
// sort toHighlight by awesomeness
toHighlight = toHighlight.sort(function(a,b) { return a.pixel.y - b.pixel.y; });
var groups = [],
labelHeight = 21,
groupsToMove = 0;
for(var i=0,len=toHighlight.length; i < len; i++) {
// TODO: remove this nonsense
var top = 1500 + i * labelHeight,
bottom = 1500 + (i+1) * labelHeight,
desiredPosition = toHighlight[i].plot.fromPlotPointToDocumentPixel({x:toHighlight[i].point[0], y:toHighlight[i].point[1]}).y;
averageMomentum = desiredPosition - (bottom+top)/2;
if(averageMomentum != 0) groupsToMove++;
groups.push({
top: top,
bottom: bottom,
averageMomentum: averageMomentum,
desiredPosition: desiredPosition,
items: [$.extend(toHighlight[i], { desiredPosition: desiredPosition })],
moveBy: function(amount) {
this.top += amount;
this.bottom += amount;
},
reachEquilibrium: function() {
this.moveBy(this.averageMomentum);
this.averageMomentum = 0;
},
moveToAndAbsorb: function(groupToAbsorb) {
if(groupToAbsorb.bottom < this.top) { // we are below, move up
this.moveBy( groupToAbsorb.bottom - this.top);
// append our list of items to what we are absorbing
this.items = groupToAbsorb.items.concat(this.items);
this.top = groupToAbsorb.top;
this.recalculateMomentum();
} else { // we are above, move down
this.moveBy( groupToAbsorb.top - this.bottom );
// prepend our list of items to what we are absorbing
this.items = this.items.concat(groupToAbsorb.items);
this.bottom = groupToAbsorb.bottom;
this.recalculateMomentum();
}
},
recalculateMomentum: function() {
var momentumSum = 0;
for(var i=0,len=this.items.length; i < len; i++) {
momentumSum += this.items[i].desiredPosition - (this.top + i*labelHeight + labelHeight/2);
}
this.averageMomentum = momentumSum / this.items.length;
}
});
}
var i, len = groups.length, absorbThis;
while( groupsToMove > 0 ) {
for(i=0; i<len && groupsToMove > 0; i++) {
if(groups[i].averageMomentum < 0) {
// check above
if(i > 0 && groups[i-1].bottom > groups[i].top + groups[i].averageMomentum) {
absorbThis = groups.splice(--i, 1)[0];
groups[i].moveToAndAbsorb(absorbThis);
len--;
if(absorbThis.averageMomentum !== 0) groupsToMove--;
} else {
groups[i].reachEquilibrium();
groupsToMove--;
}
} else if(groups[i].averageMomentum > 0) {
// check below
if(i < (len-1) && groups[i+1].top < groups[i].bottom + groups[i].averageMomentum) {
absorbThis = groups.splice(i+1, 1)[0];
groups[i].moveToAndAbsorb(absorbThis);
len--;
if(absorbThis.averageMomentum !== 0) groupsToMove--;
} else {
groups[i].reachEquilibrium();
groupsToMove--;
}
}
}
}
if(toHighlight.length > 0) {
var now = (new Date()).getTime(),
rateLimit = $.browser.webkit ? 40 : 110;
if(!self.lastHighlightAt) self.lastHighlightAt = 0;
if(now - self.lastHighlightAt < rateLimit) {
return;
} else {
self.lastHighlightAt = now;
$.each(groups,function(p,r){
$.each(this.items,function(i) {
if(self.hoverLabelRenderer()) {
self.hoverLabelRenderer()(this.point, r.top+i*labelHeight, this.plot);
}
else {
this.plot.highlightPoint(this.point, r.top+i*labelHeight, this.plot);
}
});
});
}
}
} else {
self.removeHighlightPoints();
}
});
}
return _canvas;
}
//
// accessor
//
self.options = function( set_options ) {
if ( arguments.length ) { _options = set_options; }
return _options;
}
//
// accessor
//
self.noHover = function( set_noHover ) {
if ( arguments.length ) { _noHover = set_noHover; }
return _noHover;
}
//
// accessor
//
self.xAxisInterval = function( set_xAxisInterval ) {
if ( arguments.length ) { _xAxisInterval = set_xAxisInterval; }
return _xAxisInterval;
}
//
// accessor
//
self.xAxisLabelFormatter = function( set_xAxisLabelFormatter ) {
if ( arguments.length ) {
_xAxisLabelFormatter = Graphy.util.functionByNameOrFunction( set_xAxisLabelFormatter, Graphy.formatters );
}
return _xAxisLabelFormatter;
}
//
// accessor
//
self.yAxisLabelFormatter = function( set_yAxisLabelFormatter ) {
if ( arguments.length ) {
_yAxisLabelFormatter = Graphy.util.functionByNameOrFunction( set_yAxisLabelFormatter, Graphy.formatters );
}
return _yAxisLabelFormatter;
}
//
// accessor
//
self.xAxisRenderer = function( set_xAxisRenderer ) {
if ( arguments.length ) {
_xAxisRenderer = Graphy.util.functionByNameOrFunction( set_xAxisRenderer, Graphy.renderers.axis );
}
return _xAxisRenderer;
}
//
// accessor
//
self.yAxisRenderer = function( set_yAxisRenderer ) {
if ( arguments.length ) {
_yAxisRenderer = Graphy.util.functionByNameOrFunction( set_yAxisRenderer, Graphy.renderers.axis );
}
return _yAxisRenderer;
}
//
// accessor
//
self.vRuleLabel = function( set_vRuleLabel ) {
if ( arguments.length ) { _vRuleLabel = set_vRuleLabel; }
return _vRuleLabel;
}
self.drawVRule = function( set_drawVRule ) {
if ( arguments.length ) {
_drawVRule = Graphy.util.functionByNameOrFunction( set_drawVRule, Graphy.formatters );
}
return _drawVRule;
}
//
// accessor
//
self.hoverLabelRenderer = function( set_hoverLabelRenderer ) {
if ( arguments.length ) {
_hoverLabelRenderer = set_hoverLabelRenderer;
}
return _hoverLabelRenderer;
}
//
// read-only accessor
//
self.$canvas = function() {
return _$canvas;
}
//
// read-only accessor for the canvas' context
//
self.ctx = function() {
return _ctx;
}
//
// read-only accessor
//
self.index = function() {
return _index;
}
//
// read-only accessor
//
self.valueRect = function() {
return Graphy.util.createRect(_valueRect);
}
//
// read-only accessor
//
self.valueRectByUnit = function(key) {
return Graphy.util.createRect( _valueRectByUnit[key] );
}
//
// Checks to see if "renderer" will be used in _any_ of the plots
//
self.usesRenderer = function(needle_renderer) {
var options, renderer;
needle_renderer = Graphy.util.functionByNameOrFunction( needle_renderer, Graphy.renderers, Graphy.renderers.plot );
// global options
if ( Graphy.util.functionByNameOrFunction( _options.renderer, Graphy.renderers, Graphy.renderers.plot ) == needle_renderer ) { return true; }
// each plot
for ( var i = 0; i < _plots.length; i++ ) {
renderer = Graphy.util.functionByNameOrFunction( _plots[i].options.renderer, Graphy.renderers, Graphy.renderers.plot );
if ( renderer == needle_renderer ) { return true; }
}
return false;
}
//
// axis may be either "x" or "y". anything else will result in a combined object
//
// read-only accessor. units array will contain an object like {label: "cm", color: "red"}
//
self.units = function(axis) {
if (axis == "x") {
return _xUnits;
} else if (axis=="y") {
return _yUnits;
} else {
return $.merge( $.merge([], _yUnits), _xUnits );
}
}
//
// read-only accessor. units array will contain an object like {label: "cm", color: "red"}
//
self.yUnits = function() {
return _yUnits;
}
//
// read-only accessor. units array will contain an object like {label: "cm", color: "red"}
//
self.xUnits = function() {
return _xUnits;
}
//
// read-only accessor
//
self.graphRect = function() {
return _graphRect;
}
//
// read-only accessor
//
self.firstPlot = function() {
return _plots[0];
}
//
// read-only accessor
//
self.lastPlot = function() {
return _plots[_plots.length-1];
}
//
// read-only accessor
//
self.hasSelectedItems = function() {
return selectedPlotCount > 0;
}
//
// Removes all plots and redraws (blank)
//
self.clear = function() {
self.removeHighlightPoints();
_plots = [];
_valueRect = false;
_xUnits = [];
_yUnits = [];
selectedPlotCount = 0;
self.noHover(true);
_valueRectByUnit = {};
self.draw();
}
//
// Plots a data series. This is probably what you are looking for. Data can either be an
// array series or a hash formatted { data: [[x,y]...], options: {} }. If data is not a 2d array
// it will be converted to one with the x being the array index.
//
self.plot = function(data, options) {
if ( data.constructor != Array ) {
options = data['options'];
data = data['data'];
}
data = data.sort(function(a,b){ return (a[0] - b[0]); });//sort by x-axis value ascending
options = $.extend( {}, _options, options );
for ( var i = 0, len = data.length; i < len; i++ ) {
if ( data[i].constructor != Array ) { data[i] = [ _xAxisInterval ? _xAxisInterval * i : i, data[i] ]; }
var dp = data[i];
if(dp[0] === null || dp[0] === undefined || dp[1] === null || dp[1] === undefined) continue;
if ( dp[0].constructor == Date ) { dp[0] = dp[0].getTime(); }
if ( dp[1].constructor == Date ) { dp[1] = dp[1].getTime(); }
if ( options['xOffset'] ) { dp[0] += ( options['xOffset'].constructor == Date ? options['xOffset'].getTime() : options['xOffset'] ); }
//
// Find mins an maxs
//
if ( !_valueRect ) {
_valueRect = Graphy.util.createRect( {left: dp[0], right: dp[0], bottom: dp[1], top: dp[1]} );
} else {
_valueRect.recalculateWithPoint(dp);
}
var yUnitOption = options['unit'] || options['yUnit'];
if ( yUnitOption ) {
if ( !_valueRectByUnit[yUnitOption] ) {
_valueRectByUnit[yUnitOption] = Graphy.util.createRect( {left: dp[0], right: dp[0], bottom: dp[1], top: dp[1]} );
_yUnits.push( {"label": yUnitOption, "color": options['color']} );
} else {
_valueRectByUnit[yUnitOption].recalculateWithPoint( dp );
}
}
if ( options['xUnit'] ) {
if ( !_valueRectByUnit[options['xUnit']] ) {
_valueRectByUnit[options['xUnit']] = Graphy.util.createRect( {left: dp[0], right: dp[0], bottom: dp[1], top: dp[1]} );
_xUnits.push( {"label": options['xUnit'], "color": options['color']} );
} else {
_valueRectByUnit[options['xUnit']].recalculateWithPoint( dp );
}
}
}
// Each rect width should be the same to prevent misalignment along the x-axis.
if(_.size(_valueRectByUnit)) {
var leftMost = _.min(_valueRectByUnit, function(rect){ return rect.left }).left;
var rightMost = _.max(_valueRectByUnit, function(rect){ return rect.right }).right;
_.each(_valueRectByUnit, function(rect){
rect.left = leftMost;
rect.right = rightMost;
});
}
if ( options['force_zero'] || Graphy.util.functionByNameOrFunction( options['renderer'], Graphy.renderers ) == Graphy.renderers.bar ) { // merged options
if ( _valueRectByUnit[yUnitOption].top < 0 ) { _valueRectByUnit[yUnitOption].top = 0; }
if ( _valueRectByUnit[yUnitOption].bottom > 0 ) { _valueRectByUnit[yUnitOption].bottom = 0; }
if ( _valueRect.top < 0 ) { _valueRect.top = 0; }
if ( _valueRect.bottom > 0 ) { _valueRect.bottom = 0; }
}
var p = this.newPlotObject({ "data": data, "options": options, graphy: self });
p.draw = function() { self.draw(); return p; };
p.remove = function() { return self.removePlot(p); };
p.select = function() {
if ( !p.options['selected'] ) {
p.options['selected'] = true;
selectedPlotCount++;
}
self.draw();
return p;
};
p.unselect = p.deselect = function() {
if ( p.options['selected'] ) {
p.options['selected'] = false;
selectedPlotCount--;
}
self.draw();
return p;
};
// only enable hover if a non-bar graph has been added. hover is disabled when the graph is cleared and this is where it eventually gets enabled.
if(!spec.noHover && Graphy.util.functionByNameOrFunction( options['renderer'], Graphy.renderers ) != Graphy.renderers.bar) {
self.noHover(false);
}
_plots.push( p );
return p;
}
// FIXME: do we want to do this differently? perhaps a prototype plot object that we can do new Graphy.Plot()
self.newPlotObject = function(opts) {
var p = {
valueRect: function() {
return this.options['unit'] ? this.graphy.valueRectByUnit(this.options['unit']).niceRect : this.graphy.valueRect().niceRect;
},
fromDocumentPixelToPlotPoint: function(documentPixel) {
var graphRectPixel = this.graphy.fromDocumentPixelToGraphRectPixel(documentPixel);
var valueRect = this.valueRect(),
x = Graphy.util.applyValueToNewRatio(graphRectPixel.x, _graphRect.left, _graphRect.right, valueRect.left, valueRect.right),
y = Graphy.util.applyValueToNewRatio(graphRectPixel.y, _graphRect.bottom, _graphRect.top, valueRect.top, valueRect.bottom, true);
return {x:x, y:y};
},
fromPlotPointToDocumentPixel: function(plotPoint) {
var valueRect = this.valueRect(),
x = Graphy.util.applyValueToNewRatio(plotPoint.x, valueRect.left, valueRect.right, _graphRect.left, _graphRect.right),
y = Graphy.util.applyValueToNewRatio(plotPoint.y, valueRect.top, valueRect.bottom, _graphRect.bottom, _graphRect.top, true);
return this.graphy.fromGraphRectPixelToDocumentPixel({x:x,y:y});
},
removeHighlightPointIfExists: function() {
if(this.highlightedPoint) {
if(this.highlightedPoint.element) this.highlightedPoint.element.remove();
if(this.highlightedPoint.arrowElement) this.highlightedPoint.arrowElement.remove();
delete this.highlightedPoint;
}
},
highlightPoint: function(point, topDocumentPixelPosition, p) {
//TODO: need to check if its in the exact perfect position
// target point is already highlighted, so dont do anything
//if(this.highlightedPoint && this.highlightedPoint.point == point) return;
p.removeHighlightPointIfExists();
var pixel = p.fromPlotPointToDocumentPixel({x:point[0],y:point[1]}),
highlightEstimatedHeight = 20,
defaultTop = pixel.y-highlightEstimatedHeight/2,
highlightTop = topDocumentPixelPosition;
var labelOffset = highlightTop - defaultTop,
labelOffsetAbs = Math.abs(labelOffset),
arrowWidth = 30,
arrowHeight = 8,
$c = $.createCanvas(arrowWidth, (arrowHeight + labelOffsetAbs)).css({position:'absolute','z-index':1001}),
c = $c.get(0);
var ctx = c.getContext('2d');
// set up the label and build it
var label,
x_display_point = Math.round(point[0] * 100)/100,
y_display_point = Math.round(point[1] * 100)/100;
if(p.options.unit) {
label = y_display_point + ' ' + p.options.unit + ' @ ' + Graphy.formatters.humanDate(x_display_point);
} else {
label = 'x: ' + x_display_point + ', y: ' + y_display_point;
}
if(p.options.dataSourceAndTypeLabel){
label += " - " + p.options.dataSourceAndTypeLabel;
}
p.highlightedPoint = {
point: point,
arrowElement: $c,
element: $('<div class="graphy_point_label">' + label + '</div>')
.css('background-color', p.options.color || 'gray')
.css('z-index', 1001)
.prependTo('body')
.addClass('graphy')
}
// figure out if we need to draw it on the left or not
var $gCanvas = $(p.graphy.canvas()),
gRight = $gCanvas.offset().left + $gCanvas.width(),
allowedLabelWidth = 250,
xTransform,
arrowLeft,
labelLeft,
showOnLeft = gRight < pixel.x + allowedLabelWidth;
// deal with displaying the label and arrow on either the left or the right side by setting left offset positions and an optional x coord transform
if(showOnLeft) {
xTransform = function(x) { return -x + arrowWidth; }
arrowLeft = pixel.x - arrowWidth;
labelLeft = pixel.x - arrowWidth - p.highlightedPoint.element.outerWidth();
} else {
xTransform = function(x) { return x; }
arrowLeft = pixel.x;
labelLeft = pixel.x + arrowWidth;
}
// if you are trying to understand this, i recommend drawing a picture
var cOffsetTop;
ctx.lineWidth = 2;
if(labelOffset <= 0) {
ctx.moveTo(xTransform(0), arrowHeight/2 + labelOffsetAbs);
ctx.lineTo(xTransform(arrowWidth), 0);
ctx.lineTo(xTransform(arrowWidth), arrowHeight);
cOffsetTop = highlightTop + (highlightEstimatedHeight - arrowHeight)/2;
} else {
var overlapNudge = arrowHeight/2 - labelOffsetAbs;
ctx.moveTo(xTransform(0), overlapNudge > 0 ? overlapNudge : 0);
ctx.lineTo(xTransform(arrowWidth), overlapNudge > 0 ? 0 : labelOffsetAbs - arrowHeight/2);
ctx.lineTo(xTransform(arrowWidth), overlapNudge > 0 ? arrowHeight : labelOffsetAbs + arrowHeight/2);
cOffsetTop = highlightTop - (labelOffset - arrowHeight/2) + (highlightEstimatedHeight - arrowHeight)/2;
if(overlapNudge > 0) cOffsetTop -= overlapNudge;
}
ctx.fillStyle = p.options.color || 'gray';
ctx.fill();
p.highlightedPoint.element.offset({top: highlightTop, left: labelLeft});
$c.prependTo('body').addClass('graphy').offset({left: arrowLeft, top: cOffsetTop});
}
}
return $.extend(p, opts);
};
//
// Remove the plot, specified by plot reference or index.
//
self.removePlot = function( plot_or_index ) {
var p, i;
if ( plot_or_index.constructor == Number ) {
i = plot_or_index;
p = _plots[i]
} else {
p = plot_or_index;
i = $.inArray(p, _plots);
}
// TODO: update noHover() here. it does not appear that this function is being used.
return _plots.splice(i,1)[0];
}
//
// plots an array of plots. see plot for hash format.
//
self.plots = function(plotArray, options) {
if ( plotArray ) {
for ( var i = 0; i < plotArray.length; i++ ) {
self.plot(plotArray[i], options);
}
}
return _plots;
}
//
//this helper just returns just the bar plots
//
self.barPlots = function(){
return _.select(_plots, function(p){ return p.options.renderer == 'bar'; })
}
//
//this helper returns the number of points in the plot with the most points
//
self.maxNumBarsInPlot = function(){
return _.max(self.barPlots(), function(p){ return p.data.length; }).data.length
}
//
// plots based on a json hash (single plot) or array (multiple plots). see plot for hash format.
//
self.plot_json = function(json) {
try {
var obj = $.parseJSON(json);
} catch(err) {
console.warn(err, json);
return self;
}
if ( obj ) {
if ( obj.constructor != Array ) { obj = [obj]; }
self.plots( obj );
}
return _plots;
}
//
// getter/setter for max_x/y
//
self.max = function(axisName, val) {
if ( val != null ) { axisName == 'x' ? _valueRect.right = val : _valueRect.top = val; }
return axisName == 'x' ? _valueRect.right : _valueRect.top;
}
//
// getter/setter for min_x/y
//
self.min = function(axisName, val) {
if ( val != null ) {
axisName == 'x' ? _valueRect.left = val : _valueRect.bottom = val;
}
return axisName == 'x' ? _valueRect.left : _valueRect.bottom;
}
init(spec);
return self;
}
}
// Copyright 2010-2012 DECK Monitoring LLC.
//
// This file is part of Graphy from DECK Monitoring LLC.
//
// Graphy is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
// version.
//
// Graphy is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
//
// You should have received a copy of the Lesser General Public License along with Graphy. If not, see
// <http://www.gnu.org/licenses/>.
//
Graphy.filters = {
// The CUSUM (or cumulative sum control chart) is used for
// monitoring change detection. It involves the calculation of a
// cumulative sum; the sum itself is the sum of differences
// between points in an actual data set, and a data set with
// target values.
//
// target - An Array of Arrays where each sub Array has
// the structure of [time, value]. These sub
// Arrays should be sorted by time.
// actual - Ditto.
//
// Returns a data set with a structure identical to those of
// the parameters, but each value is the CUSUM at that time.
cusum: function(target, actual) {
var series = []
, cusum = 0
, i = 0
, j = 0;
while (i < actual.length) {
if (actual[i][0] < target[j][0]) {
i++;
} else if (actual[i][0] > target[j][0]) {
j++;
} else {
cusum += (actual[i][1] - target[j][1]);
series.push([actual[i][0], cusum]);
i++; j++;
}
}
return series;
}
}
// Copyright 2010-2012 DECK Monitoring LLC.
//
// This file is part of Graphy from DECK Monitoring LLC.
//
// Graphy is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser Public License
// as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
// version.
//