Merge branch 'master' of github.com:danvk/dygraphs
[dygraphs.git] / auto_tests / tests / Util.js
... / ...
CommitLineData
1/**
2 * @fileoverview Utility functions for Dygraphs.
3 *
4 * @author konigsberg@google.com (Robert Konigsberg)
5 */
6var 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 */
13Util.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);
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 */
29Util.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);
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 */
43Util.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
54Util.getLegend = function(parent) {
55 parent = parent || document;
56 var legend = parent.getElementsByClassName("dygraph-legend")[0];
57 return legend.textContent;
58};
59
60/**
61 * Assert that all the elements in 'parent' with class 'className' is
62 * the expected font size.
63 */
64Util.assertFontSizes = function(parent, className, expectedSize) {
65 var expectedSizePx = expectedSize + "px";
66 var labels = parent.getElementsByClassName(className);
67 assertTrue(labels.length > 0);
68
69 // window.getComputedStyle is apparently compatible with all browsers
70 // (IE first became compatible with IE9.)
71 // If this test fails on earlier browsers, then enable something like this,
72 // because the font size is set by the parent div.
73 // if (!window.getComputedStyle) {
74 // fontSize = label.parentElement.style.fontSize;
75 // }
76 for (var idx = 0; idx < labels.length; idx++) {
77 var label = labels[idx];
78 var fontSize = window.getComputedStyle(label).fontSize;
79 assertEquals(expectedSizePx, fontSize);
80 }
81};
82
83
84/**
85 * Takes in an array of strings and returns an array of floats.
86 */
87Util.makeNumbers = function(ary) {
88 var ret = [];
89 for (var i = 0; i < ary.length; i++) {
90 ret.push(parseFloat(ary[i]));
91 }
92 return ret;
93};