X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=auto_tests%2Ftests%2FUtil.js;h=2d652eda7ab43032a037f0a8835c3bea36517b59;hb=2dde24e570f97f8d0f69445563d068a5678aa475;hp=7ab0b9a485b69118fb390acb9ecc6ee9b722660a;hpb=fa607ffbc86e564cd44c2626f14ef799518b8252;p=dygraphs.git diff --git a/auto_tests/tests/Util.js b/auto_tests/tests/Util.js index 7ab0b9a..2d652ed 100644 --- a/auto_tests/tests/Util.js +++ b/auto_tests/tests/Util.js @@ -6,8 +6,9 @@ var Util = {}; /** - * Get the y-labels for a given axis. You can specify a parent if more than one - * graph is on the document. + * Get the y-labels for a given axis. + * + * You can specify a parent if more than one graph is in the document. */ Util.getYLabels = function(axis_num, parent) { axis_num = axis_num || ""; @@ -18,7 +19,22 @@ Util.getYLabels = function(axis_num, parent) { ary.push(y_labels[i].innerHTML); } return ary; -} +}; + +/** + * Get the x-labels for a given axis. + * + * You can specify a parent if more than one graph is in the document. + */ +Util.getXLabels = function(parent) { + parent = parent || document; + var x_labels = parent.getElementsByClassName("dygraph-axis-label-x"); + var ary = []; + for (var i = 0; i < x_labels.length; i++) { + ary.push(x_labels[i].innerHTML); + } + return ary; +}; /** * Returns all text in tags w/ a given css class, sorted. @@ -33,4 +49,49 @@ Util.getClassTexts = function(css_class, parent) { } texts.sort(); return texts; -} +}; + +Util.getLegend = function(parent) { + parent = parent || document; + var legend = parent.getElementsByClassName("dygraph-legend")[0]; + var re = new RegExp(String.fromCharCode(160), 'g'); + return legend.textContent.replace(re, ' '); +}; + +/** + * Assert that all elements have a certain style property. + */ +Util.assertStyleOfChildren = function(selector, property, expectedValue) { + assertTrue(selector.length > 0); + $.each(selector, function(idx, child) { + assertEquals(expectedValue, $(child).css(property)); + }); +}; + + +/** + * Takes in an array of strings and returns an array of floats. + */ +Util.makeNumbers = function(ary) { + var ret = []; + for (var i = 0; i < ary.length; i++) { + ret.push(parseFloat(ary[i])); + } + return ret; +}; + + +/** + * Sample a pixel from the canvas. + * Returns an [r, g, b, a] tuple where each values is in [0, 255]. + */ +Util.samplePixel = function(canvas, x, y) { + var ctx = canvas.getContext("2d"); // bypasses Proxy if applied. + + // TODO(danvk): Any performance issues with this? + var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height); + + var i = 4 * (x + imageData.width * y); + var d = imageData.data; + return [d[i], d[i+1], d[i+2], d[i+3]]; +};