36532cd32f85904306cd87f490032f23cc17ab45
2 * @fileoverview Utility functions for Dygraphs.
4 * @author konigsberg@google.com (Robert Konigsberg)
7 import * as utils from
'../../src/dygraph-utils';
12 * Get the y-labels for a given axis.
14 * You can specify a parent if more than one graph is in the document.
16 Util
.getYLabels
= function(axis_num
, parent
) {
17 axis_num
= axis_num
|| "";
18 parent
= parent
|| document
;
19 var y_labels
= parent
.getElementsByClassName("dygraph-axis-label-y" + axis_num
);
21 for (var i
= 0; i
< y_labels
.length
; i
++) {
22 ary
.push(y_labels
[i
].innerHTML
.replace(/ | /g, ' '));
28 * Get the x-labels for a given axis.
30 * You can specify a parent if more than one graph is in the document.
32 Util
.getXLabels
= function(parent
) {
33 parent
= parent
|| document
;
34 var x_labels
= parent
.getElementsByClassName("dygraph-axis-label-x");
36 for (var i
= 0; i
< x_labels
.length
; i
++) {
37 ary
.push(x_labels
[i
].innerHTML
.replace(/ | /g, ' '));
43 * Returns all text in tags w/ a given css class, sorted.
44 * You can specify a parent if more than one graph is on the document.
46 Util
.getClassTexts
= function(css_class
, parent
) {
47 parent
= parent
|| document
;
49 var els
= parent
.getElementsByClassName(css_class
);
50 for (var i
= 0; i
< els
.length
; i
++) {
51 texts
[i
] = els
[i
].textContent
;
57 // Convert to a normal space
58 Util
.nbspToSpace
= function(str
) {
59 var re
= new RegExp(String
.fromCharCode(160), 'g');
60 return str
.replace(re
, ' ');
63 Util
.getLegend
= function(parent
) {
64 parent
= parent
|| document
;
65 var legend
= parent
.getElementsByClassName("dygraph-legend")[0];
66 return Util
.nbspToSpace(legend
.textContent
);
70 * Assert that all elements have a certain style property.
72 Util
.assertStyleOfChildren
= function(selector
, property
, expectedValue
) {
73 assert
.isTrue(selector
.length
> 0);
74 for (var idx
= 0; idx
< selector
.length
; idx
++) {
75 var child
= selector
[idx
];
76 assert
.equal(expectedValue
, window
.getComputedStyle(child
)[property
]);
82 * Takes in an array of strings and returns an array of floats.
84 Util
.makeNumbers
= function(ary
) {
86 for (var i
= 0; i
< ary
.length
; i
++) {
87 ret
.push(parseFloat(ary
[i
]));
94 * Sample a pixel from the canvas.
95 * Returns an [r, g, b, a] tuple where each values is in [0, 255].
96 * This is _very_ slow! If you want to sample many pixels, use PixelSampler.
98 Util
.samplePixel
= function(canvas
, x
, y
) {
99 var ctx
= canvas
.getContext("2d"); // bypasses Proxy if applied.
101 var imageData
= ctx
.getImageData(0, 0, canvas
.width
, canvas
.height
);
103 var scale
= utils
.getContextPixelRatio(ctx
);
105 var i
= 4 * (x
* scale
+ imageData
.width
* y
* scale
);
106 var d
= imageData
.data
;
107 return [d
[i
], d
[i
+1], d
[i
+2], d
[i
+3]];
111 * Overrides the browser's built-in XMLHttpRequest with a mock.
114 * var mockXhr = Util.overrideXMLHttpRequest(your_data);
115 * ... call code that does an XHR ...
116 * mockXhr.respond(); // restores default behavior.
117 * ... do your assertions ...
119 Util
.overrideXMLHttpRequest
= function(data
) {
120 var originalXMLHttpRequest
= XMLHttpRequest
;
123 var FakeXMLHttpRequest
= function () {
126 FakeXMLHttpRequest
.prototype.open
= function () {};
127 FakeXMLHttpRequest
.prototype.send
= function () {
130 this.responseText
= data
;
132 FakeXMLHttpRequest
.restore
= function() {
133 window
.XMLHttpRequest
= originalXMLHttpRequest
;
135 FakeXMLHttpRequest
.respond
= function() {
136 for (var i
= 0; i
< requests
.length
; i
++) {
137 requests
[i
].onreadystatechange();
139 FakeXMLHttpRequest
.restore();
141 window
.XMLHttpRequest
= FakeXMLHttpRequest
;
142 return FakeXMLHttpRequest
;
146 * Format a date as 2000/01/23
147 * @param {number} dateMillis Millis since epoch.
148 * @return {string} The date formatted as YYYY-MM-DD.
150 Util
.formatDate
= function(dateMillis
) {
151 return utils
.dateString_(dateMillis
).slice(0, 10); // 10 == "YYYY/MM
/DD
".length
155 * Capture console.{log,warn,error} statements into obj.
156 * obj will look like {log:[], warn:[], error:[]}
157 * This returns a function which will restore the original console.
159 Util.captureConsole = function(obj) {
163 var orig = [console.log, console.warn, console.error];
164 console.log = function(text) { obj.log.push(text); };
165 console.warn = function(text) { obj.warn.push(text); };
166 console.error = function(text) { obj.error.push(text); };
169 console.log = orig[0];
170 console.warn = orig[1];
171 console.error = orig[2];