e692fc61e104600067983a0a231b97c70e8ea1eb
[dygraphs.git] / data_api.js
1 /**
2 * @fileoverview Tests for data access methods.
3 *
4 * @author danvdk@gmail.com (Dan Vanderkam)
5 */
6 import Dygraph from '../../src/dygraph';
7 describe("data-api", function() {
8
9 cleanupAfterEach();
10
11 var opts, graphDiv;
12
13 beforeEach(function() {
14 opts = {
15 width: 480,
16 height: 320
17 };
18
19 graphDiv = document.getElementById("graph");
20 });
21
22 it('testBasicAccessors', function() {
23 var g = new Dygraph(graphDiv, temperature_data, opts);
24
25 assert.equal(365, g.numRows());
26 assert.equal(3, g.numColumns());
27
28 // 2007-01-01,62,39
29 assert.equal(62, g.getValue(0, 1));
30 assert.equal(39, g.getValue(0, 2));
31
32 // 2007-12-31,57,42
33 assert.equal(57, g.getValue(364, 1));
34 assert.equal(42, g.getValue(364, 2));
35 });
36
37
38 it('testAccessorsCustomBars', function() {
39 var g = new Dygraph(graphDiv, data_temp_high_low, {
40 customBars: true
41 });
42
43 assert.equal(1070, g.numRows());
44 assert.equal(3, g.numColumns());
45
46 // 2007-01-01,46;51;56,43;45;48
47 assert.deepEqual([46, 51, 56], g.getValue(0, 1));
48 assert.deepEqual([43, 45, 48], g.getValue(0, 2));
49
50 // 2009-12-05,37;42;47 (i.e. missing second column)
51 assert.deepEqual([37, 42, 47], g.getValue(1069, 1));
52 assert.deepEqual([null, null, null], g.getValue(1069, 2));
53 });
54
55
56 // Regression test for #554.
57 it('testGetRowForX', function() {
58 var g = new Dygraph(graphDiv, [
59 "x,y",
60 "1,2",
61 "3,4",
62 "5,6",
63 "7,8",
64 "9,10"
65 ].join('\n'), opts);
66
67 assert.equal(null, g.getRowForX(0));
68 assert.equal(0, g.getRowForX(1));
69 assert.equal(null, g.getRowForX(2));
70 assert.equal(1, g.getRowForX(3));
71 assert.equal(null, g.getRowForX(4));
72 assert.equal(2, g.getRowForX(5));
73 assert.equal(null, g.getRowForX(6));
74 assert.equal(3, g.getRowForX(7));
75 assert.equal(null, g.getRowForX(8));
76 assert.equal(4, g.getRowForX(9));
77 assert.equal(null, g.getRowForX(10));
78 });
79
80 // If there are rows with identical x-values, getRowForX promises that it will
81 // return the first one.
82 it('testGetRowForXDuplicates', function() {
83 var g = new Dygraph(graphDiv, [
84 "x,y",
85 "1,2", // 0
86 "1,4", // 1
87 "1,6", // 2
88 "1,8", // 3
89 "1,6", // 4
90 "9,2", // 5
91 "9,4",
92 "9,6",
93 "9,8",
94 "9,10"
95 ].join('\n'), opts);
96
97 assert.equal(0, g.getRowForX(1));
98 assert.equal(null, g.getRowForX(2));
99 assert.equal(5, g.getRowForX(9));
100 });
101
102 });