3 * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview This file contains utility functions used by dygraphs. These
9 * are typically static (i.e. not related to any particular dygraph). Examples
10 * include date/time formatting functions, basic algorithms (e.g. binary
11 * search) and generic DOM-manipulation functions.
14 Dygraph
.LOG_SCALE
= 10;
15 Dygraph
.LN_TEN
= Math
.log(Dygraph
.LOG_SCALE
);
18 Dygraph
.log10
= function(x
) {
19 return Math
.log(x
) / Dygraph
.LN_TEN
;
22 // Various logging levels.
28 // TODO(danvk): any way I can get the line numbers to be this.warn call?
31 * Log an error on the JS console at the given severity.
32 * @param { Integer } severity One of Dygraph.{DEBUG,INFO,WARNING,ERROR}
33 * @param { String } The message to log.
35 Dygraph
.log
= function(severity
, message
) {
36 if (typeof(console
) != 'undefined') {
39 console
.debug('dygraphs: ' + message
);
42 console
.info('dygraphs: ' + message
);
45 console
.warn('dygraphs: ' + message
);
48 console
.error('dygraphs: ' + message
);
55 Dygraph
.info
= function(message
) {
56 Dygraph
.log(Dygraph
.INFO
, message
);
59 Dygraph
.prototype.info
= Dygraph
.info
;
62 Dygraph
.warn
= function(message
) {
63 Dygraph
.log(Dygraph
.WARNING
, message
);
66 Dygraph
.prototype.warn
= Dygraph
.warn
;
69 Dygraph
.error
= function(message
) {
70 Dygraph
.log(Dygraph
.ERROR
, message
);
73 Dygraph
.prototype.error
= Dygraph
.error
;
77 * Return the 2d context for a dygraph canvas.
79 * This method is only exposed for the sake of replacing the function in
80 * automated tests, e.g.
82 * var oldFunc = Dygraph.getContext();
83 * Dygraph.getContext = function(canvas) {
84 * var realContext = oldFunc(canvas);
85 * return new Proxy(realContext);
88 Dygraph
.getContext
= function(canvas
) {
89 return canvas
.getContext("2d");
94 * Add an event handler. This smooths a difference between IE and the rest of
96 * @param { DOM element } el The element to add the event to.
97 * @param { String } evt The name of the event, e.g. 'click' or 'mousemove'.
98 * @param { Function } fn The function to call on the event. The function takes
99 * one parameter: the event object.
101 Dygraph
.addEvent
= function(el
, evt
, fn
) {
102 var normed_fn
= function(e
) {
103 if (!e
) var e
= window
.event
;
106 if (window
.addEventListener
) { // Mozilla, Netscape, Firefox
107 el
.addEventListener(evt
, normed_fn
, false);
109 el
.attachEvent('on' + evt
, normed_fn
);
115 * Cancels further processing of an event. This is useful to prevent default
116 * browser actions, e.g. highlighting text on a double-click.
117 * Based on the article at
118 * http://www.switchonthecode.com/tutorials/javascript-tutorial-the-scroll-wheel
119 * @param { Event } e The event whose normal behavior should be canceled.
121 Dygraph
.cancelEvent
= function(e
) {
122 e
= e
? e
: window
.event
;
123 if (e
.stopPropagation
) {
126 if (e
.preventDefault
) {
129 e
.cancelBubble
= true;
131 e
.returnValue
= false;
136 * Convert hsv values to an rgb(r,g,b) string. Taken from MochiKit.Color. This
137 * is used to generate default series colors which are evenly spaced on the
139 * @param { Number } hue Range is 0.0-1.0.
140 * @param { Number } saturation Range is 0.0-1.0.
141 * @param { Number } value Range is 0.0-1.0.
142 * @return { String } "rgb(r,g,b)" where r, g and b range from 0-255.
145 Dygraph
.hsvToRGB
= function (hue
, saturation
, value
) {
149 if (saturation
=== 0) {
154 var i
= Math
.floor(hue
* 6);
155 var f
= (hue
* 6) - i
;
156 var p
= value
* (1 - saturation
);
157 var q
= value
* (1 - (saturation
* f
));
158 var t
= value
* (1 - (saturation
* (1 - f
)));
160 case 1: red
= q
; green
= value
; blue
= p
; break;
161 case 2: red
= p
; green
= value
; blue
= t
; break;
162 case 3: red
= p
; green
= q
; blue
= value
; break;
163 case 4: red
= t
; green
= p
; blue
= value
; break;
164 case 5: red
= value
; green
= p
; blue
= q
; break;
165 case 6: // fall through
166 case 0: red
= value
; green
= t
; blue
= p
; break;
169 red
= Math
.floor(255 * red
+ 0.5);
170 green
= Math
.floor(255 * green
+ 0.5);
171 blue
= Math
.floor(255 * blue
+ 0.5);
172 return 'rgb(' + red
+ ',' + green
+ ',' + blue
+ ')';
175 // The following functions are from quirksmode.org with a modification for Safari from
176 // http://blog.firetree.net/2005/07/04/javascript-find-position/
177 // http://www.quirksmode.org/js
/findpos
.html
178 // ... and modifications to support scrolling divs.
181 * Find the x-coordinate of the supplied object relative to the left side
185 Dygraph
.findPosX
= function(obj
) {
187 if(obj
.offsetParent
) {
190 curleft
+= copyObj
.offsetLeft
;
191 if(!copyObj
.offsetParent
) {
194 copyObj
= copyObj
.offsetParent
;
199 // This handles the case where the object is inside a scrolled div.
200 while(obj
&& obj
!= document
.body
) {
201 curleft
-= obj
.scrollLeft
;
202 obj
= obj
.parentNode
;
208 * Find the y-coordinate of the supplied object relative to the top of the
212 Dygraph
.findPosY
= function(obj
) {
214 if(obj
.offsetParent
) {
217 curtop
+= copyObj
.offsetTop
;
218 if(!copyObj
.offsetParent
) {
221 copyObj
= copyObj
.offsetParent
;
226 // This handles the case where the object is inside a scrolled div.
227 while(obj
&& obj
!= document
.body
) {
228 curtop
-= obj
.scrollTop
;
229 obj
= obj
.parentNode
;
236 * Returns the x-coordinate of the event in a coordinate system where the
237 * top-left corner of the page (not the window) is (0,0).
238 * Taken from MochiKit.Signal
240 Dygraph
.pageX
= function(e
) {
242 return (!e
.pageX
|| e
.pageX
< 0) ? 0 : e
.pageX
;
245 var b
= document
.body
;
247 (de
.scrollLeft
|| b
.scrollLeft
) -
248 (de
.clientLeft
|| 0);
254 * Returns the y-coordinate of the event in a coordinate system where the
255 * top-left corner of the page (not the window) is (0,0).
256 * Taken from MochiKit.Signal
258 Dygraph
.pageY
= function(e
) {
260 return (!e
.pageY
|| e
.pageY
< 0) ? 0 : e
.pageY
;
263 var b
= document
.body
;
265 (de
.scrollTop
|| b
.scrollTop
) -
272 * @param { Number } x The number to consider.
273 * @return { Boolean } Whether the number is zero or NaN.
275 // TODO(danvk): rename this function to something like 'isNonZeroNan'.
276 Dygraph
.isOK
= function(x
) {
277 return x
&& !isNaN(x
);
281 * Number formatting function which mimicks the behavior of %g in printf, i.e.
282 * either exponential or fixed format (without trailing 0s) is used depending on
283 * the length of the generated string. The advantage of this format is that
284 * there is a predictable upper bound on the resulting string length,
285 * significant figures are not dropped, and normal numbers are not displayed in
286 * exponential notation.
288 * NOTE: JavaScript's native toPrecision() is NOT a drop-in replacement for %g.
289 * It creates strings which are too long for absolute values between 10^-4 and
290 * 10^-6, e.g. '0.00001' instead of '1e-5'. See tests/number-format.html for
293 * @param {Number} x The number to format
294 * @param {Number} opt_precision The precision to use, default 2.
295 * @return {String} A string formatted like %g in printf. The max generated
296 * string length should be precision + 6 (e.g 1.123e+300).
298 Dygraph
.floatFormat
= function(x
, opt_precision
) {
299 // Avoid invalid precision values; [1, 21] is the valid range.
300 var p
= Math
.min(Math
.max(1, opt_precision
|| 2), 21);
302 // This is deceptively simple. The actual algorithm comes from:
304 // Max allowed length = p + 4
305 // where 4 comes from 'e+n' and '.'.
307 // Length of fixed format = 2 + y + p
308 // where 2 comes from '0.' and y = # of leading zeroes.
310 // Equating the two and solving for y yields y = 2, or 0.00xxxx which is
313 // Since the behavior of toPrecision() is identical for larger numbers, we
314 // don't have to worry about the other bound.
316 // Finally, the argument for toExponential() is the number of trailing digits,
317 // so we take off 1 for the value before the '.'.
318 return (Math
.abs(x
) < 1.0e-3 && x
!= 0.0) ?
319 x
.toExponential(p
- 1) : x
.toPrecision(p
);
324 * Converts '9' to '09' (useful for dates)
326 Dygraph
.zeropad
= function(x
) {
327 if (x
< 10) return "0" + x
; else return "" + x
;
331 * Return a string version of the hours, minutes and seconds portion of a date.
332 * @param {Number} date The JavaScript date (ms since epoch)
333 * @return {String} A time of the form "HH:MM:SS"
336 Dygraph
.hmsString_
= function(date
) {
337 var zeropad
= Dygraph
.zeropad
;
338 var d
= new Date(date
);
339 if (d
.getSeconds()) {
340 return zeropad(d
.getHours()) + ":" +
341 zeropad(d
.getMinutes()) + ":" +
342 zeropad(d
.getSeconds());
344 return zeropad(d
.getHours()) + ":" + zeropad(d
.getMinutes());
349 * Round a number to the specified number of digits past the decimal point.
350 * @param {Number} num The number to round
351 * @param {Number} places The number of decimals to which to round
352 * @return {Number} The rounded number
355 Dygraph
.round_
= function(num
, places
) {
356 var shift
= Math
.pow(10, places
);
357 return Math
.round(num
* shift
)/shift
;
362 * Implementation of binary search over an array.
363 * Currently does not work when val is outside the range of arry's values.
364 * @param { Integer } val the value to search for
365 * @param { Integer[] } arry is the value over which to search
366 * @param { Integer } abs If abs > 0, find the lowest entry greater than val
367 * If abs < 0, find the highest entry less than val.
368 * if abs == 0, find the entry that equals val.
369 * @param { Integer } [low] The first index in arry to consider (optional)
370 * @param { Integer } [high] The last index in arry to consider (optional)
372 Dygraph
.binarySearch
= function(val
, arry
, abs
, low
, high
) {
373 if (low
== null || high
== null) {
375 high
= arry
.length
- 1;
383 var validIndex
= function(idx
) {
384 return idx
>= 0 && idx
< arry
.length
;
386 var mid
= parseInt((low
+ high
) / 2);
387 var element
= arry
[mid
];
388 if (element
== val
) {
393 // Accept if element > val, but also if prior element < val.
395 if (validIndex(idx
) && arry
[idx
] < val
) {
399 return Dygraph
.binarySearch(val
, arry
, abs
, low
, mid
- 1);
403 // Accept if element < val, but also if prior element > val.
405 if (validIndex(idx
) && arry
[idx
] > val
) {
409 return Dygraph
.binarySearch(val
, arry
, abs
, mid
+ 1, high
);
415 * Parses a date, returning the number of milliseconds since epoch. This can be
416 * passed in as an xValueParser in the Dygraph constructor.
417 * TODO(danvk): enumerate formats that this understands.
418 * @param {String} A date in YYYYMMDD format.
419 * @return {Number} Milliseconds since epoch.
421 Dygraph
.dateParser
= function(dateStr
) {
424 if (dateStr
.search("-") != -1) { // e.g. '2009-7-12' or '2009-07-12'
425 dateStrSlashed
= dateStr
.replace("-", "/", "g");
426 while (dateStrSlashed
.search("-") != -1) {
427 dateStrSlashed
= dateStrSlashed
.replace("-", "/");
429 d
= Dygraph
.dateStrToMillis(dateStrSlashed
);
430 } else if (dateStr
.length
== 8) { // e.g. '20090712'
431 // TODO(danvk): remove support for this format. It's confusing.
432 dateStrSlashed
= dateStr
.substr(0,4) + "/" + dateStr
.substr(4,2)
433 + "/" + dateStr
.substr(6,2);
434 d
= Dygraph
.dateStrToMillis(dateStrSlashed
);
436 // Any format that Date.parse will accept, e.g. "2009/07/12" or
437 // "2009/07/12 12:34:56"
438 d
= Dygraph
.dateStrToMillis(dateStr
);
441 if (!d
|| isNaN(d
)) {
442 Dygraph
.error("Couldn't parse " + dateStr
+ " as a date");
449 * This is identical to JavaScript's built-in Date.parse() method, except that
450 * it doesn't get replaced with an incompatible method by aggressive JS
451 * libraries like MooTools or Joomla.
452 * @param { String } str The date string, e.g. "2011/05/06"
453 * @return { Integer } millis since epoch
455 Dygraph
.dateStrToMillis
= function(str
) {
456 return new Date(str
).getTime();
459 // These functions are all based on MochiKit.
461 * Copies all the properties from o to self.
465 Dygraph
.update
= function (self
, o
) {
466 if (typeof(o
) != 'undefined' && o
!== null) {
468 if (o
.hasOwnProperty(k
)) {
477 * Copies all the properties from o to self.
481 Dygraph
.updateDeep
= function (self
, o
) {
482 if (typeof(o
) != 'undefined' && o
!== null) {
484 if (o
.hasOwnProperty(k
)) {
487 } else if (Dygraph
.isArrayLike(o
[k
])) {
488 self
[k
] = o
[k
].slice();
489 } else if (typeof(o
[k
]) == 'object') {
490 if (typeof(self
[k
]) != 'object') {
493 Dygraph
.updateDeep(self
[k
], o
[k
]);
506 Dygraph
.isArrayLike
= function (o
) {
509 (typ
!= 'object' && !(typ
== 'function' &&
510 typeof(o
.item
) == 'function')) ||
512 typeof(o
.length
) != 'number' ||
523 Dygraph
.isDateLike
= function (o
) {
524 if (typeof(o
) != "object" || o
=== null ||
525 typeof(o
.getTime
) != 'function') {
532 * Note: this only seems to work for arrays.
535 Dygraph
.clone
= function(o
) {
536 // TODO(danvk): figure out how MochiKit's version works
538 for (var i
= 0; i
< o
.length
; i
++) {
539 if (Dygraph
.isArrayLike(o
[i
])) {
540 r
.push(Dygraph
.clone(o
[i
]));
550 * Create a new canvas element. This is more complex than a simple
551 * document.createElement("canvas") because of IE and excanvas.
553 Dygraph
.createCanvas
= function() {
554 var canvas
= document
.createElement("canvas");
556 isIE
= (/MSIE/.test(navigator
.userAgent
) && !window
.opera
);
557 if (isIE
&& (typeof(G_vmlCanvasManager
) != 'undefined')) {
558 canvas
= G_vmlCanvasManager
.initElement(canvas
);
566 * This function will scan the option list and determine if they
567 * require us to recalculate the pixel positions of each point.
568 * @param { List } a list of options to check.
569 * @return { Boolean } true if the graph needs new points else false.
571 Dygraph
.isPixelChangingOptionList
= function(labels
, attrs
) {
572 // A whitelist of options that do not change pixel positions.
573 var pixelSafeOptions
= {
574 'annotationClickHandler': true,
575 'annotationDblClickHandler': true,
576 'annotationMouseOutHandler': true,
577 'annotationMouseOverHandler': true,
578 'axisLabelColor': true,
579 'axisLineColor': true,
580 'axisLineWidth': true,
581 'clickCallback': true,
582 'colorSaturation': true,
585 'connectSeparatedPoints': true,
586 'digitsAfterDecimal': true,
587 'drawCallback': true,
592 'gridLineColor': true,
593 'gridLineWidth': true,
594 'hideOverlayOnMouseOut': true,
595 'highlightCallback': true,
596 'highlightCircleSize': true,
597 'interactionModel': true,
598 'isZoomedIgnoreProgrammaticZoom': true,
600 'labelsDivStyles': true,
601 'labelsDivWidth': true,
604 'labelsSeparateLines': true,
605 'labelsShowZeroValues': true,
607 'maxNumberWidth': true,
608 'panEdgeFraction': true,
609 'pixelsPerYLabel': true,
610 'pointClickCallback': true,
612 'showLabelsOnHighlight': true,
616 'underlayCallback': true,
617 'unhighlightCallback': true,
618 'xAxisLabelFormatter': true,
620 'xValueFormatter': true,
621 'yAxisLabelFormatter': true,
622 'yValueFormatter': true,
626 // Assume that we do not require new points.
627 // This will change to true if we actually do need new points.
628 var requiresNewPoints
= false;
630 // Create a dictionary of series names for faster lookup.
631 // If there are no labels, then the dictionary stays empty.
632 var seriesNamesDictionary
= { };
634 for (var i
= 1; i
< labels
.length
; i
++) {
635 seriesNamesDictionary
[labels
[i
]] = true;
639 // Iterate through the list of updated options.
640 for (property
in attrs
) {
641 // Break early if we already know we need new points from a previous option.
642 if (requiresNewPoints
) {
645 if (attrs
.hasOwnProperty(property
)) {
646 // Find out of this field is actually a series specific options list.
647 if (seriesNamesDictionary
[property
]) {
648 // This property value is a list of options for this series.
649 // If any of these sub properties are not pixel safe, set the flag.
650 for (subProperty
in attrs
[property
]) {
651 // Break early if we already know we need new points from a previous option.
652 if (requiresNewPoints
) {
655 if (attrs
[property
].hasOwnProperty(subProperty
) && !pixelSafeOptions
[subProperty
]) {
656 requiresNewPoints
= true;
659 // If this was not a series specific option list, check if its a pixel changing property.
660 } else if (!pixelSafeOptions
[property
]) {
661 requiresNewPoints
= true;
666 return requiresNewPoints
;