rickshaw-rails 1.4.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,353 @@
1
+ .rickshaw_graph .detail {
2
+ pointer-events: none;
3
+ position: absolute;
4
+ top: 0;
5
+ z-index: 2;
6
+ background: rgba(0, 0, 0, 0.1);
7
+ bottom: 0;
8
+ width: 1px;
9
+ transition: opacity 0.25s linear;
10
+ -moz-transition: opacity 0.25s linear;
11
+ -o-transition: opacity 0.25s linear;
12
+ -webkit-transition: opacity 0.25s linear;
13
+ }
14
+ .rickshaw_graph .detail.inactive {
15
+ opacity: 0;
16
+ }
17
+ .rickshaw_graph .detail .item.active {
18
+ opacity: 1;
19
+ }
20
+ .rickshaw_graph .detail .x_label {
21
+ font-family: Arial, sans-serif;
22
+ border-radius: 3px;
23
+ padding: 6px;
24
+ opacity: 0.5;
25
+ border: 1px solid #e0e0e0;
26
+ font-size: 12px;
27
+ position: absolute;
28
+ background: white;
29
+ white-space: nowrap;
30
+ }
31
+ .rickshaw_graph .detail .x_label.left {
32
+ left: 0;
33
+ }
34
+ .rickshaw_graph .detail .x_label.right {
35
+ right: 0;
36
+ }
37
+ .rickshaw_graph .detail .item {
38
+ position: absolute;
39
+ z-index: 2;
40
+ border-radius: 3px;
41
+ padding: 0.25em;
42
+ font-size: 12px;
43
+ font-family: Arial, sans-serif;
44
+ opacity: 0;
45
+ background: rgba(0, 0, 0, 0.4);
46
+ color: white;
47
+ border: 1px solid rgba(0, 0, 0, 0.4);
48
+ margin-left: 1em;
49
+ margin-right: 1em;
50
+ margin-top: -1em;
51
+ white-space: nowrap;
52
+ }
53
+ .rickshaw_graph .detail .item.left {
54
+ left: 0;
55
+ }
56
+ .rickshaw_graph .detail .item.right {
57
+ right: 0;
58
+ }
59
+ .rickshaw_graph .detail .item.active {
60
+ opacity: 1;
61
+ background: rgba(0, 0, 0, 0.8);
62
+ }
63
+ .rickshaw_graph .detail .item:after {
64
+ position: absolute;
65
+ display: block;
66
+ width: 0;
67
+ height: 0;
68
+
69
+ content: "";
70
+
71
+ border: 5px solid transparent;
72
+ }
73
+ .rickshaw_graph .detail .item.left:after {
74
+ top: 1em;
75
+ left: -5px;
76
+ margin-top: -5px;
77
+ border-right-color: rgba(0, 0, 0, 0.8);
78
+ border-left-width: 0;
79
+ }
80
+ .rickshaw_graph .detail .item.right:after {
81
+ top: 1em;
82
+ right: -5px;
83
+ margin-top: -5px;
84
+ border-left-color: rgba(0, 0, 0, 0.8);
85
+ border-right-width: 0;
86
+ }
87
+ .rickshaw_graph .detail .dot {
88
+ width: 4px;
89
+ height: 4px;
90
+ margin-left: -2px;
91
+ margin-top: -2px;
92
+ border-radius: 5px;
93
+ position: absolute;
94
+ box-shadow: 0 0 2px rgba(0, 0, 0, 0.6);
95
+ background: white;
96
+ border-width: 2px;
97
+ border-style: solid;
98
+ display: none;
99
+ background-clip: padding-box;
100
+ }
101
+ .rickshaw_graph .detail .dot.active {
102
+ display: block;
103
+ }
104
+ /* graph */
105
+
106
+ .rickshaw_graph {
107
+ position: relative;
108
+ }
109
+ .rickshaw_graph svg {
110
+ display: block;
111
+ overflow: hidden;
112
+ }
113
+
114
+ /* ticks */
115
+
116
+ .rickshaw_graph .x_tick {
117
+ position: absolute;
118
+ top: 0;
119
+ bottom: 0;
120
+ width: 0px;
121
+ border-left: 1px dotted rgba(0, 0, 0, 0.2);
122
+ pointer-events: none;
123
+ }
124
+ .rickshaw_graph .x_tick .title {
125
+ position: absolute;
126
+ font-size: 12px;
127
+ font-family: Arial, sans-serif;
128
+ opacity: 0.5;
129
+ white-space: nowrap;
130
+ margin-left: 3px;
131
+ bottom: 1px;
132
+ }
133
+
134
+ /* annotations */
135
+
136
+ .rickshaw_annotation_timeline {
137
+ height: 1px;
138
+ border-top: 1px solid #e0e0e0;
139
+ margin-top: 10px;
140
+ position: relative;
141
+ }
142
+ .rickshaw_annotation_timeline .annotation {
143
+ position: absolute;
144
+ height: 6px;
145
+ width: 6px;
146
+ margin-left: -2px;
147
+ top: -3px;
148
+ border-radius: 5px;
149
+ background-color: rgba(0, 0, 0, 0.25);
150
+ }
151
+ .rickshaw_graph .annotation_line {
152
+ position: absolute;
153
+ top: 0;
154
+ bottom: -6px;
155
+ width: 0px;
156
+ border-left: 2px solid rgba(0, 0, 0, 0.3);
157
+ display: none;
158
+ }
159
+ .rickshaw_graph .annotation_line.active {
160
+ display: block;
161
+ }
162
+
163
+ .rickshaw_graph .annotation_range {
164
+ background: rgba(0, 0, 0, 0.1);
165
+ display: none;
166
+ position: absolute;
167
+ top: 0;
168
+ bottom: -6px;
169
+ }
170
+ .rickshaw_graph .annotation_range.active {
171
+ display: block;
172
+ }
173
+ .rickshaw_graph .annotation_range.active.offscreen {
174
+ display: none;
175
+ }
176
+
177
+ .rickshaw_annotation_timeline .annotation .content {
178
+ background: white;
179
+ color: black;
180
+ opacity: 0.9;
181
+ padding: 5px 5px;
182
+ box-shadow: 0 0 2px rgba(0, 0, 0, 0.8);
183
+ border-radius: 3px;
184
+ position: relative;
185
+ z-index: 20;
186
+ font-size: 12px;
187
+ padding: 6px 8px 8px;
188
+ top: 18px;
189
+ left: -11px;
190
+ width: 160px;
191
+ display: none;
192
+ cursor: pointer;
193
+ }
194
+ .rickshaw_annotation_timeline .annotation .content:before {
195
+ content: "\25b2";
196
+ position: absolute;
197
+ top: -11px;
198
+ color: white;
199
+ text-shadow: 0 -1px 1px rgba(0, 0, 0, 0.8);
200
+ }
201
+ .rickshaw_annotation_timeline .annotation.active,
202
+ .rickshaw_annotation_timeline .annotation:hover {
203
+ background-color: rgba(0, 0, 0, 0.8);
204
+ cursor: none;
205
+ }
206
+ .rickshaw_annotation_timeline .annotation .content:hover {
207
+ z-index: 50;
208
+ }
209
+ .rickshaw_annotation_timeline .annotation.active .content {
210
+ display: block;
211
+ }
212
+ .rickshaw_annotation_timeline .annotation:hover .content {
213
+ display: block;
214
+ z-index: 50;
215
+ }
216
+ .rickshaw_graph .y_axis,
217
+ .rickshaw_graph .x_axis_d3 {
218
+ fill: none;
219
+ }
220
+ .rickshaw_graph .y_ticks .tick,
221
+ .rickshaw_graph .x_ticks_d3 .tick {
222
+ stroke: rgba(0, 0, 0, 0.16);
223
+ stroke-width: 2px;
224
+ shape-rendering: crisp-edges;
225
+ pointer-events: none;
226
+ }
227
+ .rickshaw_graph .y_grid .tick,
228
+ .rickshaw_graph .x_grid_d3 .tick {
229
+ z-index: -1;
230
+ stroke: rgba(0, 0, 0, 0.20);
231
+ stroke-width: 1px;
232
+ stroke-dasharray: 1 1;
233
+ }
234
+ .rickshaw_graph .y_grid .tick[data-y-value="0"] {
235
+ stroke-dasharray: 1 0;
236
+ }
237
+ .rickshaw_graph .y_grid path,
238
+ .rickshaw_graph .x_grid_d3 path {
239
+ fill: none;
240
+ stroke: none;
241
+ }
242
+ .rickshaw_graph .y_ticks path,
243
+ .rickshaw_graph .x_ticks_d3 path {
244
+ fill: none;
245
+ stroke: #808080;
246
+ }
247
+ .rickshaw_graph .y_ticks text,
248
+ .rickshaw_graph .x_ticks_d3 text {
249
+ opacity: 0.5;
250
+ font-size: 12px;
251
+ pointer-events: none;
252
+ }
253
+ .rickshaw_graph .x_tick.glow .title,
254
+ .rickshaw_graph .y_ticks.glow text {
255
+ fill: black;
256
+ color: black;
257
+ text-shadow:
258
+ -1px 1px 0 rgba(255, 255, 255, 0.1),
259
+ 1px -1px 0 rgba(255, 255, 255, 0.1),
260
+ 1px 1px 0 rgba(255, 255, 255, 0.1),
261
+ 0px 1px 0 rgba(255, 255, 255, 0.1),
262
+ 0px -1px 0 rgba(255, 255, 255, 0.1),
263
+ 1px 0px 0 rgba(255, 255, 255, 0.1),
264
+ -1px 0px 0 rgba(255, 255, 255, 0.1),
265
+ -1px -1px 0 rgba(255, 255, 255, 0.1);
266
+ }
267
+ .rickshaw_graph .x_tick.inverse .title,
268
+ .rickshaw_graph .y_ticks.inverse text {
269
+ fill: white;
270
+ color: white;
271
+ text-shadow:
272
+ -1px 1px 0 rgba(0, 0, 0, 0.8),
273
+ 1px -1px 0 rgba(0, 0, 0, 0.8),
274
+ 1px 1px 0 rgba(0, 0, 0, 0.8),
275
+ 0px 1px 0 rgba(0, 0, 0, 0.8),
276
+ 0px -1px 0 rgba(0, 0, 0, 0.8),
277
+ 1px 0px 0 rgba(0, 0, 0, 0.8),
278
+ -1px 0px 0 rgba(0, 0, 0, 0.8),
279
+ -1px -1px 0 rgba(0, 0, 0, 0.8);
280
+ }
281
+ .rickshaw_legend {
282
+ font-family: Arial;
283
+ font-size: 12px;
284
+ color: white;
285
+ background: #404040;
286
+ display: inline-block;
287
+ padding: 12px 5px;
288
+ border-radius: 2px;
289
+ position: relative;
290
+ }
291
+ .rickshaw_legend:hover {
292
+ z-index: 10;
293
+ }
294
+ .rickshaw_legend .swatch {
295
+ width: 10px;
296
+ height: 10px;
297
+ border: 1px solid rgba(0, 0, 0, 0.2);
298
+ }
299
+ .rickshaw_legend .line {
300
+ clear: both;
301
+ line-height: 140%;
302
+ padding-right: 15px;
303
+ }
304
+ .rickshaw_legend .line .swatch {
305
+ display: inline-block;
306
+ margin-right: 3px;
307
+ border-radius: 2px;
308
+ }
309
+ .rickshaw_legend .label {
310
+ margin: 0;
311
+ white-space: nowrap;
312
+ display: inline;
313
+ font-size: inherit;
314
+ background-color: transparent;
315
+ color: inherit;
316
+ font-weight: normal;
317
+ line-height: normal;
318
+ padding: 0px;
319
+ text-shadow: none;
320
+ }
321
+ .rickshaw_legend .action:hover {
322
+ opacity: 0.6;
323
+ }
324
+ .rickshaw_legend .action {
325
+ margin-right: 0.2em;
326
+ font-size: 10px;
327
+ opacity: 0.2;
328
+ cursor: pointer;
329
+ font-size: 14px;
330
+ }
331
+ .rickshaw_legend .line.disabled {
332
+ opacity: 0.4;
333
+ }
334
+ .rickshaw_legend ul {
335
+ list-style-type: none;
336
+ margin: 0;
337
+ padding: 0;
338
+ margin: 2px;
339
+ cursor: pointer;
340
+ }
341
+ .rickshaw_legend li {
342
+ padding: 0 0 0 2px;
343
+ min-width: 80px;
344
+ white-space: nowrap;
345
+ }
346
+ .rickshaw_legend li:hover {
347
+ background: rgba(255, 255, 255, 0.08);
348
+ border-radius: 3px;
349
+ }
350
+ .rickshaw_legend li:active {
351
+ background: rgba(255, 255, 255, 0.2);
352
+ border-radius: 3px;
353
+ }
@@ -0,0 +1,3994 @@
1
+ /* jshint -W079 */
2
+
3
+ var Rickshaw = {
4
+
5
+ namespace: function(namespace, obj) {
6
+
7
+ var parts = namespace.split('.');
8
+
9
+ var parent = Rickshaw;
10
+
11
+ for(var i = 1, length = parts.length; i < length; i++) {
12
+ var currentPart = parts[i];
13
+ parent[currentPart] = parent[currentPart] || {};
14
+ parent = parent[currentPart];
15
+ }
16
+ return parent;
17
+ },
18
+
19
+ keys: function(obj) {
20
+ var keys = [];
21
+ for (var key in obj) keys.push(key);
22
+ return keys;
23
+ },
24
+
25
+ extend: function(destination, source) {
26
+
27
+ for (var property in source) {
28
+ destination[property] = source[property];
29
+ }
30
+ return destination;
31
+ },
32
+
33
+ clone: function(obj) {
34
+ return JSON.parse(JSON.stringify(obj));
35
+ }
36
+ };
37
+
38
+ if (typeof module !== 'undefined' && module.exports) {
39
+ var d3 = require('d3');
40
+ module.exports = Rickshaw;
41
+ }
42
+
43
+ /* Adapted from https://github.com/Jakobo/PTClass */
44
+
45
+ /*
46
+ Copyright (c) 2005-2010 Sam Stephenson
47
+
48
+ Permission is hereby granted, free of charge, to any person obtaining a copy
49
+ of this software and associated documentation files (the "Software"), to deal
50
+ in the Software without restriction, including without limitation the rights
51
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
52
+ copies of the Software, and to permit persons to whom the Software is
53
+ furnished to do so, subject to the following conditions:
54
+
55
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
56
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
57
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
58
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
59
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
60
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
61
+ SOFTWARE.
62
+ */
63
+ /* Based on Alex Arnell's inheritance implementation. */
64
+ /** section: Language
65
+ * class Class
66
+ *
67
+ * Manages Prototype's class-based OOP system.
68
+ *
69
+ * Refer to Prototype's web site for a [tutorial on classes and
70
+ * inheritance](http://prototypejs.org/learn/class-inheritance).
71
+ **/
72
+ (function(globalContext) {
73
+ /* ------------------------------------ */
74
+ /* Import from object.js */
75
+ /* ------------------------------------ */
76
+ var _toString = Object.prototype.toString,
77
+ NULL_TYPE = 'Null',
78
+ UNDEFINED_TYPE = 'Undefined',
79
+ BOOLEAN_TYPE = 'Boolean',
80
+ NUMBER_TYPE = 'Number',
81
+ STRING_TYPE = 'String',
82
+ OBJECT_TYPE = 'Object',
83
+ FUNCTION_CLASS = '[object Function]';
84
+ function isFunction(object) {
85
+ return _toString.call(object) === FUNCTION_CLASS;
86
+ }
87
+ function extend(destination, source) {
88
+ for (var property in source) if (source.hasOwnProperty(property)) // modify protect primitive slaughter
89
+ destination[property] = source[property];
90
+ return destination;
91
+ }
92
+ function keys(object) {
93
+ if (Type(object) !== OBJECT_TYPE) { throw new TypeError(); }
94
+ var results = [];
95
+ for (var property in object) {
96
+ if (object.hasOwnProperty(property)) {
97
+ results.push(property);
98
+ }
99
+ }
100
+ return results;
101
+ }
102
+ function Type(o) {
103
+ switch(o) {
104
+ case null: return NULL_TYPE;
105
+ case (void 0): return UNDEFINED_TYPE;
106
+ }
107
+ var type = typeof o;
108
+ switch(type) {
109
+ case 'boolean': return BOOLEAN_TYPE;
110
+ case 'number': return NUMBER_TYPE;
111
+ case 'string': return STRING_TYPE;
112
+ }
113
+ return OBJECT_TYPE;
114
+ }
115
+ function isUndefined(object) {
116
+ return typeof object === "undefined";
117
+ }
118
+ /* ------------------------------------ */
119
+ /* Import from Function.js */
120
+ /* ------------------------------------ */
121
+ var slice = Array.prototype.slice;
122
+ function argumentNames(fn) {
123
+ var names = fn.toString().match(/^[\s\(]*function[^(]*\(([^)]*)\)/)[1]
124
+ .replace(/\/\/.*?[\r\n]|\/\*(?:.|[\r\n])*?\*\//g, '')
125
+ .replace(/\s+/g, '').split(',');
126
+ return names.length == 1 && !names[0] ? [] : names;
127
+ }
128
+ function wrap(fn, wrapper) {
129
+ var __method = fn;
130
+ return function() {
131
+ var a = update([bind(__method, this)], arguments);
132
+ return wrapper.apply(this, a);
133
+ }
134
+ }
135
+ function update(array, args) {
136
+ var arrayLength = array.length, length = args.length;
137
+ while (length--) array[arrayLength + length] = args[length];
138
+ return array;
139
+ }
140
+ function merge(array, args) {
141
+ array = slice.call(array, 0);
142
+ return update(array, args);
143
+ }
144
+ function bind(fn, context) {
145
+ if (arguments.length < 2 && isUndefined(arguments[0])) return this;
146
+ var __method = fn, args = slice.call(arguments, 2);
147
+ return function() {
148
+ var a = merge(args, arguments);
149
+ return __method.apply(context, a);
150
+ }
151
+ }
152
+
153
+ /* ------------------------------------ */
154
+ /* Import from Prototype.js */
155
+ /* ------------------------------------ */
156
+ var emptyFunction = function(){};
157
+
158
+ var Class = (function() {
159
+
160
+ // Some versions of JScript fail to enumerate over properties, names of which
161
+ // correspond to non-enumerable properties in the prototype chain
162
+ var IS_DONTENUM_BUGGY = (function(){
163
+ for (var p in { toString: 1 }) {
164
+ // check actual property name, so that it works with augmented Object.prototype
165
+ if (p === 'toString') return false;
166
+ }
167
+ return true;
168
+ })();
169
+
170
+ function subclass() {};
171
+ function create() {
172
+ var parent = null, properties = [].slice.apply(arguments);
173
+ if (isFunction(properties[0]))
174
+ parent = properties.shift();
175
+
176
+ function klass() {
177
+ this.initialize.apply(this, arguments);
178
+ }
179
+
180
+ extend(klass, Class.Methods);
181
+ klass.superclass = parent;
182
+ klass.subclasses = [];
183
+
184
+ if (parent) {
185
+ subclass.prototype = parent.prototype;
186
+ klass.prototype = new subclass;
187
+ try { parent.subclasses.push(klass) } catch(e) {}
188
+ }
189
+
190
+ for (var i = 0, length = properties.length; i < length; i++)
191
+ klass.addMethods(properties[i]);
192
+
193
+ if (!klass.prototype.initialize)
194
+ klass.prototype.initialize = emptyFunction;
195
+
196
+ klass.prototype.constructor = klass;
197
+ return klass;
198
+ }
199
+
200
+ function addMethods(source) {
201
+ var ancestor = this.superclass && this.superclass.prototype,
202
+ properties = keys(source);
203
+
204
+ // IE6 doesn't enumerate `toString` and `valueOf` (among other built-in `Object.prototype`) properties,
205
+ // Force copy if they're not Object.prototype ones.
206
+ // Do not copy other Object.prototype.* for performance reasons
207
+ if (IS_DONTENUM_BUGGY) {
208
+ if (source.toString != Object.prototype.toString)
209
+ properties.push("toString");
210
+ if (source.valueOf != Object.prototype.valueOf)
211
+ properties.push("valueOf");
212
+ }
213
+
214
+ for (var i = 0, length = properties.length; i < length; i++) {
215
+ var property = properties[i], value = source[property];
216
+ if (ancestor && isFunction(value) &&
217
+ argumentNames(value)[0] == "$super") {
218
+ var method = value;
219
+ value = wrap((function(m) {
220
+ return function() { return ancestor[m].apply(this, arguments); };
221
+ })(property), method);
222
+
223
+ value.valueOf = bind(method.valueOf, method);
224
+ value.toString = bind(method.toString, method);
225
+ }
226
+ this.prototype[property] = value;
227
+ }
228
+
229
+ return this;
230
+ }
231
+
232
+ return {
233
+ create: create,
234
+ Methods: {
235
+ addMethods: addMethods
236
+ }
237
+ };
238
+ })();
239
+
240
+ if (globalContext.exports) {
241
+ globalContext.exports.Class = Class;
242
+ }
243
+ else {
244
+ globalContext.Class = Class;
245
+ }
246
+ })(Rickshaw);
247
+ Rickshaw.namespace('Rickshaw.Compat.ClassList');
248
+
249
+ Rickshaw.Compat.ClassList = function() {
250
+
251
+ /* adapted from http://purl.eligrey.com/github/classList.js/blob/master/classList.js */
252
+
253
+ if (typeof document !== "undefined" && !("classList" in document.createElement("a"))) {
254
+
255
+ (function (view) {
256
+
257
+ "use strict";
258
+
259
+ var
260
+ classListProp = "classList"
261
+ , protoProp = "prototype"
262
+ , elemCtrProto = (view.HTMLElement || view.Element)[protoProp]
263
+ , objCtr = Object
264
+ , strTrim = String[protoProp].trim || function () {
265
+ return this.replace(/^\s+|\s+$/g, "");
266
+ }
267
+ , arrIndexOf = Array[protoProp].indexOf || function (item) {
268
+ var
269
+ i = 0
270
+ , len = this.length
271
+ ;
272
+ for (; i < len; i++) {
273
+ if (i in this && this[i] === item) {
274
+ return i;
275
+ }
276
+ }
277
+ return -1;
278
+ }
279
+ // Vendors: please allow content code to instantiate DOMExceptions
280
+ , DOMEx = function (type, message) {
281
+ this.name = type;
282
+ this.code = DOMException[type];
283
+ this.message = message;
284
+ }
285
+ , checkTokenAndGetIndex = function (classList, token) {
286
+ if (token === "") {
287
+ throw new DOMEx(
288
+ "SYNTAX_ERR"
289
+ , "An invalid or illegal string was specified"
290
+ );
291
+ }
292
+ if (/\s/.test(token)) {
293
+ throw new DOMEx(
294
+ "INVALID_CHARACTER_ERR"
295
+ , "String contains an invalid character"
296
+ );
297
+ }
298
+ return arrIndexOf.call(classList, token);
299
+ }
300
+ , ClassList = function (elem) {
301
+ var
302
+ trimmedClasses = strTrim.call(elem.className)
303
+ , classes = trimmedClasses ? trimmedClasses.split(/\s+/) : []
304
+ , i = 0
305
+ , len = classes.length
306
+ ;
307
+ for (; i < len; i++) {
308
+ this.push(classes[i]);
309
+ }
310
+ this._updateClassName = function () {
311
+ elem.className = this.toString();
312
+ };
313
+ }
314
+ , classListProto = ClassList[protoProp] = []
315
+ , classListGetter = function () {
316
+ return new ClassList(this);
317
+ }
318
+ ;
319
+ // Most DOMException implementations don't allow calling DOMException's toString()
320
+ // on non-DOMExceptions. Error's toString() is sufficient here.
321
+ DOMEx[protoProp] = Error[protoProp];
322
+ classListProto.item = function (i) {
323
+ return this[i] || null;
324
+ };
325
+ classListProto.contains = function (token) {
326
+ token += "";
327
+ return checkTokenAndGetIndex(this, token) !== -1;
328
+ };
329
+ classListProto.add = function (token) {
330
+ token += "";
331
+ if (checkTokenAndGetIndex(this, token) === -1) {
332
+ this.push(token);
333
+ this._updateClassName();
334
+ }
335
+ };
336
+ classListProto.remove = function (token) {
337
+ token += "";
338
+ var index = checkTokenAndGetIndex(this, token);
339
+ if (index !== -1) {
340
+ this.splice(index, 1);
341
+ this._updateClassName();
342
+ }
343
+ };
344
+ classListProto.toggle = function (token) {
345
+ token += "";
346
+ if (checkTokenAndGetIndex(this, token) === -1) {
347
+ this.add(token);
348
+ } else {
349
+ this.remove(token);
350
+ }
351
+ };
352
+ classListProto.toString = function () {
353
+ return this.join(" ");
354
+ };
355
+
356
+ if (objCtr.defineProperty) {
357
+ var classListPropDesc = {
358
+ get: classListGetter
359
+ , enumerable: true
360
+ , configurable: true
361
+ };
362
+ try {
363
+ objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
364
+ } catch (ex) { // IE 8 doesn't support enumerable:true
365
+ if (ex.number === -0x7FF5EC54) {
366
+ classListPropDesc.enumerable = false;
367
+ objCtr.defineProperty(elemCtrProto, classListProp, classListPropDesc);
368
+ }
369
+ }
370
+ } else if (objCtr[protoProp].__defineGetter__) {
371
+ elemCtrProto.__defineGetter__(classListProp, classListGetter);
372
+ }
373
+
374
+ }(window));
375
+
376
+ }
377
+ };
378
+
379
+ if ( (typeof RICKSHAW_NO_COMPAT !== "undefined" && !RICKSHAW_NO_COMPAT) || typeof RICKSHAW_NO_COMPAT === "undefined") {
380
+ new Rickshaw.Compat.ClassList();
381
+ }
382
+ Rickshaw.namespace('Rickshaw.Graph');
383
+
384
+ Rickshaw.Graph = function(args) {
385
+
386
+ var self = this;
387
+
388
+ this.initialize = function(args) {
389
+
390
+ if (!args.element) throw "Rickshaw.Graph needs a reference to an element";
391
+ if (args.element.nodeType !== 1) throw "Rickshaw.Graph element was defined but not an HTML element";
392
+
393
+ this.element = args.element;
394
+ this.series = args.series;
395
+ this.window = {};
396
+
397
+ this.updateCallbacks = [];
398
+ this.configureCallbacks = [];
399
+
400
+ this.defaults = {
401
+ interpolation: 'cardinal',
402
+ offset: 'zero',
403
+ min: undefined,
404
+ max: undefined,
405
+ preserve: false,
406
+ xScale: undefined,
407
+ yScale: undefined
408
+ };
409
+
410
+ this._loadRenderers();
411
+ this.configure(args);
412
+ this.validateSeries(args.series);
413
+
414
+ this.series.active = function() { return self.series.filter( function(s) { return !s.disabled } ) };
415
+ this.setSize({ width: args.width, height: args.height });
416
+ this.element.classList.add('rickshaw_graph');
417
+
418
+ this.vis = d3.select(this.element)
419
+ .append("svg:svg")
420
+ .attr('width', this.width)
421
+ .attr('height', this.height);
422
+
423
+ this.discoverRange();
424
+ };
425
+
426
+ this._loadRenderers = function() {
427
+
428
+ for (var name in Rickshaw.Graph.Renderer) {
429
+ if (!name || !Rickshaw.Graph.Renderer.hasOwnProperty(name)) continue;
430
+ var r = Rickshaw.Graph.Renderer[name];
431
+ if (!r || !r.prototype || !r.prototype.render) continue;
432
+ self.registerRenderer(new r( { graph: self } ));
433
+ }
434
+ };
435
+
436
+ this.validateSeries = function(series) {
437
+
438
+ if (!Array.isArray(series) && !(series instanceof Rickshaw.Series)) {
439
+ var seriesSignature = Object.prototype.toString.apply(series);
440
+ throw "series is not an array: " + seriesSignature;
441
+ }
442
+
443
+ var pointsCount;
444
+
445
+ series.forEach( function(s) {
446
+
447
+ if (!(s instanceof Object)) {
448
+ throw "series element is not an object: " + s;
449
+ }
450
+ if (!(s.data)) {
451
+ throw "series has no data: " + JSON.stringify(s);
452
+ }
453
+ if (!Array.isArray(s.data)) {
454
+ throw "series data is not an array: " + JSON.stringify(s.data);
455
+ }
456
+
457
+ var x = s.data[0].x;
458
+ var y = s.data[0].y;
459
+
460
+ if (typeof x != 'number' || ( typeof y != 'number' && y !== null ) ) {
461
+ throw "x and y properties of points should be numbers instead of " +
462
+ (typeof x) + " and " + (typeof y);
463
+ }
464
+
465
+ if (s.data.length >= 3) {
466
+ // probe to sanity check sort order
467
+ if (s.data[2].x < s.data[1].x || s.data[1].x < s.data[0].x || s.data[s.data.length - 1].x < s.data[0].x) {
468
+ throw "series data needs to be sorted on x values for series name: " + s.name;
469
+ }
470
+ }
471
+
472
+ }, this );
473
+ };
474
+
475
+ this.dataDomain = function() {
476
+
477
+ var data = this.series.map( function(s) { return s.data } );
478
+
479
+ var min = d3.min( data.map( function(d) { return d[0].x } ) );
480
+ var max = d3.max( data.map( function(d) { return d[d.length - 1].x } ) );
481
+
482
+ return [min, max];
483
+ };
484
+
485
+ this.discoverRange = function() {
486
+
487
+ var domain = this.renderer.domain();
488
+
489
+ this.x = (this.xScale || d3.scale.linear()).domain(domain.x).range([0, this.width]);
490
+ this.y = (this.yScale || d3.scale.linear()).domain(domain.y).range([this.height, 0]);
491
+
492
+ this.y.magnitude = d3.scale.linear()
493
+ .domain([domain.y[0] - domain.y[0], domain.y[1] - domain.y[0]])
494
+ .range([0, this.height]);
495
+ };
496
+
497
+ this.render = function() {
498
+
499
+ var stackedData = this.stackData();
500
+ this.discoverRange();
501
+
502
+ this.renderer.render();
503
+
504
+ this.updateCallbacks.forEach( function(callback) {
505
+ callback();
506
+ } );
507
+
508
+ };
509
+
510
+ this.update = this.render;
511
+
512
+ this.stackData = function() {
513
+
514
+ var data = this.series.active()
515
+ .map( function(d) { return d.data } )
516
+ .map( function(d) { return d.filter( function(d) { return this._slice(d) }, this ) }, this);
517
+
518
+ var preserve = this.preserve;
519
+ if (!preserve) {
520
+ this.series.forEach( function(series) {
521
+ if (series.scale) {
522
+ // data must be preserved when a scale is used
523
+ preserve = true;
524
+ }
525
+ } );
526
+ }
527
+
528
+ data = preserve ? Rickshaw.clone(data) : data;
529
+
530
+ this.series.active().forEach( function(series, index) {
531
+ if (series.scale) {
532
+ // apply scale to each series
533
+ var seriesData = data[index];
534
+ if(seriesData) {
535
+ seriesData.forEach( function(d) {
536
+ d.y = series.scale(d.y);
537
+ } );
538
+ }
539
+ }
540
+ } );
541
+
542
+ this.stackData.hooks.data.forEach( function(entry) {
543
+ data = entry.f.apply(self, [data]);
544
+ } );
545
+
546
+ var stackedData;
547
+
548
+ if (!this.renderer.unstack) {
549
+
550
+ this._validateStackable();
551
+
552
+ var layout = d3.layout.stack();
553
+ layout.offset( self.offset );
554
+ stackedData = layout(data);
555
+ }
556
+
557
+ stackedData = stackedData || data;
558
+
559
+ if (this.renderer.unstack) {
560
+ stackedData.forEach( function(seriesData) {
561
+ seriesData.forEach( function(d) {
562
+ d.y0 = d.y0 === undefined ? 0 : d.y0;
563
+ } );
564
+ } );
565
+ }
566
+
567
+ this.stackData.hooks.after.forEach( function(entry) {
568
+ stackedData = entry.f.apply(self, [data]);
569
+ } );
570
+
571
+ var i = 0;
572
+ this.series.forEach( function(series) {
573
+ if (series.disabled) return;
574
+ series.stack = stackedData[i++];
575
+ } );
576
+
577
+ this.stackedData = stackedData;
578
+ return stackedData;
579
+ };
580
+
581
+ this._validateStackable = function() {
582
+
583
+ var series = this.series;
584
+ var pointsCount;
585
+
586
+ series.forEach( function(s) {
587
+
588
+ pointsCount = pointsCount || s.data.length;
589
+
590
+ if (pointsCount && s.data.length != pointsCount) {
591
+ throw "stacked series cannot have differing numbers of points: " +
592
+ pointsCount + " vs " + s.data.length + "; see Rickshaw.Series.fill()";
593
+ }
594
+
595
+ }, this );
596
+ };
597
+
598
+ this.stackData.hooks = { data: [], after: [] };
599
+
600
+ this._slice = function(d) {
601
+
602
+ if (this.window.xMin || this.window.xMax) {
603
+
604
+ var isInRange = true;
605
+
606
+ if (this.window.xMin && d.x < this.window.xMin) isInRange = false;
607
+ if (this.window.xMax && d.x > this.window.xMax) isInRange = false;
608
+
609
+ return isInRange;
610
+ }
611
+
612
+ return true;
613
+ };
614
+
615
+ this.onUpdate = function(callback) {
616
+ this.updateCallbacks.push(callback);
617
+ };
618
+
619
+ this.onConfigure = function(callback) {
620
+ this.configureCallbacks.push(callback);
621
+ };
622
+
623
+ this.registerRenderer = function(renderer) {
624
+ this._renderers = this._renderers || {};
625
+ this._renderers[renderer.name] = renderer;
626
+ };
627
+
628
+ this.configure = function(args) {
629
+
630
+ this.config = this.config || {};
631
+
632
+ if (args.width || args.height) {
633
+ this.setSize(args);
634
+ }
635
+
636
+ Rickshaw.keys(this.defaults).forEach( function(k) {
637
+ this.config[k] = k in args ? args[k]
638
+ : k in this ? this[k]
639
+ : this.defaults[k];
640
+ }, this );
641
+
642
+ Rickshaw.keys(this.config).forEach( function(k) {
643
+ this[k] = this.config[k];
644
+ }, this );
645
+
646
+ var renderer = args.renderer || (this.renderer && this.renderer.name) || 'stack';
647
+ this.setRenderer(renderer, args);
648
+
649
+ this.configureCallbacks.forEach( function(callback) {
650
+ callback(args);
651
+ } );
652
+ };
653
+
654
+ this.setRenderer = function(r, args) {
655
+ if (typeof r == 'function') {
656
+ this.renderer = new r( { graph: self } );
657
+ this.registerRenderer(this.renderer);
658
+ } else {
659
+ if (!this._renderers[r]) {
660
+ throw "couldn't find renderer " + r;
661
+ }
662
+ this.renderer = this._renderers[r];
663
+ }
664
+
665
+ if (typeof args == 'object') {
666
+ this.renderer.configure(args);
667
+ }
668
+ };
669
+
670
+ this.setSize = function(args) {
671
+
672
+ args = args || {};
673
+
674
+ if (typeof window !== undefined) {
675
+ var style = window.getComputedStyle(this.element, null);
676
+ var elementWidth = parseInt(style.getPropertyValue('width'), 10);
677
+ var elementHeight = parseInt(style.getPropertyValue('height'), 10);
678
+ }
679
+
680
+ this.width = args.width || elementWidth || 400;
681
+ this.height = args.height || elementHeight || 250;
682
+
683
+ this.vis && this.vis
684
+ .attr('width', this.width)
685
+ .attr('height', this.height);
686
+ };
687
+
688
+ this.initialize(args);
689
+ };
690
+ Rickshaw.namespace('Rickshaw.Fixtures.Color');
691
+
692
+ Rickshaw.Fixtures.Color = function() {
693
+
694
+ this.schemes = {};
695
+
696
+ this.schemes.spectrum14 = [
697
+ '#ecb796',
698
+ '#dc8f70',
699
+ '#b2a470',
700
+ '#92875a',
701
+ '#716c49',
702
+ '#d2ed82',
703
+ '#bbe468',
704
+ '#a1d05d',
705
+ '#e7cbe6',
706
+ '#d8aad6',
707
+ '#a888c2',
708
+ '#9dc2d3',
709
+ '#649eb9',
710
+ '#387aa3'
711
+ ].reverse();
712
+
713
+ this.schemes.spectrum2000 = [
714
+ '#57306f',
715
+ '#514c76',
716
+ '#646583',
717
+ '#738394',
718
+ '#6b9c7d',
719
+ '#84b665',
720
+ '#a7ca50',
721
+ '#bfe746',
722
+ '#e2f528',
723
+ '#fff726',
724
+ '#ecdd00',
725
+ '#d4b11d',
726
+ '#de8800',
727
+ '#de4800',
728
+ '#c91515',
729
+ '#9a0000',
730
+ '#7b0429',
731
+ '#580839',
732
+ '#31082b'
733
+ ];
734
+
735
+ this.schemes.spectrum2001 = [
736
+ '#2f243f',
737
+ '#3c2c55',
738
+ '#4a3768',
739
+ '#565270',
740
+ '#6b6b7c',
741
+ '#72957f',
742
+ '#86ad6e',
743
+ '#a1bc5e',
744
+ '#b8d954',
745
+ '#d3e04e',
746
+ '#ccad2a',
747
+ '#cc8412',
748
+ '#c1521d',
749
+ '#ad3821',
750
+ '#8a1010',
751
+ '#681717',
752
+ '#531e1e',
753
+ '#3d1818',
754
+ '#320a1b'
755
+ ];
756
+
757
+ this.schemes.classic9 = [
758
+ '#423d4f',
759
+ '#4a6860',
760
+ '#848f39',
761
+ '#a2b73c',
762
+ '#ddcb53',
763
+ '#c5a32f',
764
+ '#7d5836',
765
+ '#963b20',
766
+ '#7c2626',
767
+ '#491d37',
768
+ '#2f254a'
769
+ ].reverse();
770
+
771
+ this.schemes.httpStatus = {
772
+ 503: '#ea5029',
773
+ 502: '#d23f14',
774
+ 500: '#bf3613',
775
+ 410: '#efacea',
776
+ 409: '#e291dc',
777
+ 403: '#f457e8',
778
+ 408: '#e121d2',
779
+ 401: '#b92dae',
780
+ 405: '#f47ceb',
781
+ 404: '#a82a9f',
782
+ 400: '#b263c6',
783
+ 301: '#6fa024',
784
+ 302: '#87c32b',
785
+ 307: '#a0d84c',
786
+ 304: '#28b55c',
787
+ 200: '#1a4f74',
788
+ 206: '#27839f',
789
+ 201: '#52adc9',
790
+ 202: '#7c979f',
791
+ 203: '#a5b8bd',
792
+ 204: '#c1cdd1'
793
+ };
794
+
795
+ this.schemes.colorwheel = [
796
+ '#b5b6a9',
797
+ '#858772',
798
+ '#785f43',
799
+ '#96557e',
800
+ '#4682b4',
801
+ '#65b9ac',
802
+ '#73c03a',
803
+ '#cb513a'
804
+ ].reverse();
805
+
806
+ this.schemes.cool = [
807
+ '#5e9d2f',
808
+ '#73c03a',
809
+ '#4682b4',
810
+ '#7bc3b8',
811
+ '#a9884e',
812
+ '#c1b266',
813
+ '#a47493',
814
+ '#c09fb5'
815
+ ];
816
+
817
+ this.schemes.munin = [
818
+ '#00cc00',
819
+ '#0066b3',
820
+ '#ff8000',
821
+ '#ffcc00',
822
+ '#330099',
823
+ '#990099',
824
+ '#ccff00',
825
+ '#ff0000',
826
+ '#808080',
827
+ '#008f00',
828
+ '#00487d',
829
+ '#b35a00',
830
+ '#b38f00',
831
+ '#6b006b',
832
+ '#8fb300',
833
+ '#b30000',
834
+ '#bebebe',
835
+ '#80ff80',
836
+ '#80c9ff',
837
+ '#ffc080',
838
+ '#ffe680',
839
+ '#aa80ff',
840
+ '#ee00cc',
841
+ '#ff8080',
842
+ '#666600',
843
+ '#ffbfff',
844
+ '#00ffcc',
845
+ '#cc6699',
846
+ '#999900'
847
+ ];
848
+ };
849
+ Rickshaw.namespace('Rickshaw.Fixtures.RandomData');
850
+
851
+ Rickshaw.Fixtures.RandomData = function(timeInterval) {
852
+
853
+ var addData;
854
+ timeInterval = timeInterval || 1;
855
+
856
+ var lastRandomValue = 200;
857
+
858
+ var timeBase = Math.floor(new Date().getTime() / 1000);
859
+
860
+ this.addData = function(data) {
861
+
862
+ var randomValue = Math.random() * 100 + 15 + lastRandomValue;
863
+ var index = data[0].length;
864
+
865
+ var counter = 1;
866
+
867
+ data.forEach( function(series) {
868
+ var randomVariance = Math.random() * 20;
869
+ var v = randomValue / 25 + counter++ +
870
+ (Math.cos((index * counter * 11) / 960) + 2) * 15 +
871
+ (Math.cos(index / 7) + 2) * 7 +
872
+ (Math.cos(index / 17) + 2) * 1;
873
+
874
+ series.push( { x: (index * timeInterval) + timeBase, y: v + randomVariance } );
875
+ } );
876
+
877
+ lastRandomValue = randomValue * 0.85;
878
+ };
879
+
880
+ this.removeData = function(data) {
881
+ data.forEach( function(series) {
882
+ series.shift();
883
+ } );
884
+ timeBase += timeInterval;
885
+ };
886
+ };
887
+
888
+ Rickshaw.namespace('Rickshaw.Fixtures.Time');
889
+
890
+ Rickshaw.Fixtures.Time = function() {
891
+
892
+ var self = this;
893
+
894
+ this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
895
+
896
+ this.units = [
897
+ {
898
+ name: 'decade',
899
+ seconds: 86400 * 365.25 * 10,
900
+ formatter: function(d) { return (parseInt(d.getUTCFullYear() / 10, 10) * 10) }
901
+ }, {
902
+ name: 'year',
903
+ seconds: 86400 * 365.25,
904
+ formatter: function(d) { return d.getUTCFullYear() }
905
+ }, {
906
+ name: 'month',
907
+ seconds: 86400 * 30.5,
908
+ formatter: function(d) { return self.months[d.getUTCMonth()] }
909
+ }, {
910
+ name: 'week',
911
+ seconds: 86400 * 7,
912
+ formatter: function(d) { return self.formatDate(d) }
913
+ }, {
914
+ name: 'day',
915
+ seconds: 86400,
916
+ formatter: function(d) { return d.getUTCDate() }
917
+ }, {
918
+ name: '6 hour',
919
+ seconds: 3600 * 6,
920
+ formatter: function(d) { return self.formatTime(d) }
921
+ }, {
922
+ name: 'hour',
923
+ seconds: 3600,
924
+ formatter: function(d) { return self.formatTime(d) }
925
+ }, {
926
+ name: '15 minute',
927
+ seconds: 60 * 15,
928
+ formatter: function(d) { return self.formatTime(d) }
929
+ }, {
930
+ name: 'minute',
931
+ seconds: 60,
932
+ formatter: function(d) { return d.getUTCMinutes() }
933
+ }, {
934
+ name: '15 second',
935
+ seconds: 15,
936
+ formatter: function(d) { return d.getUTCSeconds() + 's' }
937
+ }, {
938
+ name: 'second',
939
+ seconds: 1,
940
+ formatter: function(d) { return d.getUTCSeconds() + 's' }
941
+ }, {
942
+ name: 'decisecond',
943
+ seconds: 1/10,
944
+ formatter: function(d) { return d.getUTCMilliseconds() + 'ms' }
945
+ }, {
946
+ name: 'centisecond',
947
+ seconds: 1/100,
948
+ formatter: function(d) { return d.getUTCMilliseconds() + 'ms' }
949
+ }
950
+ ];
951
+
952
+ this.unit = function(unitName) {
953
+ return this.units.filter( function(unit) { return unitName == unit.name } ).shift();
954
+ };
955
+
956
+ this.formatDate = function(d) {
957
+ return d3.time.format('%b %e')(d);
958
+ };
959
+
960
+ this.formatTime = function(d) {
961
+ return d.toUTCString().match(/(\d+:\d+):/)[1];
962
+ };
963
+
964
+ this.ceil = function(time, unit) {
965
+
966
+ var date, floor, year;
967
+
968
+ if (unit.name == 'month') {
969
+
970
+ date = new Date(time * 1000);
971
+
972
+ floor = Date.UTC(date.getUTCFullYear(), date.getUTCMonth()) / 1000;
973
+ if (floor == time) return time;
974
+
975
+ year = date.getUTCFullYear();
976
+ var month = date.getUTCMonth();
977
+
978
+ if (month == 11) {
979
+ month = 0;
980
+ year = year + 1;
981
+ } else {
982
+ month += 1;
983
+ }
984
+
985
+ return Date.UTC(year, month) / 1000;
986
+ }
987
+
988
+ if (unit.name == 'year') {
989
+
990
+ date = new Date(time * 1000);
991
+
992
+ floor = Date.UTC(date.getUTCFullYear(), 0) / 1000;
993
+ if (floor == time) return time;
994
+
995
+ year = date.getUTCFullYear() + 1;
996
+
997
+ return Date.UTC(year, 0) / 1000;
998
+ }
999
+
1000
+ return Math.ceil(time / unit.seconds) * unit.seconds;
1001
+ };
1002
+ };
1003
+ Rickshaw.namespace('Rickshaw.Fixtures.Time.Local');
1004
+
1005
+ Rickshaw.Fixtures.Time.Local = function() {
1006
+
1007
+ var self = this;
1008
+
1009
+ this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
1010
+
1011
+ this.units = [
1012
+ {
1013
+ name: 'decade',
1014
+ seconds: 86400 * 365.25 * 10,
1015
+ formatter: function(d) { return (parseInt(d.getFullYear() / 10, 10) * 10) }
1016
+ }, {
1017
+ name: 'year',
1018
+ seconds: 86400 * 365.25,
1019
+ formatter: function(d) { return d.getFullYear() }
1020
+ }, {
1021
+ name: 'month',
1022
+ seconds: 86400 * 30.5,
1023
+ formatter: function(d) { return self.months[d.getMonth()] }
1024
+ }, {
1025
+ name: 'week',
1026
+ seconds: 86400 * 7,
1027
+ formatter: function(d) { return self.formatDate(d) }
1028
+ }, {
1029
+ name: 'day',
1030
+ seconds: 86400,
1031
+ formatter: function(d) { return d.getDate() }
1032
+ }, {
1033
+ name: '6 hour',
1034
+ seconds: 3600 * 6,
1035
+ formatter: function(d) { return self.formatTime(d) }
1036
+ }, {
1037
+ name: 'hour',
1038
+ seconds: 3600,
1039
+ formatter: function(d) { return self.formatTime(d) }
1040
+ }, {
1041
+ name: '15 minute',
1042
+ seconds: 60 * 15,
1043
+ formatter: function(d) { return self.formatTime(d) }
1044
+ }, {
1045
+ name: 'minute',
1046
+ seconds: 60,
1047
+ formatter: function(d) { return d.getMinutes() }
1048
+ }, {
1049
+ name: '15 second',
1050
+ seconds: 15,
1051
+ formatter: function(d) { return d.getSeconds() + 's' }
1052
+ }, {
1053
+ name: 'second',
1054
+ seconds: 1,
1055
+ formatter: function(d) { return d.getSeconds() + 's' }
1056
+ }, {
1057
+ name: 'decisecond',
1058
+ seconds: 1/10,
1059
+ formatter: function(d) { return d.getMilliseconds() + 'ms' }
1060
+ }, {
1061
+ name: 'centisecond',
1062
+ seconds: 1/100,
1063
+ formatter: function(d) { return d.getMilliseconds() + 'ms' }
1064
+ }
1065
+ ];
1066
+
1067
+ this.unit = function(unitName) {
1068
+ return this.units.filter( function(unit) { return unitName == unit.name } ).shift();
1069
+ };
1070
+
1071
+ this.formatDate = function(d) {
1072
+ return d3.time.format('%b %e')(d);
1073
+ };
1074
+
1075
+ this.formatTime = function(d) {
1076
+ return d.toString().match(/(\d+:\d+):/)[1];
1077
+ };
1078
+
1079
+ this.ceil = function(time, unit) {
1080
+
1081
+ var date, floor, year;
1082
+
1083
+ if (unit.name == 'day') {
1084
+
1085
+ var nearFuture = new Date((time + unit.seconds - 1) * 1000);
1086
+
1087
+ var rounded = new Date(0);
1088
+ rounded.setMilliseconds(0);
1089
+ rounded.setSeconds(0);
1090
+ rounded.setMinutes(0);
1091
+ rounded.setHours(0);
1092
+ rounded.setDate(nearFuture.getDate());
1093
+ rounded.setMonth(nearFuture.getMonth());
1094
+ rounded.setFullYear(nearFuture.getFullYear());
1095
+
1096
+ return rounded.getTime() / 1000;
1097
+ }
1098
+
1099
+ if (unit.name == 'month') {
1100
+
1101
+ date = new Date(time * 1000);
1102
+
1103
+ floor = new Date(date.getFullYear(), date.getMonth()).getTime() / 1000;
1104
+ if (floor == time) return time;
1105
+
1106
+ year = date.getFullYear();
1107
+ var month = date.getMonth();
1108
+
1109
+ if (month == 11) {
1110
+ month = 0;
1111
+ year = year + 1;
1112
+ } else {
1113
+ month += 1;
1114
+ }
1115
+
1116
+ return new Date(year, month).getTime() / 1000;
1117
+ }
1118
+
1119
+ if (unit.name == 'year') {
1120
+
1121
+ date = new Date(time * 1000);
1122
+
1123
+ floor = new Date(date.getUTCFullYear(), 0).getTime() / 1000;
1124
+ if (floor == time) return time;
1125
+
1126
+ year = date.getFullYear() + 1;
1127
+
1128
+ return new Date(year, 0).getTime() / 1000;
1129
+ }
1130
+
1131
+ return Math.ceil(time / unit.seconds) * unit.seconds;
1132
+ };
1133
+ };
1134
+ Rickshaw.namespace('Rickshaw.Fixtures.Number');
1135
+
1136
+ Rickshaw.Fixtures.Number.formatKMBT = function(y) {
1137
+ var abs_y = Math.abs(y);
1138
+ if (abs_y >= 1000000000000) { return y / 1000000000000 + "T" }
1139
+ else if (abs_y >= 1000000000) { return y / 1000000000 + "B" }
1140
+ else if (abs_y >= 1000000) { return y / 1000000 + "M" }
1141
+ else if (abs_y >= 1000) { return y / 1000 + "K" }
1142
+ else if (abs_y < 1 && y > 0) { return y.toFixed(2) }
1143
+ else if (abs_y === 0) { return '' }
1144
+ else { return y }
1145
+ };
1146
+
1147
+ Rickshaw.Fixtures.Number.formatBase1024KMGTP = function(y) {
1148
+ var abs_y = Math.abs(y);
1149
+ if (abs_y >= 1125899906842624) { return y / 1125899906842624 + "P" }
1150
+ else if (abs_y >= 1099511627776){ return y / 1099511627776 + "T" }
1151
+ else if (abs_y >= 1073741824) { return y / 1073741824 + "G" }
1152
+ else if (abs_y >= 1048576) { return y / 1048576 + "M" }
1153
+ else if (abs_y >= 1024) { return y / 1024 + "K" }
1154
+ else if (abs_y < 1 && y > 0) { return y.toFixed(2) }
1155
+ else if (abs_y === 0) { return '' }
1156
+ else { return y }
1157
+ };
1158
+ Rickshaw.namespace("Rickshaw.Color.Palette");
1159
+
1160
+ Rickshaw.Color.Palette = function(args) {
1161
+
1162
+ var color = new Rickshaw.Fixtures.Color();
1163
+
1164
+ args = args || {};
1165
+ this.schemes = {};
1166
+
1167
+ this.scheme = color.schemes[args.scheme] || args.scheme || color.schemes.colorwheel;
1168
+ this.runningIndex = 0;
1169
+ this.generatorIndex = 0;
1170
+
1171
+ if (args.interpolatedStopCount) {
1172
+ var schemeCount = this.scheme.length - 1;
1173
+ var i, j, scheme = [];
1174
+ for (i = 0; i < schemeCount; i++) {
1175
+ scheme.push(this.scheme[i]);
1176
+ var generator = d3.interpolateHsl(this.scheme[i], this.scheme[i + 1]);
1177
+ for (j = 1; j < args.interpolatedStopCount; j++) {
1178
+ scheme.push(generator((1 / args.interpolatedStopCount) * j));
1179
+ }
1180
+ }
1181
+ scheme.push(this.scheme[this.scheme.length - 1]);
1182
+ this.scheme = scheme;
1183
+ }
1184
+ this.rotateCount = this.scheme.length;
1185
+
1186
+ this.color = function(key) {
1187
+ return this.scheme[key] || this.scheme[this.runningIndex++] || this.interpolateColor() || '#808080';
1188
+ };
1189
+
1190
+ this.interpolateColor = function() {
1191
+ if (!Array.isArray(this.scheme)) return;
1192
+ var color;
1193
+ if (this.generatorIndex == this.rotateCount * 2 - 1) {
1194
+ color = d3.interpolateHsl(this.scheme[this.generatorIndex], this.scheme[0])(0.5);
1195
+ this.generatorIndex = 0;
1196
+ this.rotateCount *= 2;
1197
+ } else {
1198
+ color = d3.interpolateHsl(this.scheme[this.generatorIndex], this.scheme[this.generatorIndex + 1])(0.5);
1199
+ this.generatorIndex++;
1200
+ }
1201
+ this.scheme.push(color);
1202
+ return color;
1203
+ };
1204
+
1205
+ };
1206
+ Rickshaw.namespace('Rickshaw.Graph.Ajax');
1207
+
1208
+ Rickshaw.Graph.Ajax = Rickshaw.Class.create( {
1209
+
1210
+ initialize: function(args) {
1211
+
1212
+ this.dataURL = args.dataURL;
1213
+
1214
+ this.onData = args.onData || function(d) { return d };
1215
+ this.onComplete = args.onComplete || function() {};
1216
+ this.onError = args.onError || function() {};
1217
+
1218
+ this.args = args; // pass through to Rickshaw.Graph
1219
+
1220
+ this.request();
1221
+ },
1222
+
1223
+ request: function() {
1224
+
1225
+ $.ajax( {
1226
+ url: this.dataURL,
1227
+ dataType: 'json',
1228
+ success: this.success.bind(this),
1229
+ error: this.error.bind(this)
1230
+ } );
1231
+ },
1232
+
1233
+ error: function() {
1234
+
1235
+ console.log("error loading dataURL: " + this.dataURL);
1236
+ this.onError(this);
1237
+ },
1238
+
1239
+ success: function(data, status) {
1240
+
1241
+ data = this.onData(data);
1242
+ this.args.series = this._splice({ data: data, series: this.args.series });
1243
+
1244
+ this.graph = this.graph || new Rickshaw.Graph(this.args);
1245
+ this.graph.render();
1246
+
1247
+ this.onComplete(this);
1248
+ },
1249
+
1250
+ _splice: function(args) {
1251
+
1252
+ var data = args.data;
1253
+ var series = args.series;
1254
+
1255
+ if (!args.series) return data;
1256
+
1257
+ series.forEach( function(s) {
1258
+
1259
+ var seriesKey = s.key || s.name;
1260
+ if (!seriesKey) throw "series needs a key or a name";
1261
+
1262
+ data.forEach( function(d) {
1263
+
1264
+ var dataKey = d.key || d.name;
1265
+ if (!dataKey) throw "data needs a key or a name";
1266
+
1267
+ if (seriesKey == dataKey) {
1268
+ var properties = ['color', 'name', 'data'];
1269
+ properties.forEach( function(p) {
1270
+ if (d[p]) s[p] = d[p];
1271
+ } );
1272
+ }
1273
+ } );
1274
+ } );
1275
+
1276
+ return series;
1277
+ }
1278
+ } );
1279
+
1280
+ Rickshaw.namespace('Rickshaw.Graph.Annotate');
1281
+
1282
+ Rickshaw.Graph.Annotate = function(args) {
1283
+
1284
+ var graph = this.graph = args.graph;
1285
+ this.elements = { timeline: args.element };
1286
+
1287
+ var self = this;
1288
+
1289
+ this.data = {};
1290
+
1291
+ this.elements.timeline.classList.add('rickshaw_annotation_timeline');
1292
+
1293
+ this.add = function(time, content, end_time) {
1294
+ self.data[time] = self.data[time] || {'boxes': []};
1295
+ self.data[time].boxes.push({content: content, end: end_time});
1296
+ };
1297
+
1298
+ this.update = function() {
1299
+
1300
+ Rickshaw.keys(self.data).forEach( function(time) {
1301
+
1302
+ var annotation = self.data[time];
1303
+ var left = self.graph.x(time);
1304
+
1305
+ if (left < 0 || left > self.graph.x.range()[1]) {
1306
+ if (annotation.element) {
1307
+ annotation.line.classList.add('offscreen');
1308
+ annotation.element.style.display = 'none';
1309
+ }
1310
+
1311
+ annotation.boxes.forEach( function(box) {
1312
+ if ( box.rangeElement ) box.rangeElement.classList.add('offscreen');
1313
+ });
1314
+
1315
+ return;
1316
+ }
1317
+
1318
+ if (!annotation.element) {
1319
+ var element = annotation.element = document.createElement('div');
1320
+ element.classList.add('annotation');
1321
+ this.elements.timeline.appendChild(element);
1322
+ element.addEventListener('click', function(e) {
1323
+ element.classList.toggle('active');
1324
+ annotation.line.classList.toggle('active');
1325
+ annotation.boxes.forEach( function(box) {
1326
+ if ( box.rangeElement ) box.rangeElement.classList.toggle('active');
1327
+ });
1328
+ }, false);
1329
+
1330
+ }
1331
+
1332
+ annotation.element.style.left = left + 'px';
1333
+ annotation.element.style.display = 'block';
1334
+
1335
+ annotation.boxes.forEach( function(box) {
1336
+
1337
+
1338
+ var element = box.element;
1339
+
1340
+ if (!element) {
1341
+ element = box.element = document.createElement('div');
1342
+ element.classList.add('content');
1343
+ element.innerHTML = box.content;
1344
+ annotation.element.appendChild(element);
1345
+
1346
+ annotation.line = document.createElement('div');
1347
+ annotation.line.classList.add('annotation_line');
1348
+ self.graph.element.appendChild(annotation.line);
1349
+
1350
+ if ( box.end ) {
1351
+ box.rangeElement = document.createElement('div');
1352
+ box.rangeElement.classList.add('annotation_range');
1353
+ self.graph.element.appendChild(box.rangeElement);
1354
+ }
1355
+
1356
+ }
1357
+
1358
+ if ( box.end ) {
1359
+
1360
+ var annotationRangeStart = left;
1361
+ var annotationRangeEnd = Math.min( self.graph.x(box.end), self.graph.x.range()[1] );
1362
+
1363
+ // annotation makes more sense at end
1364
+ if ( annotationRangeStart > annotationRangeEnd ) {
1365
+ annotationRangeEnd = left;
1366
+ annotationRangeStart = Math.max( self.graph.x(box.end), self.graph.x.range()[0] );
1367
+ }
1368
+
1369
+ var annotationRangeWidth = annotationRangeEnd - annotationRangeStart;
1370
+
1371
+ box.rangeElement.style.left = annotationRangeStart + 'px';
1372
+ box.rangeElement.style.width = annotationRangeWidth + 'px';
1373
+
1374
+ box.rangeElement.classList.remove('offscreen');
1375
+ }
1376
+
1377
+ annotation.line.classList.remove('offscreen');
1378
+ annotation.line.style.left = left + 'px';
1379
+ } );
1380
+ }, this );
1381
+ };
1382
+
1383
+ this.graph.onUpdate( function() { self.update() } );
1384
+ };
1385
+ Rickshaw.namespace('Rickshaw.Graph.Axis.Time');
1386
+
1387
+ Rickshaw.Graph.Axis.Time = function(args) {
1388
+
1389
+ var self = this;
1390
+
1391
+ this.graph = args.graph;
1392
+ this.elements = [];
1393
+ this.ticksTreatment = args.ticksTreatment || 'plain';
1394
+ this.fixedTimeUnit = args.timeUnit;
1395
+
1396
+ var time = args.timeFixture || new Rickshaw.Fixtures.Time();
1397
+
1398
+ this.appropriateTimeUnit = function() {
1399
+
1400
+ var unit;
1401
+ var units = time.units;
1402
+
1403
+ var domain = this.graph.x.domain();
1404
+ var rangeSeconds = domain[1] - domain[0];
1405
+
1406
+ units.forEach( function(u) {
1407
+ if (Math.floor(rangeSeconds / u.seconds) >= 2) {
1408
+ unit = unit || u;
1409
+ }
1410
+ } );
1411
+
1412
+ return (unit || time.units[time.units.length - 1]);
1413
+ };
1414
+
1415
+ this.tickOffsets = function() {
1416
+
1417
+ var domain = this.graph.x.domain();
1418
+
1419
+ var unit = this.fixedTimeUnit || this.appropriateTimeUnit();
1420
+ var count = Math.ceil((domain[1] - domain[0]) / unit.seconds);
1421
+
1422
+ var runningTick = domain[0];
1423
+
1424
+ var offsets = [];
1425
+
1426
+ for (var i = 0; i < count; i++) {
1427
+
1428
+ var tickValue = time.ceil(runningTick, unit);
1429
+ runningTick = tickValue + unit.seconds / 2;
1430
+
1431
+ offsets.push( { value: tickValue, unit: unit } );
1432
+ }
1433
+
1434
+ return offsets;
1435
+ };
1436
+
1437
+ this.render = function() {
1438
+
1439
+ this.elements.forEach( function(e) {
1440
+ e.parentNode.removeChild(e);
1441
+ } );
1442
+
1443
+ this.elements = [];
1444
+
1445
+ var offsets = this.tickOffsets();
1446
+
1447
+ offsets.forEach( function(o) {
1448
+
1449
+ if (self.graph.x(o.value) > self.graph.x.range()[1]) return;
1450
+
1451
+ var element = document.createElement('div');
1452
+ element.style.left = self.graph.x(o.value) + 'px';
1453
+ element.classList.add('x_tick');
1454
+ element.classList.add(self.ticksTreatment);
1455
+
1456
+ var title = document.createElement('div');
1457
+ title.classList.add('title');
1458
+ title.innerHTML = o.unit.formatter(new Date(o.value * 1000));
1459
+ element.appendChild(title);
1460
+
1461
+ self.graph.element.appendChild(element);
1462
+ self.elements.push(element);
1463
+
1464
+ } );
1465
+ };
1466
+
1467
+ this.graph.onUpdate( function() { self.render() } );
1468
+ };
1469
+
1470
+ Rickshaw.namespace('Rickshaw.Graph.Axis.X');
1471
+
1472
+ Rickshaw.Graph.Axis.X = function(args) {
1473
+
1474
+ var self = this;
1475
+ var berthRate = 0.10;
1476
+
1477
+ this.initialize = function(args) {
1478
+
1479
+ this.graph = args.graph;
1480
+ this.orientation = args.orientation || 'top';
1481
+
1482
+ this.pixelsPerTick = args.pixelsPerTick || 75;
1483
+ if (args.ticks) this.staticTicks = args.ticks;
1484
+ if (args.tickValues) this.tickValues = args.tickValues;
1485
+
1486
+ this.tickSize = args.tickSize || 4;
1487
+ this.ticksTreatment = args.ticksTreatment || 'plain';
1488
+
1489
+ if (args.element) {
1490
+
1491
+ this.element = args.element;
1492
+ this._discoverSize(args.element, args);
1493
+
1494
+ this.vis = d3.select(args.element)
1495
+ .append("svg:svg")
1496
+ .attr('height', this.height)
1497
+ .attr('width', this.width)
1498
+ .attr('class', 'rickshaw_graph x_axis_d3');
1499
+
1500
+ this.element = this.vis[0][0];
1501
+ this.element.style.position = 'relative';
1502
+
1503
+ this.setSize({ width: args.width, height: args.height });
1504
+
1505
+ } else {
1506
+ this.vis = this.graph.vis;
1507
+ }
1508
+
1509
+ this.graph.onUpdate( function() { self.render() } );
1510
+ };
1511
+
1512
+ this.setSize = function(args) {
1513
+
1514
+ args = args || {};
1515
+ if (!this.element) return;
1516
+
1517
+ this._discoverSize(this.element.parentNode, args);
1518
+
1519
+ this.vis
1520
+ .attr('height', this.height)
1521
+ .attr('width', this.width * (1 + berthRate));
1522
+
1523
+ var berth = Math.floor(this.width * berthRate / 2);
1524
+ this.element.style.left = -1 * berth + 'px';
1525
+ };
1526
+
1527
+ this.render = function() {
1528
+
1529
+ if (this._renderWidth !== undefined && this.graph.width !== this._renderWidth) this.setSize({ auto: true });
1530
+
1531
+ var axis = d3.svg.axis().scale(this.graph.x).orient(this.orientation);
1532
+ axis.tickFormat( args.tickFormat || function(x) { return x } );
1533
+ if (this.tickValues) axis.tickValues(this.tickValues);
1534
+
1535
+ this.ticks = this.staticTicks || Math.floor(this.graph.width / this.pixelsPerTick);
1536
+
1537
+ var berth = Math.floor(this.width * berthRate / 2) || 0;
1538
+ var transform;
1539
+
1540
+ if (this.orientation == 'top') {
1541
+ var yOffset = this.height || this.graph.height;
1542
+ transform = 'translate(' + berth + ',' + yOffset + ')';
1543
+ } else {
1544
+ transform = 'translate(' + berth + ', 0)';
1545
+ }
1546
+
1547
+ if (this.element) {
1548
+ this.vis.selectAll('*').remove();
1549
+ }
1550
+
1551
+ this.vis
1552
+ .append("svg:g")
1553
+ .attr("class", ["x_ticks_d3", this.ticksTreatment].join(" "))
1554
+ .attr("transform", transform)
1555
+ .call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(this.tickSize));
1556
+
1557
+ var gridSize = (this.orientation == 'bottom' ? 1 : -1) * this.graph.height;
1558
+
1559
+ this.graph.vis
1560
+ .append("svg:g")
1561
+ .attr("class", "x_grid_d3")
1562
+ .call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(gridSize))
1563
+ .selectAll('text')
1564
+ .each(function() { this.parentNode.setAttribute('data-x-value', this.textContent) });
1565
+
1566
+ this._renderHeight = this.graph.height;
1567
+ };
1568
+
1569
+ this._discoverSize = function(element, args) {
1570
+
1571
+ if (typeof window !== 'undefined') {
1572
+
1573
+ var style = window.getComputedStyle(element, null);
1574
+ var elementHeight = parseInt(style.getPropertyValue('height'), 10);
1575
+
1576
+ if (!args.auto) {
1577
+ var elementWidth = parseInt(style.getPropertyValue('width'), 10);
1578
+ }
1579
+ }
1580
+
1581
+ this.width = (args.width || elementWidth || this.graph.width) * (1 + berthRate);
1582
+ this.height = args.height || elementHeight || 40;
1583
+ };
1584
+
1585
+ this.initialize(args);
1586
+ };
1587
+
1588
+ Rickshaw.namespace('Rickshaw.Graph.Axis.Y');
1589
+
1590
+ Rickshaw.Graph.Axis.Y = Rickshaw.Class.create( {
1591
+
1592
+ initialize: function(args) {
1593
+
1594
+ this.graph = args.graph;
1595
+ this.orientation = args.orientation || 'right';
1596
+
1597
+ this.pixelsPerTick = args.pixelsPerTick || 75;
1598
+ if (args.ticks) this.staticTicks = args.ticks;
1599
+ if (args.tickValues) this.tickValues = args.tickValues;
1600
+
1601
+ this.tickSize = args.tickSize || 4;
1602
+ this.ticksTreatment = args.ticksTreatment || 'plain';
1603
+
1604
+ this.tickFormat = args.tickFormat || function(y) { return y };
1605
+
1606
+ this.berthRate = 0.10;
1607
+
1608
+ if (args.element) {
1609
+
1610
+ this.element = args.element;
1611
+ this.vis = d3.select(args.element)
1612
+ .append("svg:svg")
1613
+ .attr('class', 'rickshaw_graph y_axis');
1614
+
1615
+ this.element = this.vis[0][0];
1616
+ this.element.style.position = 'relative';
1617
+
1618
+ this.setSize({ width: args.width, height: args.height });
1619
+
1620
+ } else {
1621
+ this.vis = this.graph.vis;
1622
+ }
1623
+
1624
+ var self = this;
1625
+ this.graph.onUpdate( function() { self.render() } );
1626
+ },
1627
+
1628
+ setSize: function(args) {
1629
+
1630
+ args = args || {};
1631
+
1632
+ if (!this.element) return;
1633
+
1634
+ if (typeof window !== 'undefined') {
1635
+
1636
+ var style = window.getComputedStyle(this.element.parentNode, null);
1637
+ var elementWidth = parseInt(style.getPropertyValue('width'), 10);
1638
+
1639
+ if (!args.auto) {
1640
+ var elementHeight = parseInt(style.getPropertyValue('height'), 10);
1641
+ }
1642
+ }
1643
+
1644
+ this.width = args.width || elementWidth || this.graph.width * this.berthRate;
1645
+ this.height = args.height || elementHeight || this.graph.height;
1646
+
1647
+ this.vis
1648
+ .attr('width', this.width)
1649
+ .attr('height', this.height * (1 + this.berthRate));
1650
+
1651
+ var berth = this.height * this.berthRate;
1652
+
1653
+ if (this.orientation == 'left') {
1654
+ this.element.style.top = -1 * berth + 'px';
1655
+ }
1656
+ },
1657
+
1658
+ render: function() {
1659
+
1660
+ if (this._renderHeight !== undefined && this.graph.height !== this._renderHeight) this.setSize({ auto: true });
1661
+
1662
+ this.ticks = this.staticTicks || Math.floor(this.graph.height / this.pixelsPerTick);
1663
+
1664
+ var axis = this._drawAxis(this.graph.y);
1665
+
1666
+ this._drawGrid(axis);
1667
+
1668
+ this._renderHeight = this.graph.height;
1669
+ },
1670
+
1671
+ _drawAxis: function(scale) {
1672
+ var axis = d3.svg.axis().scale(scale).orient(this.orientation);
1673
+ axis.tickFormat(this.tickFormat);
1674
+ if (this.tickValues) axis.tickValues(this.tickValues);
1675
+
1676
+ if (this.orientation == 'left') {
1677
+ var berth = this.height * this.berthRate;
1678
+ var transform = 'translate(' + this.width + ', ' + berth + ')';
1679
+ }
1680
+
1681
+ if (this.element) {
1682
+ this.vis.selectAll('*').remove();
1683
+ }
1684
+
1685
+ this.vis
1686
+ .append("svg:g")
1687
+ .attr("class", ["y_ticks", this.ticksTreatment].join(" "))
1688
+ .attr("transform", transform)
1689
+ .call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(this.tickSize));
1690
+
1691
+ return axis;
1692
+ },
1693
+
1694
+ _drawGrid: function(axis) {
1695
+ var gridSize = (this.orientation == 'right' ? 1 : -1) * this.graph.width;
1696
+
1697
+ this.graph.vis
1698
+ .append("svg:g")
1699
+ .attr("class", "y_grid")
1700
+ .call(axis.ticks(this.ticks).tickSubdivide(0).tickSize(gridSize))
1701
+ .selectAll('text')
1702
+ .each(function() { this.parentNode.setAttribute('data-y-value', this.textContent) });
1703
+ }
1704
+ } );
1705
+ Rickshaw.namespace('Rickshaw.Graph.Axis.Y.Scaled');
1706
+
1707
+ Rickshaw.Graph.Axis.Y.Scaled = Rickshaw.Class.create( Rickshaw.Graph.Axis.Y, {
1708
+
1709
+ initialize: function($super, args) {
1710
+
1711
+ if (typeof(args.scale) === 'undefined') {
1712
+ throw new Error('Scaled requires scale');
1713
+ }
1714
+
1715
+ this.scale = args.scale;
1716
+
1717
+ if (typeof(args.grid) === 'undefined') {
1718
+ this.grid = true;
1719
+ } else {
1720
+ this.grid = args.grid;
1721
+ }
1722
+
1723
+ $super(args);
1724
+
1725
+ },
1726
+
1727
+ _drawAxis: function($super, scale) {
1728
+ // Adjust scale's domain to compensate for adjustments to the
1729
+ // renderer's domain (e.g. padding).
1730
+ var domain = this.scale.domain();
1731
+ var renderDomain = this.graph.renderer.domain().y;
1732
+
1733
+ var extents = [
1734
+ Math.min.apply(Math, domain),
1735
+ Math.max.apply(Math, domain)];
1736
+
1737
+ // A mapping from the ideal render domain [0, 1] to the extent
1738
+ // of the original scale's domain. This is used to calculate
1739
+ // the extents of the adjusted domain.
1740
+ var extentMap = d3.scale.linear().domain([0, 1]).range(extents);
1741
+
1742
+ var adjExtents = [
1743
+ extentMap(renderDomain[0]),
1744
+ extentMap(renderDomain[1])];
1745
+
1746
+ // A mapping from the original domain to the adjusted domain.
1747
+ var adjustment = d3.scale.linear().domain(extents).range(adjExtents);
1748
+
1749
+ // Make a copy of the custom scale, apply the adjusted domain, and
1750
+ // copy the range to match the graph's scale.
1751
+ var adjustedScale = this.scale.copy()
1752
+ .domain(domain.map(adjustment))
1753
+ .range(scale.range());
1754
+
1755
+ return $super(adjustedScale);
1756
+ },
1757
+
1758
+ _drawGrid: function($super, axis) {
1759
+ if (this.grid) {
1760
+ // only draw the axis if the grid option is true
1761
+ $super(axis);
1762
+ }
1763
+ }
1764
+ } );
1765
+ Rickshaw.namespace('Rickshaw.Graph.Behavior.Series.Highlight');
1766
+
1767
+ Rickshaw.Graph.Behavior.Series.Highlight = function(args) {
1768
+
1769
+ this.graph = args.graph;
1770
+ this.legend = args.legend;
1771
+
1772
+ var self = this;
1773
+
1774
+ var colorSafe = {};
1775
+ var activeLine = null;
1776
+
1777
+ var disabledColor = args.disabledColor || function(seriesColor) {
1778
+ return d3.interpolateRgb(seriesColor, d3.rgb('#d8d8d8'))(0.8).toString();
1779
+ };
1780
+
1781
+ this.addHighlightEvents = function (l) {
1782
+
1783
+ l.element.addEventListener( 'mouseover', function(e) {
1784
+
1785
+ if (activeLine) return;
1786
+ else activeLine = l;
1787
+
1788
+ self.legend.lines.forEach( function(line) {
1789
+
1790
+ if (l === line) {
1791
+
1792
+ // if we're not in a stacked renderer bring active line to the top
1793
+ if (self.graph.renderer.unstack && (line.series.renderer ? line.series.renderer.unstack : true)) {
1794
+
1795
+ var seriesIndex = self.graph.series.indexOf(line.series);
1796
+ line.originalIndex = seriesIndex;
1797
+
1798
+ var series = self.graph.series.splice(seriesIndex, 1)[0];
1799
+ self.graph.series.push(series);
1800
+ }
1801
+ return;
1802
+ }
1803
+
1804
+ colorSafe[line.series.name] = colorSafe[line.series.name] || line.series.color;
1805
+ line.series.color = disabledColor(line.series.color);
1806
+
1807
+ } );
1808
+
1809
+ self.graph.update();
1810
+
1811
+ }, false );
1812
+
1813
+ l.element.addEventListener( 'mouseout', function(e) {
1814
+
1815
+ if (!activeLine) return;
1816
+ else activeLine = null;
1817
+
1818
+ self.legend.lines.forEach( function(line) {
1819
+
1820
+ // return reordered series to its original place
1821
+ if (l === line && line.hasOwnProperty('originalIndex')) {
1822
+
1823
+ var series = self.graph.series.pop();
1824
+ self.graph.series.splice(line.originalIndex, 0, series);
1825
+ delete line.originalIndex;
1826
+ }
1827
+
1828
+ if (colorSafe[line.series.name]) {
1829
+ line.series.color = colorSafe[line.series.name];
1830
+ }
1831
+ } );
1832
+
1833
+ self.graph.update();
1834
+
1835
+ }, false );
1836
+ };
1837
+
1838
+ if (this.legend) {
1839
+ this.legend.lines.forEach( function(l) {
1840
+ self.addHighlightEvents(l);
1841
+ } );
1842
+ }
1843
+
1844
+ };
1845
+ Rickshaw.namespace('Rickshaw.Graph.Behavior.Series.Order');
1846
+
1847
+ Rickshaw.Graph.Behavior.Series.Order = function(args) {
1848
+
1849
+ this.graph = args.graph;
1850
+ this.legend = args.legend;
1851
+
1852
+ var self = this;
1853
+
1854
+ if (typeof window.$ == 'undefined') {
1855
+ throw "couldn't find jQuery at window.$";
1856
+ }
1857
+
1858
+ if (typeof window.$.ui == 'undefined') {
1859
+ throw "couldn't find jQuery UI at window.$.ui";
1860
+ }
1861
+
1862
+ $(function() {
1863
+ $(self.legend.list).sortable( {
1864
+ containment: 'parent',
1865
+ tolerance: 'pointer',
1866
+ update: function( event, ui ) {
1867
+ var series = [];
1868
+ $(self.legend.list).find('li').each( function(index, item) {
1869
+ if (!item.series) return;
1870
+ series.push(item.series);
1871
+ } );
1872
+
1873
+ for (var i = self.graph.series.length - 1; i >= 0; i--) {
1874
+ self.graph.series[i] = series.shift();
1875
+ }
1876
+
1877
+ self.graph.update();
1878
+ }
1879
+ } );
1880
+ $(self.legend.list).disableSelection();
1881
+ });
1882
+
1883
+ //hack to make jquery-ui sortable behave
1884
+ this.graph.onUpdate( function() {
1885
+ var h = window.getComputedStyle(self.legend.element).height;
1886
+ self.legend.element.style.height = h;
1887
+ } );
1888
+ };
1889
+ Rickshaw.namespace('Rickshaw.Graph.Behavior.Series.Toggle');
1890
+
1891
+ Rickshaw.Graph.Behavior.Series.Toggle = function(args) {
1892
+
1893
+ this.graph = args.graph;
1894
+ this.legend = args.legend;
1895
+
1896
+ var self = this;
1897
+
1898
+ this.addAnchor = function(line) {
1899
+ var anchor = document.createElement('a');
1900
+ anchor.innerHTML = '&#10004;';
1901
+ anchor.classList.add('action');
1902
+ line.element.insertBefore(anchor, line.element.firstChild);
1903
+
1904
+ anchor.onclick = function(e) {
1905
+ if (line.series.disabled) {
1906
+ line.series.enable();
1907
+ line.element.classList.remove('disabled');
1908
+ } else {
1909
+ if (this.graph.series.filter(function(s) { return !s.disabled }).length <= 1) return;
1910
+ line.series.disable();
1911
+ line.element.classList.add('disabled');
1912
+ }
1913
+
1914
+ }.bind(this);
1915
+
1916
+ var label = line.element.getElementsByTagName('span')[0];
1917
+ label.onclick = function(e){
1918
+
1919
+ var disableAllOtherLines = line.series.disabled;
1920
+ if ( ! disableAllOtherLines ) {
1921
+ for ( var i = 0; i < self.legend.lines.length; i++ ) {
1922
+ var l = self.legend.lines[i];
1923
+ if ( line.series === l.series ) {
1924
+ // noop
1925
+ } else if ( l.series.disabled ) {
1926
+ // noop
1927
+ } else {
1928
+ disableAllOtherLines = true;
1929
+ break;
1930
+ }
1931
+ }
1932
+ }
1933
+
1934
+ // show all or none
1935
+ if ( disableAllOtherLines ) {
1936
+
1937
+ // these must happen first or else we try ( and probably fail ) to make a no line graph
1938
+ line.series.enable();
1939
+ line.element.classList.remove('disabled');
1940
+
1941
+ self.legend.lines.forEach(function(l){
1942
+ if ( line.series === l.series ) {
1943
+ // noop
1944
+ } else {
1945
+ l.series.disable();
1946
+ l.element.classList.add('disabled');
1947
+ }
1948
+ });
1949
+
1950
+ } else {
1951
+
1952
+ self.legend.lines.forEach(function(l){
1953
+ l.series.enable();
1954
+ l.element.classList.remove('disabled');
1955
+ });
1956
+
1957
+ }
1958
+
1959
+ };
1960
+
1961
+ };
1962
+
1963
+ if (this.legend) {
1964
+
1965
+ if (typeof $ != 'undefined' && $(this.legend.list).sortable) {
1966
+
1967
+ $(this.legend.list).sortable( {
1968
+ start: function(event, ui) {
1969
+ ui.item.bind('no.onclick',
1970
+ function(event) {
1971
+ event.preventDefault();
1972
+ }
1973
+ );
1974
+ },
1975
+ stop: function(event, ui) {
1976
+ setTimeout(function(){
1977
+ ui.item.unbind('no.onclick');
1978
+ }, 250);
1979
+ }
1980
+ });
1981
+ }
1982
+
1983
+ this.legend.lines.forEach( function(l) {
1984
+ self.addAnchor(l);
1985
+ } );
1986
+ }
1987
+
1988
+ this._addBehavior = function() {
1989
+
1990
+ this.graph.series.forEach( function(s) {
1991
+
1992
+ s.disable = function() {
1993
+
1994
+ if (self.graph.series.length <= 1) {
1995
+ throw('only one series left');
1996
+ }
1997
+
1998
+ s.disabled = true;
1999
+ self.graph.update();
2000
+ };
2001
+
2002
+ s.enable = function() {
2003
+ s.disabled = false;
2004
+ self.graph.update();
2005
+ };
2006
+ } );
2007
+ };
2008
+ this._addBehavior();
2009
+
2010
+ this.updateBehaviour = function () { this._addBehavior() };
2011
+
2012
+ };
2013
+ Rickshaw.namespace('Rickshaw.Graph.HoverDetail');
2014
+
2015
+ Rickshaw.Graph.HoverDetail = Rickshaw.Class.create({
2016
+
2017
+ initialize: function(args) {
2018
+
2019
+ var graph = this.graph = args.graph;
2020
+
2021
+ this.xFormatter = args.xFormatter || function(x) {
2022
+ return new Date( x * 1000 ).toUTCString();
2023
+ };
2024
+
2025
+ this.yFormatter = args.yFormatter || function(y) {
2026
+ return y === null ? y : y.toFixed(2);
2027
+ };
2028
+
2029
+ var element = this.element = document.createElement('div');
2030
+ element.className = 'detail';
2031
+
2032
+ this.visible = true;
2033
+ graph.element.appendChild(element);
2034
+
2035
+ this.lastEvent = null;
2036
+ this._addListeners();
2037
+
2038
+ this.onShow = args.onShow;
2039
+ this.onHide = args.onHide;
2040
+ this.onRender = args.onRender;
2041
+
2042
+ this.formatter = args.formatter || this.formatter;
2043
+
2044
+ },
2045
+
2046
+ formatter: function(series, x, y, formattedX, formattedY, d) {
2047
+ return series.name + ':&nbsp;' + formattedY;
2048
+ },
2049
+
2050
+ update: function(e) {
2051
+
2052
+ e = e || this.lastEvent;
2053
+ if (!e) return;
2054
+ this.lastEvent = e;
2055
+
2056
+ if (!e.target.nodeName.match(/^(path|svg|rect|circle)$/)) return;
2057
+
2058
+ var graph = this.graph;
2059
+
2060
+ var eventX = e.offsetX || e.layerX;
2061
+ var eventY = e.offsetY || e.layerY;
2062
+
2063
+ var j = 0;
2064
+ var points = [];
2065
+ var nearestPoint;
2066
+
2067
+ this.graph.series.active().forEach( function(series) {
2068
+
2069
+ var data = this.graph.stackedData[j++];
2070
+
2071
+ if (!data.length)
2072
+ return;
2073
+
2074
+ var domainX = graph.x.invert(eventX);
2075
+
2076
+ var domainIndexScale = d3.scale.linear()
2077
+ .domain([data[0].x, data.slice(-1)[0].x])
2078
+ .range([0, data.length - 1]);
2079
+
2080
+ var approximateIndex = Math.round(domainIndexScale(domainX));
2081
+ if (approximateIndex == data.length - 1) approximateIndex--;
2082
+
2083
+ var dataIndex = Math.min(approximateIndex || 0, data.length - 1);
2084
+
2085
+ for (var i = approximateIndex; i < data.length - 1;) {
2086
+
2087
+ if (!data[i] || !data[i + 1]) break;
2088
+
2089
+ if (data[i].x <= domainX && data[i + 1].x > domainX) {
2090
+ dataIndex = Math.abs(domainX - data[i].x) < Math.abs(domainX - data[i + 1].x) ? i : i + 1;
2091
+ break;
2092
+ }
2093
+
2094
+ if (data[i + 1].x <= domainX) { i++ } else { i-- }
2095
+ }
2096
+
2097
+ if (dataIndex < 0) dataIndex = 0;
2098
+ var value = data[dataIndex];
2099
+
2100
+ var distance = Math.sqrt(
2101
+ Math.pow(Math.abs(graph.x(value.x) - eventX), 2) +
2102
+ Math.pow(Math.abs(graph.y(value.y + value.y0) - eventY), 2)
2103
+ );
2104
+
2105
+ var xFormatter = series.xFormatter || this.xFormatter;
2106
+ var yFormatter = series.yFormatter || this.yFormatter;
2107
+
2108
+ var point = {
2109
+ formattedXValue: xFormatter(value.x),
2110
+ formattedYValue: yFormatter(series.scale ? series.scale.invert(value.y) : value.y),
2111
+ series: series,
2112
+ value: value,
2113
+ distance: distance,
2114
+ order: j,
2115
+ name: series.name
2116
+ };
2117
+
2118
+ if (!nearestPoint || distance < nearestPoint.distance) {
2119
+ nearestPoint = point;
2120
+ }
2121
+
2122
+ points.push(point);
2123
+
2124
+ }, this );
2125
+
2126
+ if (!nearestPoint)
2127
+ return;
2128
+
2129
+ nearestPoint.active = true;
2130
+
2131
+ var domainX = nearestPoint.value.x;
2132
+ var formattedXValue = nearestPoint.formattedXValue;
2133
+
2134
+ this.element.innerHTML = '';
2135
+ this.element.style.left = graph.x(domainX) + 'px';
2136
+
2137
+ this.visible && this.render( {
2138
+ points: points,
2139
+ detail: points, // for backwards compatibility
2140
+ mouseX: eventX,
2141
+ mouseY: eventY,
2142
+ formattedXValue: formattedXValue,
2143
+ domainX: domainX
2144
+ } );
2145
+ },
2146
+
2147
+ hide: function() {
2148
+ this.visible = false;
2149
+ this.element.classList.add('inactive');
2150
+
2151
+ if (typeof this.onHide == 'function') {
2152
+ this.onHide();
2153
+ }
2154
+ },
2155
+
2156
+ show: function() {
2157
+ this.visible = true;
2158
+ this.element.classList.remove('inactive');
2159
+
2160
+ if (typeof this.onShow == 'function') {
2161
+ this.onShow();
2162
+ }
2163
+ },
2164
+
2165
+ render: function(args) {
2166
+
2167
+ var graph = this.graph;
2168
+ var points = args.points;
2169
+ var point = points.filter( function(p) { return p.active } ).shift();
2170
+
2171
+ if (point.value.y === null) return;
2172
+
2173
+ var formattedXValue = point.formattedXValue;
2174
+ var formattedYValue = point.formattedYValue;
2175
+
2176
+ this.element.innerHTML = '';
2177
+ this.element.style.left = graph.x(point.value.x) + 'px';
2178
+
2179
+ var xLabel = document.createElement('div');
2180
+
2181
+ xLabel.className = 'x_label';
2182
+ xLabel.innerHTML = formattedXValue;
2183
+ this.element.appendChild(xLabel);
2184
+
2185
+ var item = document.createElement('div');
2186
+
2187
+ item.className = 'item';
2188
+
2189
+ // invert the scale if this series displays using a scale
2190
+ var series = point.series;
2191
+ var actualY = series.scale ? series.scale.invert(point.value.y) : point.value.y;
2192
+
2193
+ item.innerHTML = this.formatter(series, point.value.x, actualY, formattedXValue, formattedYValue, point);
2194
+ item.style.top = this.graph.y(point.value.y0 + point.value.y) + 'px';
2195
+
2196
+ this.element.appendChild(item);
2197
+
2198
+ var dot = document.createElement('div');
2199
+
2200
+ dot.className = 'dot';
2201
+ dot.style.top = item.style.top;
2202
+ dot.style.borderColor = series.color;
2203
+
2204
+ this.element.appendChild(dot);
2205
+
2206
+ if (point.active) {
2207
+ item.classList.add('active');
2208
+ dot.classList.add('active');
2209
+ }
2210
+
2211
+ // Assume left alignment until the element has been displayed and
2212
+ // bounding box calculations are possible.
2213
+ var alignables = [xLabel, item];
2214
+ alignables.forEach(function(el) {
2215
+ el.classList.add('left');
2216
+ });
2217
+
2218
+ this.show();
2219
+
2220
+ // If left-alignment results in any error, try right-alignment.
2221
+ var leftAlignError = this._calcLayoutError(alignables);
2222
+ if (leftAlignError > 0) {
2223
+ alignables.forEach(function(el) {
2224
+ el.classList.remove('left');
2225
+ el.classList.add('right');
2226
+ });
2227
+
2228
+ // If right-alignment is worse than left alignment, switch back.
2229
+ var rightAlignError = this._calcLayoutError(alignables);
2230
+ if (rightAlignError > leftAlignError) {
2231
+ alignables.forEach(function(el) {
2232
+ el.classList.remove('right');
2233
+ el.classList.add('left');
2234
+ });
2235
+ }
2236
+ }
2237
+
2238
+ if (typeof this.onRender == 'function') {
2239
+ this.onRender(args);
2240
+ }
2241
+ },
2242
+
2243
+ _calcLayoutError: function(alignables) {
2244
+ // Layout error is calculated as the number of linear pixels by which
2245
+ // an alignable extends past the left or right edge of the parent.
2246
+ var parentRect = this.element.parentNode.getBoundingClientRect();
2247
+
2248
+ var error = 0;
2249
+ var alignRight = alignables.forEach(function(el) {
2250
+ var rect = el.getBoundingClientRect();
2251
+ if (!rect.width) {
2252
+ return;
2253
+ }
2254
+
2255
+ if (rect.right > parentRect.right) {
2256
+ error += rect.right - parentRect.right;
2257
+ }
2258
+
2259
+ if (rect.left < parentRect.left) {
2260
+ error += parentRect.left - rect.left;
2261
+ }
2262
+ });
2263
+ return error;
2264
+ },
2265
+
2266
+ _addListeners: function() {
2267
+
2268
+ this.graph.element.addEventListener(
2269
+ 'mousemove',
2270
+ function(e) {
2271
+ this.visible = true;
2272
+ this.update(e);
2273
+ }.bind(this),
2274
+ false
2275
+ );
2276
+
2277
+ this.graph.onUpdate( function() { this.update() }.bind(this) );
2278
+
2279
+ this.graph.element.addEventListener(
2280
+ 'mouseout',
2281
+ function(e) {
2282
+ if (e.relatedTarget && !(e.relatedTarget.compareDocumentPosition(this.graph.element) & Node.DOCUMENT_POSITION_CONTAINS)) {
2283
+ this.hide();
2284
+ }
2285
+ }.bind(this),
2286
+ false
2287
+ );
2288
+ }
2289
+ });
2290
+ Rickshaw.namespace('Rickshaw.Graph.JSONP');
2291
+
2292
+ Rickshaw.Graph.JSONP = Rickshaw.Class.create( Rickshaw.Graph.Ajax, {
2293
+
2294
+ request: function() {
2295
+
2296
+ $.ajax( {
2297
+ url: this.dataURL,
2298
+ dataType: 'jsonp',
2299
+ success: this.success.bind(this),
2300
+ error: this.error.bind(this)
2301
+ } );
2302
+ }
2303
+ } );
2304
+ Rickshaw.namespace('Rickshaw.Graph.Legend');
2305
+
2306
+ Rickshaw.Graph.Legend = Rickshaw.Class.create( {
2307
+
2308
+ className: 'rickshaw_legend',
2309
+
2310
+ initialize: function(args) {
2311
+ this.element = args.element;
2312
+ this.graph = args.graph;
2313
+ this.naturalOrder = args.naturalOrder;
2314
+
2315
+ this.element.classList.add(this.className);
2316
+
2317
+ this.list = document.createElement('ul');
2318
+ this.element.appendChild(this.list);
2319
+
2320
+ this.render();
2321
+
2322
+ // we could bind this.render.bind(this) here
2323
+ // but triggering the re-render would lose the added
2324
+ // behavior of the series toggle
2325
+ this.graph.onUpdate( function() {} );
2326
+ },
2327
+
2328
+ render: function() {
2329
+ var self = this;
2330
+
2331
+ while ( this.list.firstChild ) {
2332
+ this.list.removeChild( this.list.firstChild );
2333
+ }
2334
+ this.lines = [];
2335
+
2336
+ var series = this.graph.series
2337
+ .map( function(s) { return s } );
2338
+
2339
+ if (!this.naturalOrder) {
2340
+ series = series.reverse();
2341
+ }
2342
+
2343
+ series.forEach( function(s) {
2344
+ self.addLine(s);
2345
+ } );
2346
+
2347
+
2348
+ },
2349
+
2350
+ addLine: function (series) {
2351
+ var line = document.createElement('li');
2352
+ line.className = 'line';
2353
+ if (series.disabled) {
2354
+ line.className += ' disabled';
2355
+ }
2356
+ if (series.className) {
2357
+ d3.select(line).classed(series.className, true);
2358
+ }
2359
+ var swatch = document.createElement('div');
2360
+ swatch.className = 'swatch';
2361
+ swatch.style.backgroundColor = series.color;
2362
+
2363
+ line.appendChild(swatch);
2364
+
2365
+ var label = document.createElement('span');
2366
+ label.className = 'label';
2367
+ label.innerHTML = series.name;
2368
+
2369
+ line.appendChild(label);
2370
+ this.list.appendChild(line);
2371
+
2372
+ line.series = series;
2373
+
2374
+ if (series.noLegend) {
2375
+ line.style.display = 'none';
2376
+ }
2377
+
2378
+ var _line = { element: line, series: series };
2379
+ if (this.shelving) {
2380
+ this.shelving.addAnchor(_line);
2381
+ this.shelving.updateBehaviour();
2382
+ }
2383
+ if (this.highlighter) {
2384
+ this.highlighter.addHighlightEvents(_line);
2385
+ }
2386
+ this.lines.push(_line);
2387
+ return line;
2388
+ }
2389
+ } );
2390
+
2391
+ Rickshaw.namespace('Rickshaw.Graph.RangeSlider');
2392
+
2393
+ Rickshaw.Graph.RangeSlider = Rickshaw.Class.create({
2394
+
2395
+ initialize: function(args) {
2396
+
2397
+ var element = this.element = args.element;
2398
+ var graph = this.graph = args.graph;
2399
+
2400
+ this.build();
2401
+
2402
+ graph.onUpdate( function() { this.update() }.bind(this) );
2403
+ },
2404
+
2405
+ build: function() {
2406
+
2407
+ var element = this.element;
2408
+ var graph = this.graph;
2409
+
2410
+ var domain = graph.dataDomain();
2411
+
2412
+ $( function() {
2413
+ $(element).slider( {
2414
+ range: true,
2415
+ min: domain[0],
2416
+ max: domain[1],
2417
+ values: [
2418
+ domain[0],
2419
+ domain[1]
2420
+ ],
2421
+ slide: function( event, ui ) {
2422
+
2423
+ if (ui.values[1] <= ui.values[0]) return;
2424
+
2425
+ graph.window.xMin = ui.values[0];
2426
+ graph.window.xMax = ui.values[1];
2427
+ graph.update();
2428
+
2429
+ var domain = graph.dataDomain();
2430
+
2431
+ // if we're at an extreme, stick there
2432
+ if (domain[0] == ui.values[0]) {
2433
+ graph.window.xMin = undefined;
2434
+ }
2435
+ if (domain[1] == ui.values[1]) {
2436
+ graph.window.xMax = undefined;
2437
+ }
2438
+ }
2439
+ } );
2440
+ } );
2441
+
2442
+ $(element)[0].style.width = graph.width + 'px';
2443
+
2444
+ },
2445
+
2446
+ update: function() {
2447
+
2448
+ var element = this.element;
2449
+ var graph = this.graph;
2450
+
2451
+ var values = $(element).slider('option', 'values');
2452
+
2453
+ var domain = graph.dataDomain();
2454
+
2455
+ $(element).slider('option', 'min', domain[0]);
2456
+ $(element).slider('option', 'max', domain[1]);
2457
+
2458
+ if (graph.window.xMin == null) {
2459
+ values[0] = domain[0];
2460
+ }
2461
+ if (graph.window.xMax == null) {
2462
+ values[1] = domain[1];
2463
+ }
2464
+
2465
+ $(element).slider('option', 'values', values);
2466
+ }
2467
+ });
2468
+
2469
+ Rickshaw.namespace('Rickshaw.Graph.RangeSlider.Preview');
2470
+
2471
+ Rickshaw.Graph.RangeSlider.Preview = Rickshaw.Class.create({
2472
+
2473
+ initialize: function(args) {
2474
+
2475
+ if (!args.element) throw "Rickshaw.Graph.RangeSlider.Preview needs a reference to an element";
2476
+ if (!args.graph && !args.graphs) throw "Rickshaw.Graph.RangeSlider.Preview needs a reference to an graph or an array of graphs";
2477
+
2478
+ this.element = args.element;
2479
+ this.graphs = args.graph ? [ args.graph ] : args.graphs;
2480
+
2481
+ this.defaults = {
2482
+ height: 75,
2483
+ width: 400,
2484
+ gripperColor: undefined,
2485
+ frameTopThickness: 3,
2486
+ frameHandleThickness: 10,
2487
+ frameColor: "#d4d4d4",
2488
+ frameOpacity: 1,
2489
+ minimumFrameWidth: 0
2490
+ };
2491
+
2492
+ this.defaults.gripperColor = d3.rgb(this.defaults.frameColor).darker().toString();
2493
+
2494
+ this.configureCallbacks = [];
2495
+ this.previews = [];
2496
+
2497
+ args.width = args.width || this.graphs[0].width || this.defaults.width;
2498
+ args.height = args.height || this.graphs[0].height / 5 || this.defaults.height;
2499
+
2500
+ this.configure(args);
2501
+ this.render();
2502
+ },
2503
+
2504
+ onConfigure: function(callback) {
2505
+ this.configureCallbacks.push(callback);
2506
+ },
2507
+
2508
+ configure: function(args) {
2509
+
2510
+ this.config = {};
2511
+
2512
+ this.configureCallbacks.forEach(function(callback) {
2513
+ callback(args);
2514
+ });
2515
+
2516
+ Rickshaw.keys(this.defaults).forEach(function(k) {
2517
+ this.config[k] = k in args ? args[k]
2518
+ : k in this.config ? this.config[k]
2519
+ : this.defaults[k];
2520
+ }, this);
2521
+
2522
+ if (args.width) {
2523
+ this.previews.forEach(function(preview) {
2524
+ var width = args.width - this.config.frameHandleThickness * 2;
2525
+ preview.setSize({ width: width });
2526
+ }, this);
2527
+ }
2528
+
2529
+ if (args.height) {
2530
+ this.previews.forEach(function(preview) {
2531
+ var height = this.previewHeight / this.graphs.length;
2532
+ preview.setSize({ height: height });
2533
+ }, this);
2534
+ }
2535
+ },
2536
+
2537
+ render: function() {
2538
+
2539
+ var self = this;
2540
+
2541
+ this.svg = d3.select(this.element)
2542
+ .selectAll("svg.rickshaw_range_slider_preview")
2543
+ .data([null]);
2544
+
2545
+ this.previewHeight = this.config.height - (this.config.frameTopThickness * 2);
2546
+ this.previewWidth = this.config.width - (this.config.frameHandleThickness * 2);
2547
+
2548
+ this.currentFrame = [0, this.previewWidth];
2549
+
2550
+ var buildGraph = function(parent, index) {
2551
+
2552
+ var graphArgs = Rickshaw.extend({}, parent.config);
2553
+ var height = self.previewHeight / self.graphs.length;
2554
+
2555
+ Rickshaw.extend(graphArgs, {
2556
+ element: this.appendChild(document.createElement("div")),
2557
+ height: height,
2558
+ width: self.previewWidth,
2559
+ series: parent.series
2560
+ });
2561
+
2562
+ var graph = new Rickshaw.Graph(graphArgs);
2563
+ self.previews.push(graph);
2564
+
2565
+ parent.onUpdate(function() { graph.render(); self.render() });
2566
+
2567
+ parent.onConfigure(function(args) {
2568
+ // don't propagate height
2569
+ delete args.height;
2570
+ graph.configure(args);
2571
+ graph.render();
2572
+ });
2573
+
2574
+ graph.render();
2575
+ };
2576
+
2577
+ var graphContainer = d3.select(this.element)
2578
+ .selectAll("div.rickshaw_range_slider_preview_container")
2579
+ .data(this.graphs);
2580
+
2581
+ var translateCommand = "translate(" +
2582
+ this.config.frameHandleThickness + "px, " +
2583
+ this.config.frameTopThickness + "px)";
2584
+
2585
+ graphContainer.enter()
2586
+ .append("div")
2587
+ .classed("rickshaw_range_slider_preview_container", true)
2588
+ .style("-webkit-transform", translateCommand)
2589
+ .style("-moz-transform", translateCommand)
2590
+ .style("-ms-transform", translateCommand)
2591
+ .style("transform", translateCommand)
2592
+ .each(buildGraph);
2593
+
2594
+ graphContainer.exit()
2595
+ .remove();
2596
+
2597
+ // Use the first graph as the "master" for the frame state
2598
+ var masterGraph = this.graphs[0];
2599
+
2600
+ var domainScale = d3.scale.linear()
2601
+ .domain([0, this.previewWidth])
2602
+ .range(masterGraph.dataDomain());
2603
+
2604
+ var currentWindow = [masterGraph.window.xMin, masterGraph.window.xMax];
2605
+
2606
+ this.currentFrame[0] = currentWindow[0] === undefined ?
2607
+ 0 : Math.round(domainScale.invert(currentWindow[0]));
2608
+
2609
+ if (this.currentFrame[0] < 0) this.currentFrame[0] = 0;
2610
+
2611
+ this.currentFrame[1] = currentWindow[1] === undefined ?
2612
+ this.previewWidth : domainScale.invert(currentWindow[1]);
2613
+
2614
+ if (this.currentFrame[1] - this.currentFrame[0] < self.config.minimumFrameWidth) {
2615
+ this.currentFrame[1] = (this.currentFrame[0] || 0) + self.config.minimumFrameWidth;
2616
+ }
2617
+
2618
+ this.svg.enter()
2619
+ .append("svg")
2620
+ .classed("rickshaw_range_slider_preview", true)
2621
+ .style("height", this.config.height + "px")
2622
+ .style("width", this.config.width + "px")
2623
+ .style("position", "relative")
2624
+ .style("top", -this.previewHeight + "px");
2625
+
2626
+ this._renderDimming();
2627
+ this._renderFrame();
2628
+ this._renderGrippers();
2629
+ this._renderHandles();
2630
+ this._renderMiddle();
2631
+
2632
+ this._registerMouseEvents();
2633
+ },
2634
+
2635
+ _renderDimming: function() {
2636
+
2637
+ var element = this.svg
2638
+ .selectAll("path.dimming")
2639
+ .data([null]);
2640
+
2641
+ element.enter()
2642
+ .append("path")
2643
+ .attr("fill", "white")
2644
+ .attr("fill-opacity", "0.7")
2645
+ .attr("fill-rule", "evenodd")
2646
+ .classed("dimming", true);
2647
+
2648
+ var path = "";
2649
+ path += " M " + this.config.frameHandleThickness + " " + this.config.frameTopThickness;
2650
+ path += " h " + this.previewWidth;
2651
+ path += " v " + this.previewHeight;
2652
+ path += " h " + -this.previewWidth;
2653
+ path += " z ";
2654
+ path += " M " + Math.max(this.currentFrame[0], this.config.frameHandleThickness) + " " + this.config.frameTopThickness;
2655
+ path += " H " + Math.min(this.currentFrame[1] + this.config.frameHandleThickness * 2, this.previewWidth + this.config.frameHandleThickness);
2656
+ path += " v " + this.previewHeight;
2657
+ path += " H " + Math.max(this.currentFrame[0], this.config.frameHandleThickness);
2658
+ path += " z";
2659
+
2660
+ element.attr("d", path);
2661
+ },
2662
+
2663
+ _renderFrame: function() {
2664
+
2665
+ var element = this.svg
2666
+ .selectAll("path.frame")
2667
+ .data([null]);
2668
+
2669
+ element.enter()
2670
+ .append("path")
2671
+ .attr("stroke", "white")
2672
+ .attr("stroke-width", "1px")
2673
+ .attr("stroke-linejoin", "round")
2674
+ .attr("fill", this.config.frameColor)
2675
+ .attr("fill-opacity", this.config.frameOpacity)
2676
+ .attr("fill-rule", "evenodd")
2677
+ .classed("frame", true);
2678
+
2679
+ var path = "";
2680
+ path += " M " + this.currentFrame[0] + " 0";
2681
+ path += " H " + (this.currentFrame[1] + (this.config.frameHandleThickness * 2));
2682
+ path += " V " + this.config.height;
2683
+ path += " H " + (this.currentFrame[0]);
2684
+ path += " z";
2685
+ path += " M " + (this.currentFrame[0] + this.config.frameHandleThickness) + " " + this.config.frameTopThickness;
2686
+ path += " H " + (this.currentFrame[1] + this.config.frameHandleThickness);
2687
+ path += " v " + this.previewHeight;
2688
+ path += " H " + (this.currentFrame[0] + this.config.frameHandleThickness);
2689
+ path += " z";
2690
+
2691
+ element.attr("d", path);
2692
+ },
2693
+
2694
+ _renderGrippers: function() {
2695
+
2696
+ var gripper = this.svg.selectAll("path.gripper")
2697
+ .data([null]);
2698
+
2699
+ gripper.enter()
2700
+ .append("path")
2701
+ .attr("stroke", this.config.gripperColor)
2702
+ .classed("gripper", true);
2703
+
2704
+ var path = "";
2705
+
2706
+ [0.4, 0.6].forEach(function(spacing) {
2707
+ path += " M " + Math.round((this.currentFrame[0] + (this.config.frameHandleThickness * spacing))) + " " + Math.round(this.config.height * 0.3);
2708
+ path += " V " + Math.round(this.config.height * 0.7);
2709
+ path += " M " + Math.round((this.currentFrame[1] + (this.config.frameHandleThickness * (1 + spacing)))) + " " + Math.round(this.config.height * 0.3);
2710
+ path += " V " + Math.round(this.config.height * 0.7);
2711
+ }.bind(this));
2712
+
2713
+ gripper.attr("d", path);
2714
+ },
2715
+
2716
+ _renderHandles: function() {
2717
+
2718
+ var leftHandle = this.svg.selectAll("rect.left_handle")
2719
+ .data([null]);
2720
+
2721
+ leftHandle.enter()
2722
+ .append("rect")
2723
+ .attr('width', this.config.frameHandleThickness)
2724
+ .attr('height', this.config.height)
2725
+ .style("cursor", "ew-resize")
2726
+ .style("fill-opacity", "0")
2727
+ .classed("left_handle", true);
2728
+
2729
+ leftHandle.attr('x', this.currentFrame[0]);
2730
+
2731
+ var rightHandle = this.svg.selectAll("rect.right_handle")
2732
+ .data([null]);
2733
+
2734
+ rightHandle.enter()
2735
+ .append("rect")
2736
+ .attr('width', this.config.frameHandleThickness)
2737
+ .attr('height', this.config.height)
2738
+ .style("cursor", "ew-resize")
2739
+ .style("fill-opacity", "0")
2740
+ .classed("right_handle", true);
2741
+
2742
+ rightHandle.attr('x', this.currentFrame[1] + this.config.frameHandleThickness);
2743
+ },
2744
+
2745
+ _renderMiddle: function() {
2746
+
2747
+ var middleHandle = this.svg.selectAll("rect.middle_handle")
2748
+ .data([null]);
2749
+
2750
+ middleHandle.enter()
2751
+ .append("rect")
2752
+ .attr('height', this.config.height)
2753
+ .style("cursor", "move")
2754
+ .style("fill-opacity", "0")
2755
+ .classed("middle_handle", true);
2756
+
2757
+ middleHandle
2758
+ .attr('width', Math.max(0, this.currentFrame[1] - this.currentFrame[0]))
2759
+ .attr('x', this.currentFrame[0] + this.config.frameHandleThickness);
2760
+ },
2761
+
2762
+ _registerMouseEvents: function() {
2763
+
2764
+ var element = d3.select(this.element);
2765
+
2766
+ var drag = {
2767
+ target: null,
2768
+ start: null,
2769
+ stop: null,
2770
+ left: false,
2771
+ right: false,
2772
+ rigid: false
2773
+ };
2774
+
2775
+ var self = this;
2776
+
2777
+ function onMousemove(datum, index) {
2778
+
2779
+ drag.stop = self._getClientXFromEvent(d3.event, drag);
2780
+ var distanceTraveled = drag.stop - drag.start;
2781
+ var frameAfterDrag = self.frameBeforeDrag.slice(0);
2782
+ var minimumFrameWidth = self.config.minimumFrameWidth;
2783
+
2784
+ if (drag.rigid) {
2785
+ minimumFrameWidth = self.frameBeforeDrag[1] - self.frameBeforeDrag[0];
2786
+ }
2787
+ if (drag.left) {
2788
+ frameAfterDrag[0] = Math.max(frameAfterDrag[0] + distanceTraveled, 0);
2789
+ }
2790
+ if (drag.right) {
2791
+ frameAfterDrag[1] = Math.min(frameAfterDrag[1] + distanceTraveled, self.previewWidth);
2792
+ }
2793
+
2794
+ var currentFrameWidth = frameAfterDrag[1] - frameAfterDrag[0];
2795
+
2796
+ if (currentFrameWidth <= minimumFrameWidth) {
2797
+
2798
+ if (drag.left) {
2799
+ frameAfterDrag[0] = frameAfterDrag[1] - minimumFrameWidth;
2800
+ }
2801
+ if (drag.right) {
2802
+ frameAfterDrag[1] = frameAfterDrag[0] + minimumFrameWidth;
2803
+ }
2804
+ if (frameAfterDrag[0] <= 0) {
2805
+ frameAfterDrag[1] -= frameAfterDrag[0];
2806
+ frameAfterDrag[0] = 0;
2807
+ }
2808
+ if (frameAfterDrag[1] >= self.previewWidth) {
2809
+ frameAfterDrag[0] -= (frameAfterDrag[1] - self.previewWidth);
2810
+ frameAfterDrag[1] = self.previewWidth;
2811
+ }
2812
+ }
2813
+
2814
+ self.graphs.forEach(function(graph) {
2815
+
2816
+ var domainScale = d3.scale.linear()
2817
+ .interpolate(d3.interpolateRound)
2818
+ .domain([0, self.previewWidth])
2819
+ .range(graph.dataDomain());
2820
+
2821
+ var windowAfterDrag = [
2822
+ domainScale(frameAfterDrag[0]),
2823
+ domainScale(frameAfterDrag[1])
2824
+ ];
2825
+
2826
+ if (frameAfterDrag[0] === 0) {
2827
+ windowAfterDrag[0] = undefined;
2828
+ }
2829
+ if (frameAfterDrag[1] === self.previewWidth) {
2830
+ windowAfterDrag[1] = undefined;
2831
+ }
2832
+ graph.window.xMin = windowAfterDrag[0];
2833
+ graph.window.xMax = windowAfterDrag[1];
2834
+
2835
+ graph.update();
2836
+ });
2837
+ }
2838
+
2839
+ function onMousedown() {
2840
+ drag.target = d3.event.target;
2841
+ drag.start = self._getClientXFromEvent(d3.event, drag);
2842
+ self.frameBeforeDrag = self.currentFrame.slice();
2843
+ d3.event.preventDefault ? d3.event.preventDefault() : d3.event.returnValue = false;
2844
+ d3.select(document).on("mousemove.rickshaw_range_slider_preview", onMousemove);
2845
+ d3.select(document).on("mouseup.rickshaw_range_slider_preview", onMouseup);
2846
+ d3.select(document).on("touchmove.rickshaw_range_slider_preview", onMousemove);
2847
+ d3.select(document).on("touchend.rickshaw_range_slider_preview", onMouseup);
2848
+ d3.select(document).on("touchcancel.rickshaw_range_slider_preview", onMouseup);
2849
+ }
2850
+
2851
+ function onMousedownLeftHandle(datum, index) {
2852
+ drag.left = true;
2853
+ onMousedown();
2854
+ }
2855
+
2856
+ function onMousedownRightHandle(datum, index) {
2857
+ drag.right = true;
2858
+ onMousedown();
2859
+ }
2860
+
2861
+ function onMousedownMiddleHandle(datum, index) {
2862
+ drag.left = true;
2863
+ drag.right = true;
2864
+ drag.rigid = true;
2865
+ onMousedown();
2866
+ }
2867
+
2868
+ function onMouseup(datum, index) {
2869
+ d3.select(document).on("mousemove.rickshaw_range_slider_preview", null);
2870
+ d3.select(document).on("mouseup.rickshaw_range_slider_preview", null);
2871
+ d3.select(document).on("touchmove.rickshaw_range_slider_preview", null);
2872
+ d3.select(document).on("touchend.rickshaw_range_slider_preview", null);
2873
+ d3.select(document).on("touchcancel.rickshaw_range_slider_preview", null);
2874
+ delete self.frameBeforeDrag;
2875
+ drag.left = false;
2876
+ drag.right = false;
2877
+ drag.rigid = false;
2878
+ }
2879
+
2880
+ element.select("rect.left_handle").on("mousedown", onMousedownLeftHandle);
2881
+ element.select("rect.right_handle").on("mousedown", onMousedownRightHandle);
2882
+ element.select("rect.middle_handle").on("mousedown", onMousedownMiddleHandle);
2883
+ element.select("rect.left_handle").on("touchstart", onMousedownLeftHandle);
2884
+ element.select("rect.right_handle").on("touchstart", onMousedownRightHandle);
2885
+ element.select("rect.middle_handle").on("touchstart", onMousedownMiddleHandle);
2886
+ },
2887
+
2888
+ _getClientXFromEvent: function(event, drag) {
2889
+
2890
+ switch (event.type) {
2891
+ case 'touchstart':
2892
+ case 'touchmove':
2893
+ var touchList = event.changedTouches;
2894
+ var touch = null;
2895
+ for (var touchIndex = 0; touchIndex < touchList.length; touchIndex++) {
2896
+ if (touchList[touchIndex].target === drag.target) {
2897
+ touch = touchList[touchIndex];
2898
+ break;
2899
+ }
2900
+ }
2901
+ return touch !== null ? touch.clientX : undefined;
2902
+
2903
+ default:
2904
+ return event.clientX;
2905
+ }
2906
+ }
2907
+ });
2908
+
2909
+ Rickshaw.namespace("Rickshaw.Graph.Renderer");
2910
+
2911
+ Rickshaw.Graph.Renderer = Rickshaw.Class.create( {
2912
+
2913
+ initialize: function(args) {
2914
+ this.graph = args.graph;
2915
+ this.tension = args.tension || this.tension;
2916
+ this.configure(args);
2917
+ },
2918
+
2919
+ seriesPathFactory: function() {
2920
+ //implement in subclass
2921
+ },
2922
+
2923
+ seriesStrokeFactory: function() {
2924
+ // implement in subclass
2925
+ },
2926
+
2927
+ defaults: function() {
2928
+ return {
2929
+ tension: 0.8,
2930
+ strokeWidth: 2,
2931
+ unstack: true,
2932
+ padding: { top: 0.01, right: 0, bottom: 0.01, left: 0 },
2933
+ stroke: false,
2934
+ fill: false
2935
+ };
2936
+ },
2937
+
2938
+ domain: function(data) {
2939
+
2940
+ var stackedData = data || this.graph.stackedData || this.graph.stackData();
2941
+ var firstPoint = stackedData[0][0];
2942
+
2943
+ if (firstPoint === undefined) {
2944
+ return { x: [null, null], y: [null, null] };
2945
+ }
2946
+
2947
+ var xMin = firstPoint.x;
2948
+ var xMax = firstPoint.x;
2949
+
2950
+ var yMin = firstPoint.y + firstPoint.y0;
2951
+ var yMax = firstPoint.y + firstPoint.y0;
2952
+
2953
+ stackedData.forEach( function(series) {
2954
+
2955
+ series.forEach( function(d) {
2956
+
2957
+ if (d.y == null) return;
2958
+
2959
+ var y = d.y + d.y0;
2960
+
2961
+ if (y < yMin) yMin = y;
2962
+ if (y > yMax) yMax = y;
2963
+ } );
2964
+
2965
+ if (!series.length) return;
2966
+
2967
+ if (series[0].x < xMin) xMin = series[0].x;
2968
+ if (series[series.length - 1].x > xMax) xMax = series[series.length - 1].x;
2969
+ } );
2970
+
2971
+ xMin -= (xMax - xMin) * this.padding.left;
2972
+ xMax += (xMax - xMin) * this.padding.right;
2973
+
2974
+ yMin = this.graph.min === 'auto' ? yMin : this.graph.min || 0;
2975
+ yMax = this.graph.max === undefined ? yMax : this.graph.max;
2976
+
2977
+ if (this.graph.min === 'auto' || yMin < 0) {
2978
+ yMin -= (yMax - yMin) * this.padding.bottom;
2979
+ }
2980
+
2981
+ if (this.graph.max === undefined) {
2982
+ yMax += (yMax - yMin) * this.padding.top;
2983
+ }
2984
+
2985
+ return { x: [xMin, xMax], y: [yMin, yMax] };
2986
+ },
2987
+
2988
+ render: function(args) {
2989
+
2990
+ args = args || {};
2991
+
2992
+ var graph = this.graph;
2993
+ var series = args.series || graph.series;
2994
+
2995
+ var vis = args.vis || graph.vis;
2996
+ vis.selectAll('*').remove();
2997
+
2998
+ var data = series
2999
+ .filter(function(s) { return !s.disabled })
3000
+ .map(function(s) { return s.stack });
3001
+
3002
+ var pathNodes = vis.selectAll("path.path")
3003
+ .data(data)
3004
+ .enter().append("svg:path")
3005
+ .classed('path', true)
3006
+ .attr("d", this.seriesPathFactory());
3007
+
3008
+ if (this.stroke) {
3009
+ var strokeNodes = vis.selectAll('path.stroke')
3010
+ .data(data)
3011
+ .enter().append("svg:path")
3012
+ .classed('stroke', true)
3013
+ .attr("d", this.seriesStrokeFactory());
3014
+ }
3015
+
3016
+ var i = 0;
3017
+ series.forEach( function(series) {
3018
+ if (series.disabled) return;
3019
+ series.path = pathNodes[0][i];
3020
+ if (this.stroke) series.stroke = strokeNodes[0][i];
3021
+ this._styleSeries(series);
3022
+ i++;
3023
+ }, this );
3024
+
3025
+ },
3026
+
3027
+ _styleSeries: function(series) {
3028
+
3029
+ var fill = this.fill ? series.color : 'none';
3030
+ var stroke = this.stroke ? series.color : 'none';
3031
+
3032
+ series.path.setAttribute('fill', fill);
3033
+ series.path.setAttribute('stroke', stroke);
3034
+ series.path.setAttribute('stroke-width', this.strokeWidth);
3035
+
3036
+ if (series.className) {
3037
+ d3.select(series.path).classed(series.className, true);
3038
+ }
3039
+ if (series.className && this.stroke) {
3040
+ d3.select(series.stroke).classed(series.className, true);
3041
+ }
3042
+ },
3043
+
3044
+ configure: function(args) {
3045
+
3046
+ args = args || {};
3047
+
3048
+ Rickshaw.keys(this.defaults()).forEach( function(key) {
3049
+
3050
+ if (!args.hasOwnProperty(key)) {
3051
+ this[key] = this[key] || this.graph[key] || this.defaults()[key];
3052
+ return;
3053
+ }
3054
+
3055
+ if (typeof this.defaults()[key] == 'object') {
3056
+
3057
+ Rickshaw.keys(this.defaults()[key]).forEach( function(k) {
3058
+
3059
+ this[key][k] =
3060
+ args[key][k] !== undefined ? args[key][k] :
3061
+ this[key][k] !== undefined ? this[key][k] :
3062
+ this.defaults()[key][k];
3063
+ }, this );
3064
+
3065
+ } else {
3066
+ this[key] =
3067
+ args[key] !== undefined ? args[key] :
3068
+ this[key] !== undefined ? this[key] :
3069
+ this.graph[key] !== undefined ? this.graph[key] :
3070
+ this.defaults()[key];
3071
+ }
3072
+
3073
+ }, this );
3074
+ },
3075
+
3076
+ setStrokeWidth: function(strokeWidth) {
3077
+ if (strokeWidth !== undefined) {
3078
+ this.strokeWidth = strokeWidth;
3079
+ }
3080
+ },
3081
+
3082
+ setTension: function(tension) {
3083
+ if (tension !== undefined) {
3084
+ this.tension = tension;
3085
+ }
3086
+ }
3087
+ } );
3088
+
3089
+ Rickshaw.namespace('Rickshaw.Graph.Renderer.Line');
3090
+
3091
+ Rickshaw.Graph.Renderer.Line = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {
3092
+
3093
+ name: 'line',
3094
+
3095
+ defaults: function($super) {
3096
+
3097
+ return Rickshaw.extend( $super(), {
3098
+ unstack: true,
3099
+ fill: false,
3100
+ stroke: true
3101
+ } );
3102
+ },
3103
+
3104
+ seriesPathFactory: function() {
3105
+
3106
+ var graph = this.graph;
3107
+
3108
+ var factory = d3.svg.line()
3109
+ .x( function(d) { return graph.x(d.x) } )
3110
+ .y( function(d) { return graph.y(d.y) } )
3111
+ .interpolate(this.graph.interpolation).tension(this.tension);
3112
+
3113
+ factory.defined && factory.defined( function(d) { return d.y !== null } );
3114
+ return factory;
3115
+ }
3116
+ } );
3117
+
3118
+ Rickshaw.namespace('Rickshaw.Graph.Renderer.Stack');
3119
+
3120
+ Rickshaw.Graph.Renderer.Stack = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {
3121
+
3122
+ name: 'stack',
3123
+
3124
+ defaults: function($super) {
3125
+
3126
+ return Rickshaw.extend( $super(), {
3127
+ fill: true,
3128
+ stroke: false,
3129
+ unstack: false
3130
+ } );
3131
+ },
3132
+
3133
+ seriesPathFactory: function() {
3134
+
3135
+ var graph = this.graph;
3136
+
3137
+ var factory = d3.svg.area()
3138
+ .x( function(d) { return graph.x(d.x) } )
3139
+ .y0( function(d) { return graph.y(d.y0) } )
3140
+ .y1( function(d) { return graph.y(d.y + d.y0) } )
3141
+ .interpolate(this.graph.interpolation).tension(this.tension);
3142
+
3143
+ factory.defined && factory.defined( function(d) { return d.y !== null } );
3144
+ return factory;
3145
+ }
3146
+ } );
3147
+
3148
+ Rickshaw.namespace('Rickshaw.Graph.Renderer.Bar');
3149
+
3150
+ Rickshaw.Graph.Renderer.Bar = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {
3151
+
3152
+ name: 'bar',
3153
+
3154
+ defaults: function($super) {
3155
+
3156
+ var defaults = Rickshaw.extend( $super(), {
3157
+ gapSize: 0.05,
3158
+ unstack: false
3159
+ } );
3160
+
3161
+ delete defaults.tension;
3162
+ return defaults;
3163
+ },
3164
+
3165
+ initialize: function($super, args) {
3166
+ args = args || {};
3167
+ this.gapSize = args.gapSize || this.gapSize;
3168
+ $super(args);
3169
+ },
3170
+
3171
+ domain: function($super) {
3172
+
3173
+ var domain = $super();
3174
+
3175
+ var frequentInterval = this._frequentInterval(this.graph.stackedData.slice(-1).shift());
3176
+ domain.x[1] += Number(frequentInterval.magnitude);
3177
+
3178
+ return domain;
3179
+ },
3180
+
3181
+ barWidth: function(series) {
3182
+
3183
+ var frequentInterval = this._frequentInterval(series.stack);
3184
+ var barWidth = this.graph.x(series.stack[0].x + frequentInterval.magnitude * (1 - this.gapSize));
3185
+
3186
+ return barWidth;
3187
+ },
3188
+
3189
+ render: function(args) {
3190
+
3191
+ args = args || {};
3192
+
3193
+ var graph = this.graph;
3194
+ var series = args.series || graph.series;
3195
+
3196
+ var vis = args.vis || graph.vis;
3197
+ vis.selectAll('*').remove();
3198
+
3199
+ var barWidth = this.barWidth(series.active()[0]);
3200
+ var barXOffset = 0;
3201
+
3202
+ var activeSeriesCount = series.filter( function(s) { return !s.disabled; } ).length;
3203
+ var seriesBarWidth = this.unstack ? barWidth / activeSeriesCount : barWidth;
3204
+
3205
+ var transform = function(d) {
3206
+ // add a matrix transform for negative values
3207
+ var matrix = [ 1, 0, 0, (d.y < 0 ? -1 : 1), 0, (d.y < 0 ? graph.y.magnitude(Math.abs(d.y)) * 2 : 0) ];
3208
+ return "matrix(" + matrix.join(',') + ")";
3209
+ };
3210
+
3211
+ series.forEach( function(series) {
3212
+
3213
+ if (series.disabled) return;
3214
+
3215
+ var barWidth = this.barWidth(series);
3216
+
3217
+ var nodes = vis.selectAll("path")
3218
+ .data(series.stack.filter( function(d) { return d.y !== null } ))
3219
+ .enter().append("svg:rect")
3220
+ .attr("x", function(d) { return graph.x(d.x) + barXOffset })
3221
+ .attr("y", function(d) { return (graph.y(d.y0 + Math.abs(d.y))) * (d.y < 0 ? -1 : 1 ) })
3222
+ .attr("width", seriesBarWidth)
3223
+ .attr("height", function(d) { return graph.y.magnitude(Math.abs(d.y)) })
3224
+ .attr("transform", transform);
3225
+
3226
+ Array.prototype.forEach.call(nodes[0], function(n) {
3227
+ n.setAttribute('fill', series.color);
3228
+ } );
3229
+
3230
+ if (this.unstack) barXOffset += seriesBarWidth;
3231
+
3232
+ }, this );
3233
+ },
3234
+
3235
+ _frequentInterval: function(data) {
3236
+
3237
+ var intervalCounts = {};
3238
+
3239
+ for (var i = 0; i < data.length - 1; i++) {
3240
+ var interval = data[i + 1].x - data[i].x;
3241
+ intervalCounts[interval] = intervalCounts[interval] || 0;
3242
+ intervalCounts[interval]++;
3243
+ }
3244
+
3245
+ var frequentInterval = { count: 0, magnitude: 1 };
3246
+
3247
+ Rickshaw.keys(intervalCounts).forEach( function(i) {
3248
+ if (frequentInterval.count < intervalCounts[i]) {
3249
+ frequentInterval = {
3250
+ count: intervalCounts[i],
3251
+ magnitude: i
3252
+ };
3253
+ }
3254
+ } );
3255
+
3256
+ return frequentInterval;
3257
+ }
3258
+ } );
3259
+
3260
+ Rickshaw.namespace('Rickshaw.Graph.Renderer.Area');
3261
+
3262
+ Rickshaw.Graph.Renderer.Area = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {
3263
+
3264
+ name: 'area',
3265
+
3266
+ defaults: function($super) {
3267
+
3268
+ return Rickshaw.extend( $super(), {
3269
+ unstack: false,
3270
+ fill: false,
3271
+ stroke: false
3272
+ } );
3273
+ },
3274
+
3275
+ seriesPathFactory: function() {
3276
+
3277
+ var graph = this.graph;
3278
+
3279
+ var factory = d3.svg.area()
3280
+ .x( function(d) { return graph.x(d.x) } )
3281
+ .y0( function(d) { return graph.y(d.y0) } )
3282
+ .y1( function(d) { return graph.y(d.y + d.y0) } )
3283
+ .interpolate(graph.interpolation).tension(this.tension);
3284
+
3285
+ factory.defined && factory.defined( function(d) { return d.y !== null } );
3286
+ return factory;
3287
+ },
3288
+
3289
+ seriesStrokeFactory: function() {
3290
+
3291
+ var graph = this.graph;
3292
+
3293
+ var factory = d3.svg.line()
3294
+ .x( function(d) { return graph.x(d.x) } )
3295
+ .y( function(d) { return graph.y(d.y + d.y0) } )
3296
+ .interpolate(graph.interpolation).tension(this.tension);
3297
+
3298
+ factory.defined && factory.defined( function(d) { return d.y !== null } );
3299
+ return factory;
3300
+ },
3301
+
3302
+ render: function(args) {
3303
+
3304
+ args = args || {};
3305
+
3306
+ var graph = this.graph;
3307
+ var series = args.series || graph.series;
3308
+
3309
+ var vis = args.vis || graph.vis;
3310
+ vis.selectAll('*').remove();
3311
+
3312
+ // insert or stacked areas so strokes lay on top of areas
3313
+ var method = this.unstack ? 'append' : 'insert';
3314
+
3315
+ var data = series
3316
+ .filter(function(s) { return !s.disabled })
3317
+ .map(function(s) { return s.stack });
3318
+
3319
+ var nodes = vis.selectAll("path")
3320
+ .data(data)
3321
+ .enter()[method]("svg:g", 'g');
3322
+
3323
+ nodes.append("svg:path")
3324
+ .attr("d", this.seriesPathFactory())
3325
+ .attr("class", 'area');
3326
+
3327
+ if (this.stroke) {
3328
+ nodes.append("svg:path")
3329
+ .attr("d", this.seriesStrokeFactory())
3330
+ .attr("class", 'line');
3331
+ }
3332
+
3333
+ var i = 0;
3334
+ series.forEach( function(series) {
3335
+ if (series.disabled) return;
3336
+ series.path = nodes[0][i++];
3337
+ this._styleSeries(series);
3338
+ }, this );
3339
+ },
3340
+
3341
+ _styleSeries: function(series) {
3342
+
3343
+ if (!series.path) return;
3344
+
3345
+ d3.select(series.path).select('.area')
3346
+ .attr('fill', series.color);
3347
+
3348
+ if (this.stroke) {
3349
+ d3.select(series.path).select('.line')
3350
+ .attr('fill', 'none')
3351
+ .attr('stroke', series.stroke || d3.interpolateRgb(series.color, 'black')(0.125))
3352
+ .attr('stroke-width', this.strokeWidth);
3353
+ }
3354
+
3355
+ if (series.className) {
3356
+ series.path.setAttribute('class', series.className);
3357
+ }
3358
+ }
3359
+ } );
3360
+
3361
+ Rickshaw.namespace('Rickshaw.Graph.Renderer.ScatterPlot');
3362
+
3363
+ Rickshaw.Graph.Renderer.ScatterPlot = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {
3364
+
3365
+ name: 'scatterplot',
3366
+
3367
+ defaults: function($super) {
3368
+
3369
+ return Rickshaw.extend( $super(), {
3370
+ unstack: true,
3371
+ fill: true,
3372
+ stroke: false,
3373
+ padding:{ top: 0.01, right: 0.01, bottom: 0.01, left: 0.01 },
3374
+ dotSize: 4
3375
+ } );
3376
+ },
3377
+
3378
+ initialize: function($super, args) {
3379
+ $super(args);
3380
+ },
3381
+
3382
+ render: function(args) {
3383
+
3384
+ args = args || {};
3385
+
3386
+ var graph = this.graph;
3387
+
3388
+ var series = args.series || graph.series;
3389
+ var vis = args.vis || graph.vis;
3390
+
3391
+ var dotSize = this.dotSize;
3392
+
3393
+ vis.selectAll('*').remove();
3394
+
3395
+ series.forEach( function(series) {
3396
+
3397
+ if (series.disabled) return;
3398
+
3399
+ var nodes = vis.selectAll("path")
3400
+ .data(series.stack.filter( function(d) { return d.y !== null } ))
3401
+ .enter().append("svg:circle")
3402
+ .attr("cx", function(d) { return graph.x(d.x) })
3403
+ .attr("cy", function(d) { return graph.y(d.y) })
3404
+ .attr("r", function(d) { return ("r" in d) ? d.r : dotSize});
3405
+ if (series.className) {
3406
+ nodes.classed(series.className, true);
3407
+ }
3408
+
3409
+ Array.prototype.forEach.call(nodes[0], function(n) {
3410
+ n.setAttribute('fill', series.color);
3411
+ } );
3412
+
3413
+ }, this );
3414
+ }
3415
+ } );
3416
+ Rickshaw.namespace('Rickshaw.Graph.Renderer.Multi');
3417
+
3418
+ Rickshaw.Graph.Renderer.Multi = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {
3419
+
3420
+ name: 'multi',
3421
+
3422
+ initialize: function($super, args) {
3423
+
3424
+ $super(args);
3425
+ },
3426
+
3427
+ defaults: function($super) {
3428
+
3429
+ return Rickshaw.extend( $super(), {
3430
+ unstack: true,
3431
+ fill: false,
3432
+ stroke: true
3433
+ } );
3434
+ },
3435
+
3436
+ configure: function($super, args) {
3437
+
3438
+ args = args || {};
3439
+ this.config = args;
3440
+ $super(args);
3441
+ },
3442
+
3443
+ domain: function($super) {
3444
+
3445
+ this.graph.stackData();
3446
+
3447
+ var domains = [];
3448
+
3449
+ var groups = this._groups();
3450
+ this._stack(groups);
3451
+
3452
+ groups.forEach( function(group) {
3453
+
3454
+ var data = group.series
3455
+ .filter( function(s) { return !s.disabled } )
3456
+ .map( function(s) { return s.stack });
3457
+
3458
+ if (!data.length) return;
3459
+
3460
+ var domain = $super(data);
3461
+ domains.push(domain);
3462
+ });
3463
+
3464
+ var xMin = d3.min(domains.map( function(d) { return d.x[0] } ));
3465
+ var xMax = d3.max(domains.map( function(d) { return d.x[1] } ));
3466
+ var yMin = d3.min(domains.map( function(d) { return d.y[0] } ));
3467
+ var yMax = d3.max(domains.map( function(d) { return d.y[1] } ));
3468
+
3469
+ return { x: [xMin, xMax], y: [yMin, yMax] };
3470
+ },
3471
+
3472
+ _groups: function() {
3473
+
3474
+ var graph = this.graph;
3475
+
3476
+ var renderGroups = {};
3477
+
3478
+ graph.series.forEach( function(series) {
3479
+
3480
+ if (series.disabled) return;
3481
+
3482
+ if (!renderGroups[series.renderer]) {
3483
+
3484
+ var ns = "http://www.w3.org/2000/svg";
3485
+ var vis = document.createElementNS(ns, 'g');
3486
+
3487
+ graph.vis[0][0].appendChild(vis);
3488
+
3489
+ var renderer = graph._renderers[series.renderer];
3490
+
3491
+ var config = {};
3492
+
3493
+ var defaults = [ this.defaults(), renderer.defaults(), this.config, this.graph ];
3494
+ defaults.forEach(function(d) { Rickshaw.extend(config, d) });
3495
+
3496
+ renderer.configure(config);
3497
+
3498
+ renderGroups[series.renderer] = {
3499
+ renderer: renderer,
3500
+ series: [],
3501
+ vis: d3.select(vis)
3502
+ };
3503
+ }
3504
+
3505
+ renderGroups[series.renderer].series.push(series);
3506
+
3507
+ }, this);
3508
+
3509
+ var groups = [];
3510
+
3511
+ Object.keys(renderGroups).forEach( function(key) {
3512
+ var group = renderGroups[key];
3513
+ groups.push(group);
3514
+ });
3515
+
3516
+ return groups;
3517
+ },
3518
+
3519
+ _stack: function(groups) {
3520
+
3521
+ groups.forEach( function(group) {
3522
+
3523
+ var series = group.series
3524
+ .filter( function(series) { return !series.disabled } );
3525
+
3526
+ var data = series
3527
+ .map( function(series) { return series.stack } );
3528
+
3529
+ if (!group.renderer.unstack) {
3530
+
3531
+ var layout = d3.layout.stack();
3532
+ var stackedData = Rickshaw.clone(layout(data));
3533
+
3534
+ series.forEach( function(series, index) {
3535
+ series._stack = Rickshaw.clone(stackedData[index]);
3536
+ });
3537
+ }
3538
+
3539
+ }, this );
3540
+
3541
+ return groups;
3542
+
3543
+ },
3544
+
3545
+ render: function() {
3546
+
3547
+ this.graph.series.forEach( function(series) {
3548
+ if (!series.renderer) {
3549
+ throw new Error("Each series needs a renderer for graph 'multi' renderer");
3550
+ }
3551
+ });
3552
+
3553
+ this.graph.vis.selectAll('*').remove();
3554
+
3555
+ var groups = this._groups();
3556
+ groups = this._stack(groups);
3557
+
3558
+ groups.forEach( function(group) {
3559
+
3560
+ var series = group.series
3561
+ .filter( function(series) { return !series.disabled } );
3562
+
3563
+ series.active = function() { return series };
3564
+
3565
+ group.renderer.render({ series: series, vis: group.vis });
3566
+ series.forEach(function(s) { s.stack = s._stack || s.stack || s.data; });
3567
+ });
3568
+ }
3569
+
3570
+ } );
3571
+ Rickshaw.namespace('Rickshaw.Graph.Renderer.LinePlot');
3572
+
3573
+ Rickshaw.Graph.Renderer.LinePlot = Rickshaw.Class.create( Rickshaw.Graph.Renderer, {
3574
+
3575
+ name: 'lineplot',
3576
+
3577
+ defaults: function($super) {
3578
+
3579
+ return Rickshaw.extend( $super(), {
3580
+ unstack: true,
3581
+ fill: false,
3582
+ stroke: true,
3583
+ padding:{ top: 0.01, right: 0.01, bottom: 0.01, left: 0.01 },
3584
+ dotSize: 3,
3585
+ strokeWidth: 2
3586
+ } );
3587
+ },
3588
+
3589
+ initialize: function($super, args) {
3590
+ $super(args);
3591
+ },
3592
+
3593
+ seriesPathFactory: function() {
3594
+
3595
+ var graph = this.graph;
3596
+
3597
+ var factory = d3.svg.line()
3598
+ .x( function(d) { return graph.x(d.x) } )
3599
+ .y( function(d) { return graph.y(d.y) } )
3600
+ .interpolate(this.graph.interpolation).tension(this.tension);
3601
+
3602
+ factory.defined && factory.defined( function(d) { return d.y !== null } );
3603
+ return factory;
3604
+ },
3605
+
3606
+ _renderDots: function() {
3607
+
3608
+ var graph = this.graph;
3609
+
3610
+ graph.series.forEach(function(series) {
3611
+
3612
+ if (series.disabled) return;
3613
+
3614
+ var nodes = graph.vis.selectAll("x")
3615
+ .data(series.stack.filter( function(d) { return d.y !== null } ))
3616
+ .enter().append("svg:circle")
3617
+ .attr("cx", function(d) { return graph.x(d.x) })
3618
+ .attr("cy", function(d) { return graph.y(d.y) })
3619
+ .attr("r", function(d) { return ("r" in d) ? d.r : graph.renderer.dotSize});
3620
+
3621
+ Array.prototype.forEach.call(nodes[0], function(n) {
3622
+ if (!n) return;
3623
+ n.setAttribute('data-color', series.color);
3624
+ n.setAttribute('fill', 'white');
3625
+ n.setAttribute('stroke', series.color);
3626
+ n.setAttribute('stroke-width', this.strokeWidth);
3627
+
3628
+ }.bind(this));
3629
+
3630
+ }, this);
3631
+ },
3632
+
3633
+ _renderLines: function() {
3634
+
3635
+ var graph = this.graph;
3636
+
3637
+ var nodes = graph.vis.selectAll("path")
3638
+ .data(this.graph.stackedData)
3639
+ .enter().append("svg:path")
3640
+ .attr("d", this.seriesPathFactory());
3641
+
3642
+ var i = 0;
3643
+ graph.series.forEach(function(series) {
3644
+ if (series.disabled) return;
3645
+ series.path = nodes[0][i++];
3646
+ this._styleSeries(series);
3647
+ }, this);
3648
+ },
3649
+
3650
+ render: function() {
3651
+
3652
+ var graph = this.graph;
3653
+
3654
+ graph.vis.selectAll('*').remove();
3655
+
3656
+ this._renderLines();
3657
+ this._renderDots();
3658
+ }
3659
+ } );
3660
+
3661
+ Rickshaw.namespace('Rickshaw.Graph.Smoother');
3662
+
3663
+ Rickshaw.Graph.Smoother = Rickshaw.Class.create({
3664
+
3665
+ initialize: function(args) {
3666
+
3667
+ this.graph = args.graph;
3668
+ this.element = args.element;
3669
+ this.aggregationScale = 1;
3670
+
3671
+ this.build();
3672
+
3673
+ this.graph.stackData.hooks.data.push( {
3674
+ name: 'smoother',
3675
+ orderPosition: 50,
3676
+ f: this.transformer.bind(this)
3677
+ } );
3678
+ },
3679
+
3680
+ build: function() {
3681
+
3682
+ var self = this;
3683
+
3684
+ if (this.element) {
3685
+ $( function() {
3686
+ $(self.element).slider( {
3687
+ min: 1,
3688
+ max: 100,
3689
+ slide: function( event, ui ) {
3690
+ self.setScale(ui.value);
3691
+ self.graph.update();
3692
+ }
3693
+ } );
3694
+ } );
3695
+ }
3696
+ },
3697
+
3698
+ setScale: function(scale) {
3699
+
3700
+ if (scale < 1) {
3701
+ throw "scale out of range: " + scale;
3702
+ }
3703
+
3704
+ this.aggregationScale = scale;
3705
+ this.graph.update();
3706
+ },
3707
+
3708
+ transformer: function(data) {
3709
+
3710
+ if (this.aggregationScale == 1) return data;
3711
+
3712
+ var aggregatedData = [];
3713
+
3714
+ data.forEach( function(seriesData) {
3715
+
3716
+ var aggregatedSeriesData = [];
3717
+
3718
+ while (seriesData.length) {
3719
+
3720
+ var avgX = 0, avgY = 0;
3721
+ var slice = seriesData.splice(0, this.aggregationScale);
3722
+
3723
+ slice.forEach( function(d) {
3724
+ avgX += d.x / slice.length;
3725
+ avgY += d.y / slice.length;
3726
+ } );
3727
+
3728
+ aggregatedSeriesData.push( { x: avgX, y: avgY } );
3729
+ }
3730
+
3731
+ aggregatedData.push(aggregatedSeriesData);
3732
+
3733
+ }.bind(this) );
3734
+
3735
+ return aggregatedData;
3736
+ }
3737
+ });
3738
+
3739
+ Rickshaw.namespace('Rickshaw.Graph.Socketio');
3740
+
3741
+ Rickshaw.Graph.Socketio = Rickshaw.Class.create( Rickshaw.Graph.Ajax, {
3742
+ request: function() {
3743
+ var socket = io.connect(this.dataURL);
3744
+ var self = this;
3745
+ socket.on('rickshaw', function (data) {
3746
+ self.success(data);
3747
+ });
3748
+ }
3749
+ } );
3750
+ Rickshaw.namespace('Rickshaw.Series');
3751
+
3752
+ Rickshaw.Series = Rickshaw.Class.create( Array, {
3753
+
3754
+ initialize: function (data, palette, options) {
3755
+
3756
+ options = options || {};
3757
+
3758
+ this.palette = new Rickshaw.Color.Palette(palette);
3759
+
3760
+ this.timeBase = typeof(options.timeBase) === 'undefined' ?
3761
+ Math.floor(new Date().getTime() / 1000) :
3762
+ options.timeBase;
3763
+
3764
+ var timeInterval = typeof(options.timeInterval) == 'undefined' ?
3765
+ 1000 :
3766
+ options.timeInterval;
3767
+
3768
+ this.setTimeInterval(timeInterval);
3769
+
3770
+ if (data && (typeof(data) == "object") && Array.isArray(data)) {
3771
+ data.forEach( function(item) { this.addItem(item) }, this );
3772
+ }
3773
+ },
3774
+
3775
+ addItem: function(item) {
3776
+
3777
+ if (typeof(item.name) === 'undefined') {
3778
+ throw('addItem() needs a name');
3779
+ }
3780
+
3781
+ item.color = (item.color || this.palette.color(item.name));
3782
+ item.data = (item.data || []);
3783
+
3784
+ // backfill, if necessary
3785
+ if ((item.data.length === 0) && this.length && (this.getIndex() > 0)) {
3786
+ this[0].data.forEach( function(plot) {
3787
+ item.data.push({ x: plot.x, y: 0 });
3788
+ } );
3789
+ } else if (item.data.length === 0) {
3790
+ item.data.push({ x: this.timeBase - (this.timeInterval || 0), y: 0 });
3791
+ }
3792
+
3793
+ this.push(item);
3794
+
3795
+ if (this.legend) {
3796
+ this.legend.addLine(this.itemByName(item.name));
3797
+ }
3798
+ },
3799
+
3800
+ addData: function(data, x) {
3801
+
3802
+ var index = this.getIndex();
3803
+
3804
+ Rickshaw.keys(data).forEach( function(name) {
3805
+ if (! this.itemByName(name)) {
3806
+ this.addItem({ name: name });
3807
+ }
3808
+ }, this );
3809
+
3810
+ this.forEach( function(item) {
3811
+ item.data.push({
3812
+ x: x || (index * this.timeInterval || 1) + this.timeBase,
3813
+ y: (data[item.name] || 0)
3814
+ });
3815
+ }, this );
3816
+ },
3817
+
3818
+ getIndex: function () {
3819
+ return (this[0] && this[0].data && this[0].data.length) ? this[0].data.length : 0;
3820
+ },
3821
+
3822
+ itemByName: function(name) {
3823
+
3824
+ for (var i = 0; i < this.length; i++) {
3825
+ if (this[i].name == name)
3826
+ return this[i];
3827
+ }
3828
+ },
3829
+
3830
+ setTimeInterval: function(iv) {
3831
+ this.timeInterval = iv / 1000;
3832
+ },
3833
+
3834
+ setTimeBase: function (t) {
3835
+ this.timeBase = t;
3836
+ },
3837
+
3838
+ dump: function() {
3839
+
3840
+ var data = {
3841
+ timeBase: this.timeBase,
3842
+ timeInterval: this.timeInterval,
3843
+ items: []
3844
+ };
3845
+
3846
+ this.forEach( function(item) {
3847
+
3848
+ var newItem = {
3849
+ color: item.color,
3850
+ name: item.name,
3851
+ data: []
3852
+ };
3853
+
3854
+ item.data.forEach( function(plot) {
3855
+ newItem.data.push({ x: plot.x, y: plot.y });
3856
+ } );
3857
+
3858
+ data.items.push(newItem);
3859
+ } );
3860
+
3861
+ return data;
3862
+ },
3863
+
3864
+ load: function(data) {
3865
+
3866
+ if (data.timeInterval) {
3867
+ this.timeInterval = data.timeInterval;
3868
+ }
3869
+
3870
+ if (data.timeBase) {
3871
+ this.timeBase = data.timeBase;
3872
+ }
3873
+
3874
+ if (data.items) {
3875
+ data.items.forEach( function(item) {
3876
+ this.push(item);
3877
+ if (this.legend) {
3878
+ this.legend.addLine(this.itemByName(item.name));
3879
+ }
3880
+
3881
+ }, this );
3882
+ }
3883
+ }
3884
+ } );
3885
+
3886
+ Rickshaw.Series.zeroFill = function(series) {
3887
+ Rickshaw.Series.fill(series, 0);
3888
+ };
3889
+
3890
+ Rickshaw.Series.fill = function(series, fill) {
3891
+
3892
+ var x;
3893
+ var i = 0;
3894
+
3895
+ var data = series.map( function(s) { return s.data } );
3896
+
3897
+ while ( i < Math.max.apply(null, data.map( function(d) { return d.length } )) ) {
3898
+
3899
+ x = Math.min.apply( null,
3900
+ data
3901
+ .filter(function(d) { return d[i] })
3902
+ .map(function(d) { return d[i].x })
3903
+ );
3904
+
3905
+ data.forEach( function(d) {
3906
+ if (!d[i] || d[i].x != x) {
3907
+ d.splice(i, 0, { x: x, y: fill });
3908
+ }
3909
+ } );
3910
+
3911
+ i++;
3912
+ }
3913
+ };
3914
+
3915
+ Rickshaw.namespace('Rickshaw.Series.FixedDuration');
3916
+
3917
+ Rickshaw.Series.FixedDuration = Rickshaw.Class.create(Rickshaw.Series, {
3918
+
3919
+ initialize: function (data, palette, options) {
3920
+
3921
+ options = options || {};
3922
+
3923
+ if (typeof(options.timeInterval) === 'undefined') {
3924
+ throw new Error('FixedDuration series requires timeInterval');
3925
+ }
3926
+
3927
+ if (typeof(options.maxDataPoints) === 'undefined') {
3928
+ throw new Error('FixedDuration series requires maxDataPoints');
3929
+ }
3930
+
3931
+ this.palette = new Rickshaw.Color.Palette(palette);
3932
+ this.timeBase = typeof(options.timeBase) === 'undefined' ? Math.floor(new Date().getTime() / 1000) : options.timeBase;
3933
+ this.setTimeInterval(options.timeInterval);
3934
+
3935
+ if (this[0] && this[0].data && this[0].data.length) {
3936
+ this.currentSize = this[0].data.length;
3937
+ this.currentIndex = this[0].data.length;
3938
+ } else {
3939
+ this.currentSize = 0;
3940
+ this.currentIndex = 0;
3941
+ }
3942
+
3943
+ this.maxDataPoints = options.maxDataPoints;
3944
+
3945
+
3946
+ if (data && (typeof(data) == "object") && Array.isArray(data)) {
3947
+ data.forEach( function (item) { this.addItem(item) }, this );
3948
+ this.currentSize += 1;
3949
+ this.currentIndex += 1;
3950
+ }
3951
+
3952
+ // reset timeBase for zero-filled values if needed
3953
+ this.timeBase -= (this.maxDataPoints - this.currentSize) * this.timeInterval;
3954
+
3955
+ // zero-fill up to maxDataPoints size if we don't have that much data yet
3956
+ if ((typeof(this.maxDataPoints) !== 'undefined') && (this.currentSize < this.maxDataPoints)) {
3957
+ for (var i = this.maxDataPoints - this.currentSize - 1; i > 1; i--) {
3958
+ this.currentSize += 1;
3959
+ this.currentIndex += 1;
3960
+ this.forEach( function (item) {
3961
+ item.data.unshift({ x: ((i-1) * this.timeInterval || 1) + this.timeBase, y: 0, i: i });
3962
+ }, this );
3963
+ }
3964
+ }
3965
+ },
3966
+
3967
+ addData: function($super, data, x) {
3968
+
3969
+ $super(data, x);
3970
+
3971
+ this.currentSize += 1;
3972
+ this.currentIndex += 1;
3973
+
3974
+ if (this.maxDataPoints !== undefined) {
3975
+ while (this.currentSize > this.maxDataPoints) {
3976
+ this.dropData();
3977
+ }
3978
+ }
3979
+ },
3980
+
3981
+ dropData: function() {
3982
+
3983
+ this.forEach(function(item) {
3984
+ item.data.splice(0, 1);
3985
+ } );
3986
+
3987
+ this.currentSize -= 1;
3988
+ },
3989
+
3990
+ getIndex: function () {
3991
+ return this.currentIndex;
3992
+ }
3993
+ } );
3994
+