2 * Unit tests for GViz data table support.
5 describe('gviz', function() {
7 beforeEach(function() {
8 document
.body
.innerHTML
= "<div id='graph'></div>";
11 afterEach(function() {
14 // This is a fake version of the gviz DataTable API, which can only be
15 // sourced using the google js loader.
17 // Their example of the "data" structure is:
18 // cols: [{id: 'task', label: 'Task', type: 'string'},
19 // {id: 'hours', label: 'Hours per Day', type: 'number'}],
20 // rows: [{c:[{v: 'Work'}, {v: 11}]},
21 // {c:[{v: 'Eat'}, {v: 2}]},
22 // {c:[{v: 'Commute'}, {v: 2}]},
23 // {c:[{v: 'Watch TV'}, {v:2}]},
24 // {c:[{v: 'Sleep'}, {v:7, f:'7.000'}]}
27 // https://developers.google.com/chart/interactive/docs/reference
#DataTable
28 var FakeDataTable
= function(data
) {
31 FakeDataTable
.prototype.getNumberOfColumns
= function() {
32 return this.data
.cols
.length
;
34 FakeDataTable
.prototype.getNumberOfRows
= function() {
35 return this.data
.rows
.length
;
37 FakeDataTable
.prototype.getColumnType
= function(idx
) {
38 return this.data
.cols
[idx
].type
;
40 FakeDataTable
.prototype.getColumnLabel
= function(idx
) {
41 return this.data
.cols
[idx
].label
;
43 FakeDataTable
.prototype.getValue
= function(row
, col
) {
44 return this.data
.rows
[row
].c
[col
].v
;
46 FakeDataTable
.prototype.getColumnRange
= function(col
) {
47 throw 'Not Implemented';
50 // This mirrors http://dygraphs.com/tests
/gviz
.html
51 var numericData
= new FakeDataTable({
52 cols
: [{id
:"",label
:"X",type
:"number"},
53 {id
:"",label
:"A",type
:"number"},
54 {id
:"",label
:"B",type
:"number"}],
55 rows
: [{c
:[{v
:0},{v
:1},{v
:7}]},
56 {c
:[{v
:1},{v
:2},{v
:4}]},
57 {c
:[{v
:2},{v
:3},{v
:3}]},
58 {c
:[{v
:3},{v
:4},{v
:0}]}]
61 it('should parse simple data tables', function() {
62 var g
= new Dygraph('graph', numericData
);
63 assert
.equal(4, g
.numRows());
64 assert
.equal(3, g
.numColumns());
65 assert
.equal(0, g
.getValue(0, 0));
66 assert
.equal(1, g
.getValue(0, 1));
67 assert
.equal(7, g
.getValue(0, 2));
68 assert
.equal(3, g
.getValue(3, 0));
69 assert
.equal(4, g
.getValue(3, 1));
70 assert
.equal(0, g
.getValue(3, 2));
71 assert
.deepEqual(['X', 'A', 'B'], g
.getLabels());
74 it('should parse tables with annotations', function() {
75 // Data from https://developers.google.com/chart/interactive/docs/gallery/annotatedtimeline
76 var data
= new FakeDataTable({
78 {label
: "Date", type
: "date" },
79 {label
: "Sold Pencils", type
: "number" },
80 {label
: "title1", type
: "string" },
81 {label
: "text1", type
: "string" },
82 {label
: "Sold Pens", type
: "number" },
83 {label
: "title2", type
: "string" },
84 {label
: "text2", type
: "string" }
87 {c
: [{v
: new Date(2008, 1, 1)}, {v
: 30000}, {v
: null}, {v
: null},
88 {v
: 40645}, {v
: null}, {v
: null}]},
89 {c
: [{v
: new Date(2008, 1, 2)}, {v
: 14045}, {v
: null}, {v
: null},
90 {v
: 20374}, {v
: null}, {v
: null}]},
91 {c
: [{v
: new Date(2008, 1, 3)}, {v
: 55022}, {v
: null}, {v
: null},
92 {v
: 50766}, {v
: null}, {v
: null}]},
93 {c
: [{v
: new Date(2008, 1, 4)}, {v
: 75284}, {v
: null}, {v
: null},
94 {v
: 14334}, {v
: "Out of Stock"}, {v
: "Ran out of stock"}]},
95 {c
: [{v
: new Date(2008, 1, 5)}, {v
: 41476}, {v
: "Bought Pens"},
96 {v
: "Bought 200k pens" }, {v
: 66467}, {v
: null}, {v
: null}]},
97 {c
: [{v
: new Date(2008, 1, 6)}, {v
: 33322}, {v
: null}, {v
: null},
98 {v
: 39463}, {v
: null}, {v
: null}]}
102 var g
= new Dygraph('graph', data
, {displayAnnotations
: true});
104 var annEls
= document
.getElementsByClassName('dygraphDefaultAnnotation');
105 assert
.equal(2, annEls
.length
);
107 var annotations
= g
.annotations();
108 assert
.equal(2, annotations
.length
);
109 var a0
= annotations
[0];
111 text
: 'Out of Stock\nRan out of stock',
113 xval
: new Date(2008, 1, 4).getTime(),
118 it('should parse tables with dates', function() {
119 // This mirrors http://dygraphs.com/tests
/gviz
.html
120 var data
= new FakeDataTable({
121 cols
: [{id
:"",label
:"Date",type
:"datetime"},
122 {id
:"",label
:"Column A",type
:"number"},
123 {id
:"",label
:"Column B",type
:"number"}],
124 rows
: [{c
:[{v
:new Date(2009, 6, 1)},{v
:1},{v
:7}]},
125 {c
:[{v
:new Date(2009, 6, 8)},{v
:2},{v
:4}]},
126 {c
:[{v
:new Date(2009, 6, 15)},{v
:3},{v
:3}]},
127 {c
:[{v
:new Date(2009, 6, 22)},{v
:4},{v
:0}]}]
130 var g
= new Dygraph('graph', data
);
131 assert
.equal(4, g
.numRows());
132 assert
.equal(3, g
.numColumns());
133 assert
.equal(new Date(2009, 6, 1).getTime(), g
.getValue(0, 0));
134 assert
.equal(1, g
.getValue(0, 1));
135 assert
.equal(7, g
.getValue(0, 2));
136 assert
.deepEqual(['Date', 'Column A', 'Column B'], g
.getLabels());
139 // it('should parse tables with error bars', function() {
142 it('should implement the gviz API', function() {
143 var g
= new Dygraph
.GVizChart(document
.getElementById('graph'));
146 g
.setSelection([{row
: 0}]);
147 assert
.equal('0: A: 1 B: 7', Util
.getLegend());
148 assert
.deepEqual([{row
: 0, column
: 1}, {row
: 0, column
: 2}], g
.getSelection());
150 assert
.deepEqual([], g
.getSelection());