set_data_spec.coffee 6.5 KB

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