| 1 | /** |
| 2 | * @fileoverview Utility functions for Dygraphs. |
| 3 | * |
| 4 | * @author konigsberg@google.com (Robert Konigsberg) |
| 5 | */ |
| 6 | var Util = {}; |
| 7 | |
| 8 | /** |
| 9 | * Get the y-labels for a given axis. |
| 10 | * |
| 11 | * You can specify a parent if more than one graph is in the document. |
| 12 | */ |
| 13 | Util.getYLabels = function(axis_num, parent) { |
| 14 | axis_num = axis_num || ""; |
| 15 | parent = parent || document; |
| 16 | var y_labels = parent.getElementsByClassName("dygraph-axis-label-y" + axis_num); |
| 17 | var ary = []; |
| 18 | for (var i = 0; i < y_labels.length; i++) { |
| 19 | ary.push(y_labels[i].innerHTML.replace(/ | /g, ' ')); |
| 20 | } |
| 21 | return ary; |
| 22 | }; |
| 23 | |
| 24 | /** |
| 25 | * Get the x-labels for a given axis. |
| 26 | * |
| 27 | * You can specify a parent if more than one graph is in the document. |
| 28 | */ |
| 29 | Util.getXLabels = function(parent) { |
| 30 | parent = parent || document; |
| 31 | var x_labels = parent.getElementsByClassName("dygraph-axis-label-x"); |
| 32 | var ary = []; |
| 33 | for (var i = 0; i < x_labels.length; i++) { |
| 34 | ary.push(x_labels[i].innerHTML.replace(/ | /g, ' ')); |
| 35 | } |
| 36 | return ary; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * Returns all text in tags w/ a given css class, sorted. |
| 41 | * You can specify a parent if more than one graph is on the document. |
| 42 | */ |
| 43 | Util.getClassTexts = function(css_class, parent) { |
| 44 | parent = parent || document; |
| 45 | var texts = []; |
| 46 | var els = parent.getElementsByClassName(css_class); |
| 47 | for (var i = 0; i < els.length; i++) { |
| 48 | texts[i] = els[i].textContent; |
| 49 | } |
| 50 | texts.sort(); |
| 51 | return texts; |
| 52 | }; |
| 53 | |
| 54 | Util.getLegend = function(parent) { |
| 55 | parent = parent || document; |
| 56 | var legend = parent.getElementsByClassName("dygraph-legend")[0]; |
| 57 | var re = new RegExp(String.fromCharCode(160), 'g'); |
| 58 | return legend.textContent.replace(re, ' '); |
| 59 | }; |
| 60 | |
| 61 | /** |
| 62 | * Assert that all elements have a certain style property. |
| 63 | */ |
| 64 | Util.assertStyleOfChildren = function(selector, property, expectedValue) { |
| 65 | assertTrue(selector.length > 0); |
| 66 | $.each(selector, function(idx, child) { |
| 67 | assertEquals(expectedValue, $(child).css(property)); |
| 68 | }); |
| 69 | }; |
| 70 | |
| 71 | |
| 72 | /** |
| 73 | * Takes in an array of strings and returns an array of floats. |
| 74 | */ |
| 75 | Util.makeNumbers = function(ary) { |
| 76 | var ret = []; |
| 77 | for (var i = 0; i < ary.length; i++) { |
| 78 | ret.push(parseFloat(ary[i])); |
| 79 | } |
| 80 | return ret; |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | /** |
| 85 | * Sample a pixel from the canvas. |
| 86 | * Returns an [r, g, b, a] tuple where each values is in [0, 255]. |
| 87 | */ |
| 88 | Util.samplePixel = function(canvas, x, y) { |
| 89 | var ctx = canvas.getContext("2d"); // bypasses Proxy if applied. |
| 90 | |
| 91 | // TODO(danvk): Any performance issues with this? |
| 92 | var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); |
| 93 | |
| 94 | var scale = Dygraph.getContextPixelRatio(ctx); |
| 95 | |
| 96 | var i = 4 * (x * scale + imageData.width * y * scale); |
| 97 | var d = imageData.data; |
| 98 | return [d[i], d[i+1], d[i+2], d[i+3]]; |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * Overrides the browser's built-in XMLHttpRequest with a mock. |
| 103 | * Usage: |
| 104 | * |
| 105 | * var mockXhr = Util.overrideXMLHttpRequest(your_data); |
| 106 | * ... call code that does an XHR ... |
| 107 | * mockXhr.respond(); // restores default behavior. |
| 108 | * ... do your assertions ... |
| 109 | */ |
| 110 | Util.overrideXMLHttpRequest = function(data) { |
| 111 | var originalXMLHttpRequest = XMLHttpRequest; |
| 112 | |
| 113 | var requests = []; |
| 114 | var FakeXMLHttpRequest = function () { |
| 115 | requests.push(this); |
| 116 | }; |
| 117 | FakeXMLHttpRequest.prototype.open = function () {}; |
| 118 | FakeXMLHttpRequest.prototype.send = function () { |
| 119 | this.readyState = 4; |
| 120 | this.status = 200; |
| 121 | this.responseText = data; |
| 122 | }; |
| 123 | FakeXMLHttpRequest.restore = function() { |
| 124 | XMLHttpRequest = originalXMLHttpRequest; |
| 125 | }; |
| 126 | FakeXMLHttpRequest.respond = function() { |
| 127 | for (var i = 0; i < requests.length; i++) { |
| 128 | requests[i].onreadystatechange(); |
| 129 | } |
| 130 | FakeXMLHttpRequest.restore(); |
| 131 | }; |
| 132 | XMLHttpRequest = FakeXMLHttpRequest; |
| 133 | return FakeXMLHttpRequest; |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * Format a date as 2000/01/23 |
| 138 | * @param {number} dateMillis Millis since epoch. |
| 139 | * @return {string} The date formatted as YYYY-MM-DD. |
| 140 | */ |
| 141 | Util.formatDate = function(dateMillis) { |
| 142 | return Dygraph.dateString_(dateMillis).slice(0, 10); // 10 == "YYYY/MM/DD".length |
| 143 | }; |