Commit | Line | Data |
---|---|---|
fa607ffb RK |
1 | /** |
2 | * @fileoverview Utility functions for Dygraphs. | |
3 | * | |
4 | * @author konigsberg@google.com (Robert Konigsberg) | |
5 | */ | |
6 | var Util = {}; | |
7 | ||
8 | /** | |
846038aa RK |
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. | |
fa607ffb RK |
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++) { | |
0b85865a | 19 | ary.push(y_labels[i].innerHTML.replace(/ | /g, ' ')); |
fa607ffb RK |
20 | } |
21 | return ary; | |
d574a45e | 22 | }; |
fa607ffb RK |
23 | |
24 | /** | |
846038aa RK |
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++) { | |
0b85865a | 34 | ary.push(x_labels[i].innerHTML.replace(/ | /g, ' ')); |
846038aa RK |
35 | } |
36 | return ary; | |
d574a45e | 37 | }; |
846038aa RK |
38 | |
39 | /** | |
fa607ffb RK |
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; | |
d574a45e | 52 | }; |
846038aa | 53 | |
457deb39 DV |
54 | // Convert to a normal space |
55 | Util.nbspToSpace = function(str) { | |
56 | var re = new RegExp(String.fromCharCode(160), 'g'); | |
57 | return str.replace(re, ' '); | |
58 | }; | |
59 | ||
846038aa RK |
60 | Util.getLegend = function(parent) { |
61 | parent = parent || document; | |
62 | var legend = parent.getElementsByClassName("dygraph-legend")[0]; | |
457deb39 | 63 | return Util.nbspToSpace(legend.textContent); |
d574a45e | 64 | }; |
846038aa | 65 | |
d574a45e | 66 | /** |
3a84670d | 67 | * Assert that all elements have a certain style property. |
d574a45e | 68 | */ |
3a84670d | 69 | Util.assertStyleOfChildren = function(selector, property, expectedValue) { |
89fdcedb | 70 | assert.isTrue(selector.length > 0); |
dc910fce DV |
71 | for (var idx = 0; idx < selector.length; idx++) { |
72 | var child = selector[idx]; | |
73 | assert.equal(expectedValue, window.getComputedStyle(child)[property]); | |
74 | } | |
d574a45e | 75 | }; |
846038aa | 76 | |
71a1930b RK |
77 | |
78 | /** | |
79 | * Takes in an array of strings and returns an array of floats. | |
80 | */ | |
81 | Util.makeNumbers = function(ary) { | |
82 | var ret = []; | |
83 | for (var i = 0; i < ary.length; i++) { | |
84 | ret.push(parseFloat(ary[i])); | |
85 | } | |
86 | return ret; | |
901b2ebb | 87 | }; |
12b879f4 DV |
88 | |
89 | ||
90 | /** | |
91 | * Sample a pixel from the canvas. | |
92 | * Returns an [r, g, b, a] tuple where each values is in [0, 255]. | |
93 | */ | |
94 | Util.samplePixel = function(canvas, x, y) { | |
95 | var ctx = canvas.getContext("2d"); // bypasses Proxy if applied. | |
96 | ||
97 | // TODO(danvk): Any performance issues with this? | |
98 | var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); | |
99 | ||
37819481 PH |
100 | var scale = Dygraph.getContextPixelRatio(ctx); |
101 | ||
102 | var i = 4 * (x * scale + imageData.width * y * scale); | |
12b879f4 DV |
103 | var d = imageData.data; |
104 | return [d[i], d[i+1], d[i+2], d[i+3]]; | |
105 | }; | |
5bcc58b4 DV |
106 | |
107 | /** | |
108 | * Overrides the browser's built-in XMLHttpRequest with a mock. | |
109 | * Usage: | |
110 | * | |
111 | * var mockXhr = Util.overrideXMLHttpRequest(your_data); | |
112 | * ... call code that does an XHR ... | |
113 | * mockXhr.respond(); // restores default behavior. | |
114 | * ... do your assertions ... | |
115 | */ | |
116 | Util.overrideXMLHttpRequest = function(data) { | |
117 | var originalXMLHttpRequest = XMLHttpRequest; | |
118 | ||
119 | var requests = []; | |
120 | var FakeXMLHttpRequest = function () { | |
121 | requests.push(this); | |
122 | }; | |
123 | FakeXMLHttpRequest.prototype.open = function () {}; | |
124 | FakeXMLHttpRequest.prototype.send = function () { | |
125 | this.readyState = 4; | |
126 | this.status = 200; | |
127 | this.responseText = data; | |
128 | }; | |
129 | FakeXMLHttpRequest.restore = function() { | |
89fdcedb | 130 | window.XMLHttpRequest = originalXMLHttpRequest; |
5bcc58b4 DV |
131 | }; |
132 | FakeXMLHttpRequest.respond = function() { | |
133 | for (var i = 0; i < requests.length; i++) { | |
134 | requests[i].onreadystatechange(); | |
135 | } | |
136 | FakeXMLHttpRequest.restore(); | |
137 | }; | |
89fdcedb | 138 | window.XMLHttpRequest = FakeXMLHttpRequest; |
5bcc58b4 DV |
139 | return FakeXMLHttpRequest; |
140 | }; | |
141 | ||
464b5f50 DV |
142 | /** |
143 | * Format a date as 2000/01/23 | |
b7a1dc22 | 144 | * @param {number} dateMillis Millis since epoch. |
464b5f50 DV |
145 | * @return {string} The date formatted as YYYY-MM-DD. |
146 | */ | |
b7a1dc22 DV |
147 | Util.formatDate = function(dateMillis) { |
148 | return Dygraph.dateString_(dateMillis).slice(0, 10); // 10 == "YYYY/MM/DD".length | |
464b5f50 | 149 | }; |
1b7afc93 DV |
150 | |
151 | /** | |
152 | * Capture console.{log,warn,error} statements into obj. | |
153 | * obj will look like {log:[], warn:[], error:[]} | |
154 | * This returns a function which will restore the original console. | |
155 | */ | |
156 | Util.captureConsole = function(obj) { | |
157 | obj.log = []; | |
158 | obj.warn = []; | |
159 | obj.error = []; | |
160 | var orig = [console.log, console.warn, console.error]; | |
161 | console.log = function(text) { obj.log.push(text); }; | |
162 | console.warn = function(text) { obj.warn.push(text); }; | |
163 | console.error = function(text) { obj.error.push(text); }; | |
164 | ||
165 | return function() { | |
166 | console.log = orig[0]; | |
167 | console.warn = orig[1]; | |
168 | console.error = orig[2]; | |
169 | }; | |
170 | }; |