forked from shutterstock/rickshaw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rickshaw.js
2277 lines (1744 loc) · 52.4 KB
/
rickshaw.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
Rickshaw = {
namespace: function(namespace, obj) {
var parts = namespace.split('.');
// for rudimentary compatibility w/ node
var root = typeof global != 'undefined' ? global : window;
var parent = root.Rickshaw;
for(var i = 1, length = parts.length; i < length; i++) {
currentPart = parts[i];
parent[currentPart] = parent[currentPart] || {};
parent = parent[currentPart];
}
return parent;
},
keys: function(obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
}
};
/* Adapted from https://github.com/Jakobo/PTClass */
/*
Copyright (c) 2005-2010 Sam Stephenson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* Based on Alex Arnell's inheritance implementation. */
/** section: Language
* class Class
*
* Manages Prototype's class-based OOP system.
*
* Refer to Prototype's web site for a [tutorial on classes and
* inheritance](http://prototypejs.org/learn/class-inheritance).
**/
(function(globalContext) {
/* ------------------------------------ */
/* Import from object.js */
/* ------------------------------------ */
var _toString = Object.prototype.toString,
NULL_TYPE = 'Null',
UNDEFINED_TYPE = 'Undefined',
BOOLEAN_TYPE = 'Boolean',
NUMBER_TYPE = 'Number',
STRING_TYPE = 'String',
OBJECT_TYPE = 'Object',
FUNCTION_CLASS = '[object Function]';
function isFunction(object) {
return _toString.call(object) === FUNCTION_CLASS;
}
function extend(destination, source) {
for (var property in source) if (source.hasOwnProperty(property)) // modify protect primitive slaughter
destination[property] = source[property];
return destination;
}
function keys(object) {
if (Type(object) !== OBJECT_TYPE) { throw new TypeError(); }
var results = [];
for (var property in object) {
if (object.hasOwnProperty(property)) {
results.push(property);
}
}
return results;
}
function Type(o) {
switch(o) {
case null: return NULL_TYPE;
case (void 0): return UNDEFINED_TYPE;
}
var type = typeof o;
switch(type) {
case 'boolean': return BOOLEAN_TYPE;
case 'number': return NUMBER_TYPE;
case 'string': return STRING_TYPE;
}
return OBJECT_TYPE;
}
function isUndefined(object) {
return typeof object === "undefined";
}
/* ------------------------------------ */
/* Import from Function.js */
/* ------------------------------------ */
var slice = Array.prototype.slice;
function argumentNames(fn) {
var names = fn.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1]
.replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '')
.replace(/\s+/g, '').split(',');
return names.length == 1 && !names[0] ? [] : names;
}
function wrap(fn, wrapper) {
var __method = fn;
return function() {
var a = update([bind(__method, this)], arguments);
return wrapper.apply(this, a);
}
}
function update(array, args) {
var arrayLength = array.length, length = args.length;
while (length--) array[arrayLength + length] = args[length];
return array;
}
function merge(array, args) {
array = slice.call(array, 0);
return update(array, args);
}
function bind(fn, context) {
if (arguments.length < 2 && isUndefined(arguments[0])) return this;
var __method = fn, args = slice.call(arguments, 2);
return function() {
var a = merge(args, arguments);
return __method.apply(context, a);
}
}
/* ------------------------------------ */
/* Import from Prototype.js */
/* ------------------------------------ */
var emptyFunction = function(){};
var Class = (function() {
// Some versions of JScript fail to enumerate over properties, names of which
// correspond to non-enumerable properties in the prototype chain
var IS_DONTENUM_BUGGY = (function(){
for (var p in { toString: 1 }) {
// check actual property name, so that it works with augmented Object.prototype
if (p === 'toString') return false;
}
return true;
})();
function subclass() {};
function create() {
var parent = null, properties = [].slice.apply(arguments);
if (isFunction(properties[0]))
parent = properties.shift();
function klass() {
this.initialize.apply(this, arguments);
}
extend(klass, Class.Methods);
klass.superclass = parent;
klass.subclasses = [];
if (parent) {
subclass.prototype = parent.prototype;
klass.prototype = new subclass;
try { parent.subclasses.push(klass) } catch(e) {}
}
for (var i = 0, length = properties.length; i < length; i++)
klass.addMethods(properties[i]);
if (!klass.prototype.initialize)
klass.prototype.initialize = emptyFunction;
klass.prototype.constructor = klass;
return klass;
}
function addMethods(source) {
var ancestor = this.superclass && this.superclass.prototype,
properties = keys(source);
// IE6 doesn't enumerate `toString` and `valueOf` (among other built-in `Object.prototype`) properties,
// Force copy if they're not Object.prototype ones.
// Do not copy other Object.prototype.* for performance reasons
if (IS_DONTENUM_BUGGY) {
if (source.toString != Object.prototype.toString)
properties.push("toString");
if (source.valueOf != Object.prototype.valueOf)
properties.push("valueOf");
}
for (var i = 0, length = properties.length; i < length; i++) {
var property = properties[i], value = source[property];
if (ancestor && isFunction(value) &&
argumentNames(value)[0] == "$super") {
var method = value;
value = wrap((function(m) {
return function() { return ancestor[m].apply(this, arguments); };
})(property), method);
value.valueOf = bind(method.valueOf, method);
value.toString = bind(method.toString, method);
}
this.prototype[property] = value;
}
return this;
}
return {
create: create,
Methods: {
addMethods: addMethods
}
};
})();
if (globalContext.exports) {
globalContext.exports.Class = Class;
}
else {
globalContext.Class = Class;
}
})(Rickshaw);
Rickshaw.namespace('Rickshaw.Compat.ClassList');
Rickshaw.Compat.ClassList = function() {
/* adapted from http://purl.eligrey.com/github/classList.js/blob/master/classList.js */
if (typeof document !== "undefined" && !("classList" in document.createElement("a"))) {
(function (view) {
"use strict";
var
classListProp = "classList"
, protoProp = "prototype"
, elemCtrProto = (view.HTMLElement || view.Element)[protoProp]
, objCtr = Object
, strTrim = String[protoProp].trim || function () {
return this.replace(/^\s+|\s+$/g, "");
}
, arrIndexOf = Array[protoProp].indexOf || function (item) {
var
i = 0
, len = this.length
;
for (; i < len; i++) {
if (i in this && this[i] === item) {
return i;
}
}
return -1;
}
// Vendors: please allow content code to instantiate DOMExceptions
, DOMEx = function (type, message) {
this.name = type;
this.code = DOMException[type];
this.message = message;
}
, checkTokenAndGetIndex = function (classList, token) {
if (token === "") {
throw new DOMEx(
"SYNTAX_ERR"
, "An invalid or illegal string was specified"
);
}
if (/\s/.test(token)) {
throw new DOMEx(
"INVALID_CHARACTER_ERR"
, "String contains an invalid character"
);
}
return arrIndexOf.call(classList, token);
}
, ClassList = function (elem) {
var
trimmedClasses = strTrim.call(elem.className)
, classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
, i = 0
, len = classes.length
;
for (; i < len; i++) {
this.push(classes[i]);
}
this._updateClassName = function () {
elem.className = this.toString();
};
}
, classListProto = ClassList[protoProp] = []
, classListGetter = function () {
return new ClassList(this);
}
;
// Most DOMException implementations don't allow calling DOMException's toString()
// on non-DOMExceptions. Error's toString() is sufficient here.
DOMEx[protoProp] = Error[protoProp];
classListProto.item = function (i) {
return this[i] || null;
};
classListProto.contains = function (token) {
token += "";
return checkTokenAndGetIndex(this, token) !== -1;
};
classListProto.add = function (token) {
token += "";
if (checkTokenAndGetIndex(this, token) === -1) {
this.push(token);
this._updateClassName();
}
};
classListProto.remove = function (token) {
token += "";
var index = checkTokenAndGetIndex(this, token);
if (index !== -1) {
this.splice(index, 1);
this._updateClassName();
}
};
classListProto.toggle = function (token) {
token += "";
if (checkTokenAndGetIndex(this, token) === -1) {
this.add(token);
} else {
this.remove(token);
}
};
classListProto.toString = function () {
return this.join(" ");
};
if (objCtr.defineProperty) {
var classListPropDesc = {
get: classListGetter
, enumerable: true
, configurable: true
};
try {
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
} catch (ex) { // IE 8 doesn't support enumerable:true
if (ex.number === -0x7FF5EC54) {
classListPropDesc.enumerable = false;
objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
}
}
} else if (objCtr[protoProp].__defineGetter__) {
elemCtrProto.__defineGetter__(classListProp, classListGetter);
}
}(self));
}
};
if ( (typeof RICKSHAW_NO_COMPAT !== "undefined" && !RICKSHAW_NO_COMPAT) || typeof RICKSHAW_NO_COMPAT === "undefined") {
new Rickshaw.Compat.ClassList();
}
Rickshaw.namespace('Rickshaw.Graph');
Rickshaw.Graph = function(args) {
this.element = args.element;
this.interpolation = args.interpolation || 'cardinal';
this.series = args.series;
this.offset = 'zero';
this.stroke = args.stroke || false;
var style = window.getComputedStyle(this.element, null);
var elementWidth = parseInt(style.getPropertyValue('width'));
var elementHeight = parseInt(style.getPropertyValue('height'));
this.width = args.width || elementWidth || 400;
this.height = args.height || elementHeight || 250;
this.min = args.min;
this.max = args.max;
this.window = {};
this.updateCallbacks = [];
var self = this;
this.initialize = function(args) {
this.validateSeries(args.series);
this.series.active = function() { return self.series.filter( function(s) { return !s.disabled } ) };
this.element.classList.add('rickshaw_graph');
this.vis = d3.select(this.element)
.append("svg:svg")
.attr('width', this.width)
.attr('height', this.height);
var renderers = [
Rickshaw.Graph.Renderer.Stack,
Rickshaw.Graph.Renderer.Line,
Rickshaw.Graph.Renderer.Bar,
Rickshaw.Graph.Renderer.Area,
Rickshaw.Graph.Renderer.ScatterPlot
];
renderers.forEach( function(r) {
if (!r) return;
self.registerRenderer(new r( { graph: self } ));
} );
this.setRenderer(args.renderer || 'stack');
this.discoverRange();
};
this.validateSeries = function(series) {
if (!(series instanceof Array) && !(series instanceof Rickshaw.Series)) {
var seriesSignature = Object.prototype.toString.apply(series);
throw "series is not an array: " + seriesSignature;
}
var pointsCount;
series.forEach( function(s) {
if (!(s instanceof Object)) {
throw "series element is not an object: " + s;
}
if (!(s.data)) {
throw "series has no data: " + JSON.stringify(s);
}
if (!(s.data instanceof Array)) {
throw "series data is not an array: " + JSON.stringify(s.data);
}
pointsCount = pointsCount || s.data.length;
if (pointsCount && s.data.length != pointsCount) {
throw "series cannot have differing numbers of points: " +
pointsCount + " vs " + s.data.length + "; see Rickshaw.Series.zeroFill()";
}
var dataTypeX = typeof s.data[0].x;
var dataTypeY = typeof s.data[0].y;
if (dataTypeX != 'number' || dataTypeY != 'number') {
throw "x and y properties of points should be numbers instead of " +
dataTypeX + " and " + dataTypeY;
}
} );
};
this.dataDomain = function() {
// take from the first series
var data = this.series[0].data;
return [ data[0].x, data.slice(-1).shift().x ];
};
this.discoverRange = function() {
var domain = this.renderer.domain();
this.x = d3.scale.linear().domain(domain.x).range([0, this.width]);
this.y = d3.scale.linear().domain(domain.y).range([this.height, 0]);
this.y.magnitude = d3.scale.linear().domain(domain.y).range([0, this.height]);
};
this.render = function() {
var stackedData = this.stackData();
this.discoverRange();
this.renderer.render();
this.updateCallbacks.forEach( function(callback) {
callback();
} );
};
this.update = this.render;
this.stackData = function() {
var data = this.series.active()
.map( function(d) { return d.data } )
.map( function(d) { return d.filter( function(d) { return this._slice(d) }, this ) }, this);
this.stackData.hooks.data.forEach( function(entry) {
data = entry.f.apply(self, [data]);
} );
var layout = d3.layout.stack();
layout.offset( self.offset );
var stackedData = layout(data);
this.stackData.hooks.after.forEach( function(entry) {
stackedData = entry.f.apply(self, [data]);
} );
var i = 0;
this.series.forEach( function(series) {
if (series.disabled) return;
series.stack = stackedData[i++];
} );
this.stackedData = stackedData;
return stackedData;
};
this.stackData.hooks = { data: [], after: [] };
this._slice = function(d) {
if (this.window.xMin || this.window.xMax) {
var isInRange = true;
if (this.window.xMin && d.x <= this.window.xMin) isInRange = false;
if (this.window.xMax && d.x >= this.window.xMax) isInRange = false;
return isInRange;
}
return true;
};
this.onUpdate = function(callback) {
this.updateCallbacks.push(callback);
};
this.registerRenderer = function(renderer) {
this._renderers = this._renderers || {};
this._renderers[renderer.name] = renderer;
};
this.setRenderer = function(name) {
if (!this._renderers[name]) {
throw "couldn't find renderer " + name;
}
this.renderer = this._renderers[name];
};
this.initialize(args);
};
Rickshaw.namespace('Rickshaw.Fixtures.Color');
Rickshaw.Fixtures.Color = function() {
this.schemes = {};
this.schemes.spectrum14 = [
'#ecb796',
'#dc8f70',
'#b2a470',
'#92875a',
'#716c49',
'#d2ed82',
'#bbe468',
'#a1d05d',
'#e7cbe6',
'#d8aad6',
'#a888c2',
'#9dc2d3',
'#649eb9',
'#387aa3'
].reverse();
this.schemes.spectrum2000 = [
'#57306f',
'#514c76',
'#646583',
'#738394',
'#6b9c7d',
'#84b665',
'#a7ca50',
'#bfe746',
'#e2f528',
'#fff726',
'#ecdd00',
'#d4b11d',
'#de8800',
'#de4800',
'#c91515',
'#9a0000',
'#7b0429',
'#580839',
'#31082b'
];
this.schemes.spectrum2001 = [
'#2f243f',
'#3c2c55',
'#4a3768',
'#565270',
'#6b6b7c',
'#72957f',
'#86ad6e',
'#a1bc5e',
'#b8d954',
'#d3e04e',
'#ccad2a',
'#cc8412',
'#c1521d',
'#ad3821',
'#8a1010',
'#681717',
'#531e1e',
'#3d1818',
'#320a1b'
];
this.schemes.classic9 = [
'#423d4f',
'#4a6860',
'#848f39',
'#a2b73c',
'#ddcb53',
'#c5a32f',
'#7d5836',
'#963b20',
'#7c2626',
'#491d37',
'#2f254a'
].reverse();
this.schemes.httpStatus = {
503: '#ea5029',
502: '#d23f14',
500: '#bf3613',
410: '#efacea',
409: '#e291dc',
403: '#f457e8',
408: '#e121d2',
401: '#b92dae',
405: '#f47ceb',
404: '#a82a9f',
400: '#b263c6',
301: '#6fa024',
302: '#87c32b',
307: '#a0d84c',
304: '#28b55c',
200: '#1a4f74',
206: '#27839f',
201: '#52adc9',
202: '#7c979f',
203: '#a5b8bd',
204: '#c1cdd1'
};
this.schemes.colorwheel = [
'#b5b6a9',
'#858772',
'#785f43',
'#96557e',
'#4682b4',
'#65b9ac',
'#73c03a',
'#cb513a'
].reverse();
this.schemes.cool = [
'#5e9d2f',
'#73c03a',
'#4682b4',
'#7bc3b8',
'#a9884e',
'#c1b266',
'#a47493',
'#c09fb5'
];
this.schemes.munin = [
'#00cc00',
'#0066b3',
'#ff8000',
'#ffcc00',
'#330099',
'#990099',
'#ccff00',
'#ff0000',
'#808080',
'#008f00',
'#00487d',
'#b35a00',
'#b38f00',
'#6b006b',
'#8fb300',
'#b30000',
'#bebebe',
'#80ff80',
'#80c9ff',
'#ffc080',
'#ffe680',
'#aa80ff',
'#ee00cc',
'#ff8080',
'#666600',
'#ffbfff',
'#00ffcc',
'#cc6699',
'#999900'
];
};
Rickshaw.namespace('Rickshaw.Fixtures.RandomData');
Rickshaw.Fixtures.RandomData = function(timeInterval) {
var addData;
timeInterval = timeInterval || 1;
var lastRandomValue = 200;
var timeBase = Math.floor(new Date().getTime() / 1000);
this.addData = function(data) {
var randomValue = Math.random() * 100 + 15 + lastRandomValue;
var index = data[0].length;
var counter = 1;
data.forEach( function(series) {
var randomVariance = Math.random() * 20;
var v = randomValue / 25 + counter++
+ (Math.cos((index * counter * 11) / 960) + 2) * 15
+ (Math.cos(index / 7) + 2) * 7
+ (Math.cos(index / 17) + 2) * 1;
series.push( { x: (index * timeInterval) + timeBase, y: v + randomVariance } );
} );
lastRandomValue = randomValue * .85;
}
};
Rickshaw.namespace('Rickshaw.Fixtures.Time');
Rickshaw.Fixtures.Time = function() {
var tzOffset = new Date().getTimezoneOffset() * 60;
var self = this;
this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
this.units = [
{
name: 'decade',
seconds: 86400 * 365.25 * 10,
formatter: function(d) { return (parseInt(d.getUTCFullYear() / 10) * 10) }
}, {
name: 'year',
seconds: 86400 * 365.25,
formatter: function(d) { return d.getUTCFullYear() }
}, {
name: 'month',
seconds: 86400 * 30.5,
formatter: function(d) { return self.months[d.getUTCMonth()] }
}, {
name: 'week',
seconds: 86400 * 7,
formatter: function(d) { return self.formatDate(d) }
}, {
name: 'day',
seconds: 86400,
formatter: function(d) { return d.getUTCDate() }
}, {
name: '6 hour',
seconds: 3600 * 6,
formatter: function(d) { return self.formatTime(d) }
}, {
name: 'hour',
seconds: 3600,
formatter: function(d) { return self.formatTime(d) }
}, {
name: '15 minute',
seconds: 60 * 15,
formatter: function(d) { return self.formatTime(d) }
}, {
name: 'minute',
seconds: 60,
formatter: function(d) { return d.getUTCMinutes() }
}, {
name: '15 second',
seconds: 15,
formatter: function(d) { return d.getUTCSeconds() + 's' }
}, {
name: 'second',
seconds: 1,
formatter: function(d) { return d.getUTCSeconds() + 's' }
}
];
this.unit = function(unitName) {
return this.units.filter( function(unit) { return unitName == unit.name } ).shift();
};
this.formatDate = function(d) {
return d.toUTCString().match(/, (\w+ \w+ \w+)/)[1];
};
this.formatTime = function(d) {
return d.toUTCString().match(/(\d+:\d+):/)[1];
};
this.ceil = function(time, unit) {
if (unit.name == 'month') {
var nearFuture = new Date((time + unit.seconds - 1) * 1000);
return new Date(nearFuture.getUTCFullYear(), nearFuture.getUTCMonth() + 1, 1, 0, 0, 0, 0).getTime() / 1000;
}
if (unit.name == 'year') {
var nearFuture = new Date((time + unit.seconds - 1) * 1000);
return new Date(nearFuture.getUTCFullYear(), 1, 1, 0, 0, 0, 0).getTime() / 1000;
}
return Math.ceil(time / unit.seconds) * unit.seconds;
};
};
Rickshaw.namespace('Rickshaw.Fixtures.Number');
Rickshaw.Fixtures.Number.formatKMBT = function(y) {
if (y >= 1000000000000) { return y / 1000000000000 + "T" }
else if (y >= 1000000000) { return y / 1000000000 + "B" }
else if (y >= 1000000) { return y / 1000000 + "M" }
else if (y >= 1000) { return y / 1000 + "K" }
else if (y < 1 && y > 0) { return y.toFixed(2) }
else if (y == 0) { return '' }
else { return y }
};
Rickshaw.Fixtures.Number.formatBase1024KMGTP = function(y) {
if (y >= 1125899906842624) { return y / 1125899906842624 + "P" }
else if (y >= 1099511627776){ return y / 1099511627776 + "T" }
else if (y >= 1073741824) { return y / 1073741824 + "G" }
else if (y >= 1048576) { return y / 1048576 + "M" }
else if (y >= 1024) { return y / 1024 + "K" }
else if (y < 1 && y > 0) { return y.toFixed(2) }
else if (y == 0) { return '' }
else { return y }
};
Rickshaw.namespace("Rickshaw.Color.Palette");
Rickshaw.Color.Palette = function(args) {
var color = new Rickshaw.Fixtures.Color();
args = args || {};
this.schemes = {};
this.scheme = color.schemes[args.scheme] || args.scheme || color.schemes.colorwheel;
this.runningIndex = 0;
this.color = function(key) {
return this.scheme[key] || this.scheme[this.runningIndex++] || '#808080';
};
};
Rickshaw.namespace('Graph.Ajax');
Rickshaw.Graph.Ajax = function(args) {
var self = this;
this.dataURL = args.dataURL;
$.ajax( {
url: this.dataURL,
complete: function(response, status) {
if (status === 'error') {
console.log("error loading dataURL: " + this.dataURL);
}
var data = JSON.parse(response.responseText);
if (typeof args.onData === 'function') {
var processedData = args.onData(data);
data = processedData;
}
if (args.series) {
args.series.forEach( function(s) {
var seriesKey = s.key || s.name;
if (!seriesKey) throw "series needs a key or a name";
data.forEach( function(d) {
var dataKey = d.key || d.name;
if (!dataKey) throw "data needs a key or a name";
if (seriesKey == dataKey) {
var properties = ['color', 'name', 'data'];
properties.forEach( function(p) {
s[p] = s[p] || d[p];
} );
}
} );
} );
} else {
args.series = data;
}
self.graph = new Rickshaw.Graph(args);
self.graph.render();
if (typeof args.onComplete == 'function') {
args.onComplete(self);
}
}
} );
};
Rickshaw.namespace('Rickshaw.Graph.Annotate');
Rickshaw.Graph.Annotate = function(args) {
var graph = this.graph = args.graph;
this.elements = { timeline: args.element };
var self = this;
this.data = {};
this.elements.timeline.classList.add('rickshaw_annotation_timeline');
this.add = function(time, content) {
self.data[time] = self.data[time] || {'boxes': []};
self.data[time].boxes.push({content: content});
};
this.update = function() {
for (var time in self.data) {
var annotation = self.data[time];
var left = self.graph.x(time);
if (left > self.graph.x.range()[1]) {
if (annotation.element) {
annotation.element.style.display = 'none';
}
continue;
}
if (!annotation.element) {
var element = annotation.element = document.createElement('div');
element.classList.add('annotation');
this.elements.timeline.appendChild(element);
element.addEventListener('click', function(e) {
element.classList.toggle('active');
annotation.line.classList.toggle('active');
}, false);
}
annotation.element.style.left = left + 'px';
annotation.element.style.display = 'block';
annotation.boxes.forEach( function(box) {
var element = box.element;
if (!element) {
element = box.element = document.createElement('div');
element.classList.add('content');
element.innerHTML = box.content;
annotation.element.appendChild(element);
annotation.line = document.createElement('div');
annotation.line.classList.add('annotation_line');
self.graph.element.appendChild(annotation.line);
}
annotation.line.style.left = left + 'px';
} );
}
};
this.graph.onUpdate( function() { self.update() } );
};
Rickshaw.namespace('Rickshaw.Graph.Axis.Time');
Rickshaw.Graph.Axis.Time = function(args) {