| 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2011 Dan Vanderkam (danvdk@gmail.com) |
| 4 | * MIT-licensed (http://opensource.org/licenses/MIT) |
| 5 | */ |
| 6 | |
| 7 | /** |
| 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. |
| 12 | */ |
| 13 | |
| 14 | Dygraph.LOG_SCALE = 10; |
| 15 | Dygraph.LN_TEN = Math.log(Dygraph.LOG_SCALE); |
| 16 | |
| 17 | /** @private */ |
| 18 | Dygraph.log10 = function(x) { |
| 19 | return Math.log(x) / Dygraph.LN_TEN; |
| 20 | } |
| 21 | |
| 22 | // Various logging levels. |
| 23 | Dygraph.DEBUG = 1; |
| 24 | Dygraph.INFO = 2; |
| 25 | Dygraph.WARNING = 3; |
| 26 | Dygraph.ERROR = 3; |
| 27 | |
| 28 | // TODO(danvk): any way I can get the line numbers to be this.warn call? |
| 29 | /** |
| 30 | * @private |
| 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. |
| 34 | */ |
| 35 | Dygraph.log = function(severity, message) { |
| 36 | if (typeof(console) != 'undefined') { |
| 37 | switch (severity) { |
| 38 | case Dygraph.DEBUG: |
| 39 | console.debug('dygraphs: ' + message); |
| 40 | break; |
| 41 | case Dygraph.INFO: |
| 42 | console.info('dygraphs: ' + message); |
| 43 | break; |
| 44 | case Dygraph.WARNING: |
| 45 | console.warn('dygraphs: ' + message); |
| 46 | break; |
| 47 | case Dygraph.ERROR: |
| 48 | console.error('dygraphs: ' + message); |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | /** @private */ |
| 55 | Dygraph.info = function(message) { |
| 56 | Dygraph.log(Dygraph.INFO, message); |
| 57 | }; |
| 58 | /** @private */ |
| 59 | Dygraph.prototype.info = Dygraph.info; |
| 60 | |
| 61 | /** @private */ |
| 62 | Dygraph.warn = function(message) { |
| 63 | Dygraph.log(Dygraph.WARNING, message); |
| 64 | }; |
| 65 | /** @private */ |
| 66 | Dygraph.prototype.warn = Dygraph.warn; |
| 67 | |
| 68 | /** @private */ |
| 69 | Dygraph.error = function(message) { |
| 70 | Dygraph.log(Dygraph.ERROR, message); |
| 71 | }; |
| 72 | /** @private */ |
| 73 | Dygraph.prototype.error = Dygraph.error; |
| 74 | |
| 75 | /** |
| 76 | * @private |
| 77 | * Return the 2d context for a dygraph canvas. |
| 78 | * |
| 79 | * This method is only exposed for the sake of replacing the function in |
| 80 | * automated tests, e.g. |
| 81 | * |
| 82 | * var oldFunc = Dygraph.getContext(); |
| 83 | * Dygraph.getContext = function(canvas) { |
| 84 | * var realContext = oldFunc(canvas); |
| 85 | * return new Proxy(realContext); |
| 86 | * }; |
| 87 | */ |
| 88 | Dygraph.getContext = function(canvas) { |
| 89 | return canvas.getContext("2d"); |
| 90 | }; |
| 91 | |
| 92 | /** |
| 93 | * @private |
| 94 | * Add an event handler. This smooths a difference between IE and the rest of |
| 95 | * the world. |
| 96 | * @param { DOM element } elem The element to add the event to. |
| 97 | * @param { String } type The type 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. |
| 100 | */ |
| 101 | Dygraph.addEvent = function addEvent(elem, type, fn) { |
| 102 | if (elem.addEventListener) { |
| 103 | elem.addEventListener(type, fn, false); |
| 104 | } else { |
| 105 | elem[type+fn] = function(){fn(window.event);}; |
| 106 | elem.attachEvent('on'+type, elem[type+fn]); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | /** |
| 111 | * @private |
| 112 | * Remove an event handler. This smooths a difference between IE and the rest of |
| 113 | * the world. |
| 114 | * @param { DOM element } elem The element to add the event to. |
| 115 | * @param { String } type The type of the event, e.g. 'click' or 'mousemove'. |
| 116 | * @param { Function } fn The function to call on the event. The function takes |
| 117 | * one parameter: the event object. |
| 118 | */ |
| 119 | Dygraph.removeEvent = function addEvent(elem, type, fn) { |
| 120 | if (elem.removeEventListener) { |
| 121 | elem.removeEventListener(type, fn, false); |
| 122 | } else { |
| 123 | elem.detachEvent('on'+type, elem[type+fn]); |
| 124 | elem[type+fn] = null; |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | /** |
| 129 | * @private |
| 130 | * Cancels further processing of an event. This is useful to prevent default |
| 131 | * browser actions, e.g. highlighting text on a double-click. |
| 132 | * Based on the article at |
| 133 | * http://www.switchonthecode.com/tutorials/javascript-tutorial-the-scroll-wheel |
| 134 | * @param { Event } e The event whose normal behavior should be canceled. |
| 135 | */ |
| 136 | Dygraph.cancelEvent = function(e) { |
| 137 | e = e ? e : window.event; |
| 138 | if (e.stopPropagation) { |
| 139 | e.stopPropagation(); |
| 140 | } |
| 141 | if (e.preventDefault) { |
| 142 | e.preventDefault(); |
| 143 | } |
| 144 | e.cancelBubble = true; |
| 145 | e.cancel = true; |
| 146 | e.returnValue = false; |
| 147 | return false; |
| 148 | }; |
| 149 | |
| 150 | /** |
| 151 | * Convert hsv values to an rgb(r,g,b) string. Taken from MochiKit.Color. This |
| 152 | * is used to generate default series colors which are evenly spaced on the |
| 153 | * color wheel. |
| 154 | * @param { Number } hue Range is 0.0-1.0. |
| 155 | * @param { Number } saturation Range is 0.0-1.0. |
| 156 | * @param { Number } value Range is 0.0-1.0. |
| 157 | * @return { String } "rgb(r,g,b)" where r, g and b range from 0-255. |
| 158 | * @private |
| 159 | */ |
| 160 | Dygraph.hsvToRGB = function (hue, saturation, value) { |
| 161 | var red; |
| 162 | var green; |
| 163 | var blue; |
| 164 | if (saturation === 0) { |
| 165 | red = value; |
| 166 | green = value; |
| 167 | blue = value; |
| 168 | } else { |
| 169 | var i = Math.floor(hue * 6); |
| 170 | var f = (hue * 6) - i; |
| 171 | var p = value * (1 - saturation); |
| 172 | var q = value * (1 - (saturation * f)); |
| 173 | var t = value * (1 - (saturation * (1 - f))); |
| 174 | switch (i) { |
| 175 | case 1: red = q; green = value; blue = p; break; |
| 176 | case 2: red = p; green = value; blue = t; break; |
| 177 | case 3: red = p; green = q; blue = value; break; |
| 178 | case 4: red = t; green = p; blue = value; break; |
| 179 | case 5: red = value; green = p; blue = q; break; |
| 180 | case 6: // fall through |
| 181 | case 0: red = value; green = t; blue = p; break; |
| 182 | } |
| 183 | } |
| 184 | red = Math.floor(255 * red + 0.5); |
| 185 | green = Math.floor(255 * green + 0.5); |
| 186 | blue = Math.floor(255 * blue + 0.5); |
| 187 | return 'rgb(' + red + ',' + green + ',' + blue + ')'; |
| 188 | }; |
| 189 | |
| 190 | // The following functions are from quirksmode.org with a modification for Safari from |
| 191 | // http://blog.firetree.net/2005/07/04/javascript-find-position/ |
| 192 | // http://www.quirksmode.org/js/findpos.html |
| 193 | // ... and modifications to support scrolling divs. |
| 194 | |
| 195 | /** |
| 196 | * Find the x-coordinate of the supplied object relative to the left side |
| 197 | * of the page. |
| 198 | * @private |
| 199 | */ |
| 200 | Dygraph.findPosX = function(obj) { |
| 201 | var curleft = 0; |
| 202 | if(obj.offsetParent) { |
| 203 | var copyObj = obj; |
| 204 | while(1) { |
| 205 | curleft += copyObj.offsetLeft; |
| 206 | if(!copyObj.offsetParent) { |
| 207 | break; |
| 208 | } |
| 209 | copyObj = copyObj.offsetParent; |
| 210 | } |
| 211 | } else if(obj.x) { |
| 212 | curleft += obj.x; |
| 213 | } |
| 214 | // This handles the case where the object is inside a scrolled div. |
| 215 | while(obj && obj != document.body) { |
| 216 | curleft -= obj.scrollLeft; |
| 217 | obj = obj.parentNode; |
| 218 | } |
| 219 | return curleft; |
| 220 | }; |
| 221 | |
| 222 | /** |
| 223 | * Find the y-coordinate of the supplied object relative to the top of the |
| 224 | * page. |
| 225 | * @private |
| 226 | */ |
| 227 | Dygraph.findPosY = function(obj) { |
| 228 | var curtop = 0; |
| 229 | if(obj.offsetParent) { |
| 230 | var copyObj = obj; |
| 231 | while(1) { |
| 232 | curtop += copyObj.offsetTop; |
| 233 | if(!copyObj.offsetParent) { |
| 234 | break; |
| 235 | } |
| 236 | copyObj = copyObj.offsetParent; |
| 237 | } |
| 238 | } else if(obj.y) { |
| 239 | curtop += obj.y; |
| 240 | } |
| 241 | // This handles the case where the object is inside a scrolled div. |
| 242 | while(obj && obj != document.body) { |
| 243 | curtop -= obj.scrollTop; |
| 244 | obj = obj.parentNode; |
| 245 | } |
| 246 | return curtop; |
| 247 | }; |
| 248 | |
| 249 | /** |
| 250 | * @private |
| 251 | * Returns the x-coordinate of the event in a coordinate system where the |
| 252 | * top-left corner of the page (not the window) is (0,0). |
| 253 | * Taken from MochiKit.Signal |
| 254 | */ |
| 255 | Dygraph.pageX = function(e) { |
| 256 | if (e.pageX) { |
| 257 | return (!e.pageX || e.pageX < 0) ? 0 : e.pageX; |
| 258 | } else { |
| 259 | var de = document; |
| 260 | var b = document.body; |
| 261 | return e.clientX + |
| 262 | (de.scrollLeft || b.scrollLeft) - |
| 263 | (de.clientLeft || 0); |
| 264 | } |
| 265 | }; |
| 266 | |
| 267 | /** |
| 268 | * @private |
| 269 | * Returns the y-coordinate of the event in a coordinate system where the |
| 270 | * top-left corner of the page (not the window) is (0,0). |
| 271 | * Taken from MochiKit.Signal |
| 272 | */ |
| 273 | Dygraph.pageY = function(e) { |
| 274 | if (e.pageY) { |
| 275 | return (!e.pageY || e.pageY < 0) ? 0 : e.pageY; |
| 276 | } else { |
| 277 | var de = document; |
| 278 | var b = document.body; |
| 279 | return e.clientY + |
| 280 | (de.scrollTop || b.scrollTop) - |
| 281 | (de.clientTop || 0); |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | /** |
| 286 | * @private |
| 287 | * @param { Number } x The number to consider. |
| 288 | * @return { Boolean } Whether the number is zero or NaN. |
| 289 | */ |
| 290 | // TODO(danvk): rename this function to something like 'isNonZeroNan'. |
| 291 | Dygraph.isOK = function(x) { |
| 292 | return x && !isNaN(x); |
| 293 | }; |
| 294 | |
| 295 | /** |
| 296 | * Number formatting function which mimicks the behavior of %g in printf, i.e. |
| 297 | * either exponential or fixed format (without trailing 0s) is used depending on |
| 298 | * the length of the generated string. The advantage of this format is that |
| 299 | * there is a predictable upper bound on the resulting string length, |
| 300 | * significant figures are not dropped, and normal numbers are not displayed in |
| 301 | * exponential notation. |
| 302 | * |
| 303 | * NOTE: JavaScript's native toPrecision() is NOT a drop-in replacement for %g. |
| 304 | * It creates strings which are too long for absolute values between 10^-4 and |
| 305 | * 10^-6, e.g. '0.00001' instead of '1e-5'. See tests/number-format.html for |
| 306 | * output examples. |
| 307 | * |
| 308 | * @param {Number} x The number to format |
| 309 | * @param {Number} opt_precision The precision to use, default 2. |
| 310 | * @return {String} A string formatted like %g in printf. The max generated |
| 311 | * string length should be precision + 6 (e.g 1.123e+300). |
| 312 | */ |
| 313 | Dygraph.floatFormat = function(x, opt_precision) { |
| 314 | // Avoid invalid precision values; [1, 21] is the valid range. |
| 315 | var p = Math.min(Math.max(1, opt_precision || 2), 21); |
| 316 | |
| 317 | // This is deceptively simple. The actual algorithm comes from: |
| 318 | // |
| 319 | // Max allowed length = p + 4 |
| 320 | // where 4 comes from 'e+n' and '.'. |
| 321 | // |
| 322 | // Length of fixed format = 2 + y + p |
| 323 | // where 2 comes from '0.' and y = # of leading zeroes. |
| 324 | // |
| 325 | // Equating the two and solving for y yields y = 2, or 0.00xxxx which is |
| 326 | // 1.0e-3. |
| 327 | // |
| 328 | // Since the behavior of toPrecision() is identical for larger numbers, we |
| 329 | // don't have to worry about the other bound. |
| 330 | // |
| 331 | // Finally, the argument for toExponential() is the number of trailing digits, |
| 332 | // so we take off 1 for the value before the '.'. |
| 333 | return (Math.abs(x) < 1.0e-3 && x != 0.0) ? |
| 334 | x.toExponential(p - 1) : x.toPrecision(p); |
| 335 | }; |
| 336 | |
| 337 | /** |
| 338 | * @private |
| 339 | * Converts '9' to '09' (useful for dates) |
| 340 | */ |
| 341 | Dygraph.zeropad = function(x) { |
| 342 | if (x < 10) return "0" + x; else return "" + x; |
| 343 | }; |
| 344 | |
| 345 | /** |
| 346 | * Return a string version of the hours, minutes and seconds portion of a date. |
| 347 | * @param {Number} date The JavaScript date (ms since epoch) |
| 348 | * @return {String} A time of the form "HH:MM:SS" |
| 349 | * @private |
| 350 | */ |
| 351 | Dygraph.hmsString_ = function(date) { |
| 352 | var zeropad = Dygraph.zeropad; |
| 353 | var d = new Date(date); |
| 354 | if (d.getSeconds()) { |
| 355 | return zeropad(d.getHours()) + ":" + |
| 356 | zeropad(d.getMinutes()) + ":" + |
| 357 | zeropad(d.getSeconds()); |
| 358 | } else { |
| 359 | return zeropad(d.getHours()) + ":" + zeropad(d.getMinutes()); |
| 360 | } |
| 361 | }; |
| 362 | |
| 363 | /** |
| 364 | * Round a number to the specified number of digits past the decimal point. |
| 365 | * @param {Number} num The number to round |
| 366 | * @param {Number} places The number of decimals to which to round |
| 367 | * @return {Number} The rounded number |
| 368 | * @private |
| 369 | */ |
| 370 | Dygraph.round_ = function(num, places) { |
| 371 | var shift = Math.pow(10, places); |
| 372 | return Math.round(num * shift)/shift; |
| 373 | }; |
| 374 | |
| 375 | /** |
| 376 | * @private |
| 377 | * Implementation of binary search over an array. |
| 378 | * Currently does not work when val is outside the range of arry's values. |
| 379 | * @param { Integer } val the value to search for |
| 380 | * @param { Integer[] } arry is the value over which to search |
| 381 | * @param { Integer } abs If abs > 0, find the lowest entry greater than val |
| 382 | * If abs < 0, find the highest entry less than val. |
| 383 | * if abs == 0, find the entry that equals val. |
| 384 | * @param { Integer } [low] The first index in arry to consider (optional) |
| 385 | * @param { Integer } [high] The last index in arry to consider (optional) |
| 386 | */ |
| 387 | Dygraph.binarySearch = function(val, arry, abs, low, high) { |
| 388 | if (low == null || high == null) { |
| 389 | low = 0; |
| 390 | high = arry.length - 1; |
| 391 | } |
| 392 | if (low > high) { |
| 393 | return -1; |
| 394 | } |
| 395 | if (abs == null) { |
| 396 | abs = 0; |
| 397 | } |
| 398 | var validIndex = function(idx) { |
| 399 | return idx >= 0 && idx < arry.length; |
| 400 | } |
| 401 | var mid = parseInt((low + high) / 2); |
| 402 | var element = arry[mid]; |
| 403 | if (element == val) { |
| 404 | return mid; |
| 405 | } |
| 406 | if (element > val) { |
| 407 | if (abs > 0) { |
| 408 | // Accept if element > val, but also if prior element < val. |
| 409 | var idx = mid - 1; |
| 410 | if (validIndex(idx) && arry[idx] < val) { |
| 411 | return mid; |
| 412 | } |
| 413 | } |
| 414 | return Dygraph.binarySearch(val, arry, abs, low, mid - 1); |
| 415 | } |
| 416 | if (element < val) { |
| 417 | if (abs < 0) { |
| 418 | // Accept if element < val, but also if prior element > val. |
| 419 | var idx = mid + 1; |
| 420 | if (validIndex(idx) && arry[idx] > val) { |
| 421 | return mid; |
| 422 | } |
| 423 | } |
| 424 | return Dygraph.binarySearch(val, arry, abs, mid + 1, high); |
| 425 | } |
| 426 | }; |
| 427 | |
| 428 | /** |
| 429 | * @private |
| 430 | * Parses a date, returning the number of milliseconds since epoch. This can be |
| 431 | * passed in as an xValueParser in the Dygraph constructor. |
| 432 | * TODO(danvk): enumerate formats that this understands. |
| 433 | * @param {String} A date in YYYYMMDD format. |
| 434 | * @return {Number} Milliseconds since epoch. |
| 435 | */ |
| 436 | Dygraph.dateParser = function(dateStr) { |
| 437 | var dateStrSlashed; |
| 438 | var d; |
| 439 | if (dateStr.search("-") != -1) { // e.g. '2009-7-12' or '2009-07-12' |
| 440 | dateStrSlashed = dateStr.replace("-", "/", "g"); |
| 441 | while (dateStrSlashed.search("-") != -1) { |
| 442 | dateStrSlashed = dateStrSlashed.replace("-", "/"); |
| 443 | } |
| 444 | d = Dygraph.dateStrToMillis(dateStrSlashed); |
| 445 | } else if (dateStr.length == 8) { // e.g. '20090712' |
| 446 | // TODO(danvk): remove support for this format. It's confusing. |
| 447 | dateStrSlashed = dateStr.substr(0,4) + "/" + dateStr.substr(4,2) |
| 448 | + "/" + dateStr.substr(6,2); |
| 449 | d = Dygraph.dateStrToMillis(dateStrSlashed); |
| 450 | } else { |
| 451 | // Any format that Date.parse will accept, e.g. "2009/07/12" or |
| 452 | // "2009/07/12 12:34:56" |
| 453 | d = Dygraph.dateStrToMillis(dateStr); |
| 454 | } |
| 455 | |
| 456 | if (!d || isNaN(d)) { |
| 457 | Dygraph.error("Couldn't parse " + dateStr + " as a date"); |
| 458 | } |
| 459 | return d; |
| 460 | }; |
| 461 | |
| 462 | /** |
| 463 | * @private |
| 464 | * This is identical to JavaScript's built-in Date.parse() method, except that |
| 465 | * it doesn't get replaced with an incompatible method by aggressive JS |
| 466 | * libraries like MooTools or Joomla. |
| 467 | * @param { String } str The date string, e.g. "2011/05/06" |
| 468 | * @return { Integer } millis since epoch |
| 469 | */ |
| 470 | Dygraph.dateStrToMillis = function(str) { |
| 471 | return new Date(str).getTime(); |
| 472 | }; |
| 473 | |
| 474 | // These functions are all based on MochiKit. |
| 475 | /** |
| 476 | * Copies all the properties from o to self. |
| 477 | * |
| 478 | * @private |
| 479 | */ |
| 480 | Dygraph.update = function (self, o) { |
| 481 | if (typeof(o) != 'undefined' && o !== null) { |
| 482 | for (var k in o) { |
| 483 | if (o.hasOwnProperty(k)) { |
| 484 | self[k] = o[k]; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | return self; |
| 489 | }; |
| 490 | |
| 491 | /** |
| 492 | * Copies all the properties from o to self. |
| 493 | * |
| 494 | * @private |
| 495 | */ |
| 496 | Dygraph.updateDeep = function (self, o) { |
| 497 | // Taken from http://stackoverflow.com/questions/384286/javascript-isdom-how-do-you-check-if-a-javascript-object-is-a-dom-object |
| 498 | function isNode(o) { |
| 499 | return ( |
| 500 | typeof Node === "object" ? o instanceof Node : |
| 501 | typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName==="string" |
| 502 | ); |
| 503 | } |
| 504 | |
| 505 | if (typeof(o) != 'undefined' && o !== null) { |
| 506 | for (var k in o) { |
| 507 | if (o.hasOwnProperty(k)) { |
| 508 | if (o[k] == null) { |
| 509 | self[k] = null; |
| 510 | } else if (Dygraph.isArrayLike(o[k])) { |
| 511 | self[k] = o[k].slice(); |
| 512 | } else if (isNode(o[k])) { |
| 513 | // DOM objects are shallowly-copied. |
| 514 | self[k] = o[k]; |
| 515 | } else if (typeof(o[k]) == 'object') { |
| 516 | if (typeof(self[k]) != 'object') { |
| 517 | self[k] = {}; |
| 518 | } |
| 519 | Dygraph.updateDeep(self[k], o[k]); |
| 520 | } else { |
| 521 | self[k] = o[k]; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | return self; |
| 527 | }; |
| 528 | |
| 529 | /** |
| 530 | * @private |
| 531 | */ |
| 532 | Dygraph.isArrayLike = function (o) { |
| 533 | var typ = typeof(o); |
| 534 | if ( |
| 535 | (typ != 'object' && !(typ == 'function' && |
| 536 | typeof(o.item) == 'function')) || |
| 537 | o === null || |
| 538 | typeof(o.length) != 'number' || |
| 539 | o.nodeType === 3 |
| 540 | ) { |
| 541 | return false; |
| 542 | } |
| 543 | return true; |
| 544 | }; |
| 545 | |
| 546 | /** |
| 547 | * @private |
| 548 | */ |
| 549 | Dygraph.isDateLike = function (o) { |
| 550 | if (typeof(o) != "object" || o === null || |
| 551 | typeof(o.getTime) != 'function') { |
| 552 | return false; |
| 553 | } |
| 554 | return true; |
| 555 | }; |
| 556 | |
| 557 | /** |
| 558 | * Note: this only seems to work for arrays. |
| 559 | * @private |
| 560 | */ |
| 561 | Dygraph.clone = function(o) { |
| 562 | // TODO(danvk): figure out how MochiKit's version works |
| 563 | var r = []; |
| 564 | for (var i = 0; i < o.length; i++) { |
| 565 | if (Dygraph.isArrayLike(o[i])) { |
| 566 | r.push(Dygraph.clone(o[i])); |
| 567 | } else { |
| 568 | r.push(o[i]); |
| 569 | } |
| 570 | } |
| 571 | return r; |
| 572 | }; |
| 573 | |
| 574 | /** |
| 575 | * @private |
| 576 | * Create a new canvas element. This is more complex than a simple |
| 577 | * document.createElement("canvas") because of IE and excanvas. |
| 578 | */ |
| 579 | Dygraph.createCanvas = function() { |
| 580 | var canvas = document.createElement("canvas"); |
| 581 | |
| 582 | isIE = (/MSIE/.test(navigator.userAgent) && !window.opera); |
| 583 | if (isIE && (typeof(G_vmlCanvasManager) != 'undefined')) { |
| 584 | canvas = G_vmlCanvasManager.initElement(canvas); |
| 585 | } |
| 586 | |
| 587 | return canvas; |
| 588 | }; |
| 589 | |
| 590 | /** |
| 591 | * @private |
| 592 | * This function will scan the option list and determine if they |
| 593 | * require us to recalculate the pixel positions of each point. |
| 594 | * @param { List } a list of options to check. |
| 595 | * @return { Boolean } true if the graph needs new points else false. |
| 596 | */ |
| 597 | Dygraph.isPixelChangingOptionList = function(labels, attrs) { |
| 598 | // A whitelist of options that do not change pixel positions. |
| 599 | var pixelSafeOptions = { |
| 600 | 'annotationClickHandler': true, |
| 601 | 'annotationDblClickHandler': true, |
| 602 | 'annotationMouseOutHandler': true, |
| 603 | 'annotationMouseOverHandler': true, |
| 604 | 'axisLabelColor': true, |
| 605 | 'axisLineColor': true, |
| 606 | 'axisLineWidth': true, |
| 607 | 'clickCallback': true, |
| 608 | 'colorSaturation': true, |
| 609 | 'colorValue': true, |
| 610 | 'colors': true, |
| 611 | 'connectSeparatedPoints': true, |
| 612 | 'digitsAfterDecimal': true, |
| 613 | 'drawCallback': true, |
| 614 | 'drawPoints': true, |
| 615 | 'drawXGrid': true, |
| 616 | 'drawYGrid': true, |
| 617 | 'fillAlpha': true, |
| 618 | 'gridLineColor': true, |
| 619 | 'gridLineWidth': true, |
| 620 | 'hideOverlayOnMouseOut': true, |
| 621 | 'highlightCallback': true, |
| 622 | 'highlightCircleSize': true, |
| 623 | 'interactionModel': true, |
| 624 | 'isZoomedIgnoreProgrammaticZoom': true, |
| 625 | 'labelsDiv': true, |
| 626 | 'labelsDivStyles': true, |
| 627 | 'labelsDivWidth': true, |
| 628 | 'labelsKMB': true, |
| 629 | 'labelsKMG2': true, |
| 630 | 'labelsSeparateLines': true, |
| 631 | 'labelsShowZeroValues': true, |
| 632 | 'legend': true, |
| 633 | 'maxNumberWidth': true, |
| 634 | 'panEdgeFraction': true, |
| 635 | 'pixelsPerYLabel': true, |
| 636 | 'pointClickCallback': true, |
| 637 | 'pointSize': true, |
| 638 | 'rangeSelectorPlotFillColor': true, |
| 639 | 'rangeSelectorPlotStrokeColor': true, |
| 640 | 'showLabelsOnHighlight': true, |
| 641 | 'showRoller': true, |
| 642 | 'sigFigs': true, |
| 643 | 'strokeWidth': true, |
| 644 | 'underlayCallback': true, |
| 645 | 'unhighlightCallback': true, |
| 646 | 'xAxisLabelFormatter': true, |
| 647 | 'xTicker': true, |
| 648 | 'xValueFormatter': true, |
| 649 | 'yAxisLabelFormatter': true, |
| 650 | 'yValueFormatter': true, |
| 651 | 'zoomCallback': true |
| 652 | }; |
| 653 | |
| 654 | // Assume that we do not require new points. |
| 655 | // This will change to true if we actually do need new points. |
| 656 | var requiresNewPoints = false; |
| 657 | |
| 658 | // Create a dictionary of series names for faster lookup. |
| 659 | // If there are no labels, then the dictionary stays empty. |
| 660 | var seriesNamesDictionary = { }; |
| 661 | if (labels) { |
| 662 | for (var i = 1; i < labels.length; i++) { |
| 663 | seriesNamesDictionary[labels[i]] = true; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | // Iterate through the list of updated options. |
| 668 | for (property in attrs) { |
| 669 | // Break early if we already know we need new points from a previous option. |
| 670 | if (requiresNewPoints) { |
| 671 | break; |
| 672 | } |
| 673 | if (attrs.hasOwnProperty(property)) { |
| 674 | // Find out of this field is actually a series specific options list. |
| 675 | if (seriesNamesDictionary[property]) { |
| 676 | // This property value is a list of options for this series. |
| 677 | // If any of these sub properties are not pixel safe, set the flag. |
| 678 | for (subProperty in attrs[property]) { |
| 679 | // Break early if we already know we need new points from a previous option. |
| 680 | if (requiresNewPoints) { |
| 681 | break; |
| 682 | } |
| 683 | if (attrs[property].hasOwnProperty(subProperty) && !pixelSafeOptions[subProperty]) { |
| 684 | requiresNewPoints = true; |
| 685 | } |
| 686 | } |
| 687 | // If this was not a series specific option list, check if its a pixel changing property. |
| 688 | } else if (!pixelSafeOptions[property]) { |
| 689 | requiresNewPoints = true; |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | return requiresNewPoints; |
| 695 | }; |