| 5 |
lars |
1 |
describe 'Morris.Grid#setData', ->
|
|
|
2 |
|
|
|
3 |
it 'should not alter user-supplied data', ->
|
|
|
4 |
my_data = [{x: 1, y: 1}, {x: 2, y: 2}]
|
|
|
5 |
expected_data = [{x: 1, y: 1}, {x: 2, y: 2}]
|
|
|
6 |
Morris.Line
|
|
|
7 |
element: 'graph'
|
|
|
8 |
data: my_data
|
|
|
9 |
xkey: 'x'
|
|
|
10 |
ykeys: ['y']
|
|
|
11 |
labels: ['dontcare']
|
|
|
12 |
my_data.should.deep.equal expected_data
|
|
|
13 |
|
|
|
14 |
describe 'ymin/ymax', ->
|
|
|
15 |
beforeEach ->
|
|
|
16 |
@defaults =
|
|
|
17 |
element: 'graph'
|
|
|
18 |
xkey: 'x'
|
|
|
19 |
ykeys: ['y', 'z']
|
|
|
20 |
labels: ['y', 'z']
|
|
|
21 |
|
|
|
22 |
it 'should use a user-specified minimum and maximum value', ->
|
|
|
23 |
line = Morris.Line $.extend @defaults,
|
|
|
24 |
data: [{x: 1, y: 1}]
|
|
|
25 |
ymin: 10
|
|
|
26 |
ymax: 20
|
|
|
27 |
line.ymin.should.equal 10
|
|
|
28 |
line.ymax.should.equal 20
|
|
|
29 |
|
|
|
30 |
describe 'auto', ->
|
|
|
31 |
|
|
|
32 |
it 'should automatically calculate the minimum and maximum value', ->
|
|
|
33 |
line = Morris.Line $.extend @defaults,
|
|
|
34 |
data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}]
|
|
|
35 |
ymin: 'auto'
|
|
|
36 |
ymax: 'auto'
|
|
|
37 |
line.ymin.should.equal 10
|
|
|
38 |
line.ymax.should.equal 15
|
|
|
39 |
|
|
|
40 |
it 'should automatically calculate the minimum and maximum value given no y data', ->
|
|
|
41 |
line = Morris.Line $.extend @defaults,
|
|
|
42 |
data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}]
|
|
|
43 |
ymin: 'auto'
|
|
|
44 |
ymax: 'auto'
|
|
|
45 |
line.ymin.should.equal 0
|
|
|
46 |
line.ymax.should.equal 1
|
|
|
47 |
|
|
|
48 |
describe 'auto [n]', ->
|
|
|
49 |
|
|
|
50 |
it 'should automatically calculate the minimum and maximum value', ->
|
|
|
51 |
line = Morris.Line $.extend @defaults,
|
|
|
52 |
data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}]
|
|
|
53 |
ymin: 'auto 11'
|
|
|
54 |
ymax: 'auto 13'
|
|
|
55 |
line.ymin.should.equal 10
|
|
|
56 |
line.ymax.should.equal 15
|
|
|
57 |
|
|
|
58 |
it 'should automatically calculate the minimum and maximum value given no data', ->
|
|
|
59 |
line = Morris.Line $.extend @defaults,
|
|
|
60 |
data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}]
|
|
|
61 |
ymin: 'auto 11'
|
|
|
62 |
ymax: 'auto 13'
|
|
|
63 |
line.ymin.should.equal 11
|
|
|
64 |
line.ymax.should.equal 13
|
|
|
65 |
|
|
|
66 |
it 'should use a user-specified minimum and maximum value', ->
|
|
|
67 |
line = Morris.Line $.extend @defaults,
|
|
|
68 |
data: [{x: 1, y: 10}, {x: 2, y: 15}, {x: 3, y: null}, {x: 4}]
|
|
|
69 |
ymin: 'auto 5'
|
|
|
70 |
ymax: 'auto 20'
|
|
|
71 |
line.ymin.should.equal 5
|
|
|
72 |
line.ymax.should.equal 20
|
|
|
73 |
|
|
|
74 |
it 'should use a user-specified minimum and maximum value given no data', ->
|
|
|
75 |
line = Morris.Line $.extend @defaults,
|
|
|
76 |
data: [{x: 1}, {x: 2}, {x: 3}, {x: 4}]
|
|
|
77 |
ymin: 'auto 5'
|
|
|
78 |
ymax: 'auto 20'
|
|
|
79 |
line.ymin.should.equal 5
|
|
|
80 |
line.ymax.should.equal 20
|
|
|
81 |
|
|
|
82 |
describe 'xmin/xmax', ->
|
|
|
83 |
|
|
|
84 |
it 'should calculate the horizontal range', ->
|
|
|
85 |
line = Morris.Line
|
|
|
86 |
element: 'graph'
|
|
|
87 |
data: [{x: 2, y: 2}, {x: 1, y: 1}, {x: 4, y: 4}, {x: 3, y: 3}]
|
|
|
88 |
xkey: 'x'
|
|
|
89 |
ykeys: ['y']
|
|
|
90 |
labels: ['y']
|
|
|
91 |
line.xmin.should == 1
|
|
|
92 |
line.xmax.should == 4
|
|
|
93 |
|
|
|
94 |
it "should pad the range if there's only one data point", ->
|
|
|
95 |
line = Morris.Line
|
|
|
96 |
element: 'graph'
|
|
|
97 |
data: [{x: 2, y: 2}]
|
|
|
98 |
xkey: 'x'
|
|
|
99 |
ykeys: ['y']
|
|
|
100 |
labels: ['y']
|
|
|
101 |
line.xmin.should == 1
|
|
|
102 |
line.xmax.should == 3
|
|
|
103 |
|
|
|
104 |
describe 'sorting', ->
|
|
|
105 |
|
|
|
106 |
it 'should sort data when parseTime is true', ->
|
|
|
107 |
line = Morris.Line
|
|
|
108 |
element: 'graph'
|
|
|
109 |
data: [
|
|
|
110 |
{x: '2012 Q1', y: 2},
|
|
|
111 |
{x: '2012 Q3', y: 1},
|
|
|
112 |
{x: '2012 Q4', y: 4},
|
|
|
113 |
{x: '2012 Q2', y: 3}]
|
|
|
114 |
xkey: 'x'
|
|
|
115 |
ykeys: ['y']
|
|
|
116 |
labels: ['y']
|
|
|
117 |
line.data.map((row) -> row.label).should.deep.equal ['2012 Q1', '2012 Q2', '2012 Q3', '2012 Q4']
|
|
|
118 |
|
|
|
119 |
it 'should not sort data when parseTime is false', ->
|
|
|
120 |
line = Morris.Line
|
|
|
121 |
element: 'graph'
|
|
|
122 |
data: [{x: 1, y: 2}, {x: 4, y: 1}, {x: 3, y: 4}, {x: 2, y: 3}]
|
|
|
123 |
xkey: 'x'
|
|
|
124 |
ykeys: ['y']
|
|
|
125 |
labels: ['y']
|
|
|
126 |
parseTime: false
|
|
|
127 |
line.data.map((row) -> row.label).should.deep.equal [1, 4, 3, 2]
|
|
|
128 |
|
|
|
129 |
describe 'timestamp data', ->
|
|
|
130 |
|
|
|
131 |
it 'should generate default labels for timestamp x-values', ->
|
|
|
132 |
d = [
|
|
|
133 |
new Date 2012, 0, 1
|
|
|
134 |
new Date 2012, 0, 2
|
|
|
135 |
new Date 2012, 0, 3
|
|
|
136 |
new Date 2012, 0, 4
|
|
|
137 |
]
|
|
|
138 |
line = Morris.Line
|
|
|
139 |
element: 'graph'
|
|
|
140 |
data: [
|
|
|
141 |
{x: d[0].getTime(), y: 2},
|
|
|
142 |
{x: d[1].getTime(), y: 1},
|
|
|
143 |
{x: d[2].getTime(), y: 4},
|
|
|
144 |
{x: d[3].getTime(), y: 3}]
|
|
|
145 |
xkey: 'x'
|
|
|
146 |
ykeys: ['y']
|
|
|
147 |
labels: ['y']
|
|
|
148 |
line.data.map((row) -> row.label).should.deep.equal d.map((t) -> t.toString())
|
|
|
149 |
|
|
|
150 |
it 'should use a user-supplied formatter for labels', ->
|
|
|
151 |
line = Morris.Line
|
|
|
152 |
element: 'graph'
|
|
|
153 |
data: [
|
|
|
154 |
{x: new Date(2012, 0, 1).getTime(), y: 2},
|
|
|
155 |
{x: new Date(2012, 0, 2).getTime(), y: 1},
|
|
|
156 |
{x: new Date(2012, 0, 3).getTime(), y: 4},
|
|
|
157 |
{x: new Date(2012, 0, 4).getTime(), y: 3}]
|
|
|
158 |
xkey: 'x'
|
|
|
159 |
ykeys: ['y']
|
|
|
160 |
labels: ['y']
|
|
|
161 |
dateFormat: (ts) ->
|
|
|
162 |
date = new Date(ts)
|
|
|
163 |
"#{date.getFullYear()}-#{date.getMonth()+1}-#{date.getDate()}"
|
|
|
164 |
line.data.map((row) -> row.label).should.deep.equal ['2012-1-1', '2012-1-2', '2012-1-3', '2012-1-4']
|
|
|
165 |
|
|
|
166 |
it 'should parse y-values in strings', ->
|
|
|
167 |
line = Morris.Line
|
|
|
168 |
element: 'graph'
|
|
|
169 |
data: [{x: 2, y: '12'}, {x: 1, y: '13.5'}, {x: 4, y: '14'}, {x: 3, y: '16'}]
|
|
|
170 |
xkey: 'x'
|
|
|
171 |
ykeys: ['y']
|
|
|
172 |
labels: ['y']
|
|
|
173 |
line.ymin.should == 12
|
|
|
174 |
line.ymax.should == 16
|
|
|
175 |
line.data.map((row) -> row.y).should.deep.equal [[13.5], [12], [16], [14]]
|
|
|
176 |
|
|
|
177 |
it 'should clear the chart when empty data is supplied', ->
|
|
|
178 |
line = Morris.Line
|
|
|
179 |
element: 'graph',
|
|
|
180 |
data: [{x: 2, y: '12'}, {x: 1, y: '13.5'}, {x: 4, y: '14'}, {x: 3, y: '16'}]
|
|
|
181 |
xkey: 'x'
|
|
|
182 |
ykeys: ['y']
|
|
|
183 |
labels: ['y']
|
|
|
184 |
line.data.length.should.equal 4
|
|
|
185 |
line.setData([])
|
|
|
186 |
line.data.length.should.equal 0
|
|
|
187 |
line.setData([{x: 2, y: '12'}, {x: 1, y: '13.5'}, {x: 4, y: '14'}, {x: 3, y: '16'}])
|
|
|
188 |
line.data.length.should.equal 4
|
|
|
189 |
|
|
|
190 |
it 'should be able to add data if the chart is initialised with empty data', ->
|
|
|
191 |
line = Morris.Line
|
|
|
192 |
element: 'graph',
|
|
|
193 |
data: []
|
|
|
194 |
xkey: 'x'
|
|
|
195 |
ykeys: ['y']
|
|
|
196 |
labels: ['y']
|
|
|
197 |
line.data.length.should.equal 0
|
|
|
198 |
line.setData([{x: 2, y: '12'}, {x: 1, y: '13.5'}, {x: 4, y: '14'}, {x: 3, y: '16'}])
|
|
|
199 |
line.data.length.should.equal 4
|
|
|
200 |
|
|
|
201 |
it 'should automatically choose significant numbers for y-labels', ->
|
|
|
202 |
line = Morris.Line
|
|
|
203 |
element: 'graph',
|
|
|
204 |
data: [{x: 1, y: 0}, {x: 2, y: 3600}]
|
|
|
205 |
xkey: 'x'
|
|
|
206 |
ykeys: ['y']
|
|
|
207 |
labels: ['y']
|
|
|
208 |
line.grid.should == [0, 1000, 2000, 3000, 4000]
|