label_series_spec.coffee 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. describe '#labelSeries', ->
  2. it 'should generate decade intervals', ->
  3. Morris.labelSeries(
  4. new Date(1952, 0, 1).getTime(),
  5. new Date(2012, 0, 1).getTime(),
  6. 1000
  7. ).should.deep.equal([
  8. ["1960", new Date(1960, 0, 1).getTime()],
  9. ["1970", new Date(1970, 0, 1).getTime()],
  10. ["1980", new Date(1980, 0, 1).getTime()],
  11. ["1990", new Date(1990, 0, 1).getTime()],
  12. ["2000", new Date(2000, 0, 1).getTime()],
  13. ["2010", new Date(2010, 0, 1).getTime()]
  14. ])
  15. Morris.labelSeries(
  16. new Date(1952, 3, 1).getTime(),
  17. new Date(2012, 3, 1).getTime(),
  18. 1000
  19. ).should.deep.equal([
  20. ["1960", new Date(1960, 0, 1).getTime()],
  21. ["1970", new Date(1970, 0, 1).getTime()],
  22. ["1980", new Date(1980, 0, 1).getTime()],
  23. ["1990", new Date(1990, 0, 1).getTime()],
  24. ["2000", new Date(2000, 0, 1).getTime()],
  25. ["2010", new Date(2010, 0, 1).getTime()]
  26. ])
  27. it 'should generate year intervals', ->
  28. Morris.labelSeries(
  29. new Date(2007, 0, 1).getTime(),
  30. new Date(2012, 0, 1).getTime(),
  31. 1000
  32. ).should.deep.equal([
  33. ["2007", new Date(2007, 0, 1).getTime()],
  34. ["2008", new Date(2008, 0, 1).getTime()],
  35. ["2009", new Date(2009, 0, 1).getTime()],
  36. ["2010", new Date(2010, 0, 1).getTime()],
  37. ["2011", new Date(2011, 0, 1).getTime()],
  38. ["2012", new Date(2012, 0, 1).getTime()]
  39. ])
  40. Morris.labelSeries(
  41. new Date(2007, 3, 1).getTime(),
  42. new Date(2012, 3, 1).getTime(),
  43. 1000
  44. ).should.deep.equal([
  45. ["2008", new Date(2008, 0, 1).getTime()],
  46. ["2009", new Date(2009, 0, 1).getTime()],
  47. ["2010", new Date(2010, 0, 1).getTime()],
  48. ["2011", new Date(2011, 0, 1).getTime()],
  49. ["2012", new Date(2012, 0, 1).getTime()]
  50. ])
  51. it 'should generate month intervals', ->
  52. Morris.labelSeries(
  53. new Date(2012, 0, 1).getTime(),
  54. new Date(2012, 5, 1).getTime(),
  55. 1000
  56. ).should.deep.equal([
  57. ["2012-01", new Date(2012, 0, 1).getTime()],
  58. ["2012-02", new Date(2012, 1, 1).getTime()],
  59. ["2012-03", new Date(2012, 2, 1).getTime()],
  60. ["2012-04", new Date(2012, 3, 1).getTime()],
  61. ["2012-05", new Date(2012, 4, 1).getTime()],
  62. ["2012-06", new Date(2012, 5, 1).getTime()]
  63. ])
  64. it 'should generate week intervals', ->
  65. Morris.labelSeries(
  66. new Date(2012, 0, 1).getTime(),
  67. new Date(2012, 1, 10).getTime(),
  68. 1000
  69. ).should.deep.equal([
  70. ["2012-01-01", new Date(2012, 0, 1).getTime()],
  71. ["2012-01-08", new Date(2012, 0, 8).getTime()],
  72. ["2012-01-15", new Date(2012, 0, 15).getTime()],
  73. ["2012-01-22", new Date(2012, 0, 22).getTime()],
  74. ["2012-01-29", new Date(2012, 0, 29).getTime()],
  75. ["2012-02-05", new Date(2012, 1, 5).getTime()]
  76. ])
  77. it 'should generate day intervals', ->
  78. Morris.labelSeries(
  79. new Date(2012, 0, 1).getTime(),
  80. new Date(2012, 0, 6).getTime(),
  81. 1000
  82. ).should.deep.equal([
  83. ["2012-01-01", new Date(2012, 0, 1).getTime()],
  84. ["2012-01-02", new Date(2012, 0, 2).getTime()],
  85. ["2012-01-03", new Date(2012, 0, 3).getTime()],
  86. ["2012-01-04", new Date(2012, 0, 4).getTime()],
  87. ["2012-01-05", new Date(2012, 0, 5).getTime()],
  88. ["2012-01-06", new Date(2012, 0, 6).getTime()]
  89. ])
  90. it 'should generate hour intervals', ->
  91. Morris.labelSeries(
  92. new Date(2012, 0, 1, 0).getTime(),
  93. new Date(2012, 0, 1, 5).getTime(),
  94. 1000
  95. ).should.deep.equal([
  96. ["00:00", new Date(2012, 0, 1, 0).getTime()],
  97. ["01:00", new Date(2012, 0, 1, 1).getTime()],
  98. ["02:00", new Date(2012, 0, 1, 2).getTime()],
  99. ["03:00", new Date(2012, 0, 1, 3).getTime()],
  100. ["04:00", new Date(2012, 0, 1, 4).getTime()],
  101. ["05:00", new Date(2012, 0, 1, 5).getTime()]
  102. ])
  103. it 'should generate half-hour intervals', ->
  104. Morris.labelSeries(
  105. new Date(2012, 0, 1, 0, 0).getTime(),
  106. new Date(2012, 0, 1, 2, 30).getTime(),
  107. 1000
  108. ).should.deep.equal([
  109. ["00:00", new Date(2012, 0, 1, 0, 0).getTime()],
  110. ["00:30", new Date(2012, 0, 1, 0, 30).getTime()],
  111. ["01:00", new Date(2012, 0, 1, 1, 0).getTime()],
  112. ["01:30", new Date(2012, 0, 1, 1, 30).getTime()],
  113. ["02:00", new Date(2012, 0, 1, 2, 0).getTime()],
  114. ["02:30", new Date(2012, 0, 1, 2, 30).getTime()]
  115. ])
  116. Morris.labelSeries(
  117. new Date(2012, 4, 12, 0, 0).getTime(),
  118. new Date(2012, 4, 12, 2, 30).getTime(),
  119. 1000
  120. ).should.deep.equal([
  121. ["00:00", new Date(2012, 4, 12, 0, 0).getTime()],
  122. ["00:30", new Date(2012, 4, 12, 0, 30).getTime()],
  123. ["01:00", new Date(2012, 4, 12, 1, 0).getTime()],
  124. ["01:30", new Date(2012, 4, 12, 1, 30).getTime()],
  125. ["02:00", new Date(2012, 4, 12, 2, 0).getTime()],
  126. ["02:30", new Date(2012, 4, 12, 2, 30).getTime()]
  127. ])
  128. it 'should generate fifteen-minute intervals', ->
  129. Morris.labelSeries(
  130. new Date(2012, 0, 1, 0, 0).getTime(),
  131. new Date(2012, 0, 1, 1, 15).getTime(),
  132. 1000
  133. ).should.deep.equal([
  134. ["00:00", new Date(2012, 0, 1, 0, 0).getTime()],
  135. ["00:15", new Date(2012, 0, 1, 0, 15).getTime()],
  136. ["00:30", new Date(2012, 0, 1, 0, 30).getTime()],
  137. ["00:45", new Date(2012, 0, 1, 0, 45).getTime()],
  138. ["01:00", new Date(2012, 0, 1, 1, 0).getTime()],
  139. ["01:15", new Date(2012, 0, 1, 1, 15).getTime()]
  140. ])
  141. Morris.labelSeries(
  142. new Date(2012, 4, 12, 0, 0).getTime(),
  143. new Date(2012, 4, 12, 1, 15).getTime(),
  144. 1000
  145. ).should.deep.equal([
  146. ["00:00", new Date(2012, 4, 12, 0, 0).getTime()],
  147. ["00:15", new Date(2012, 4, 12, 0, 15).getTime()],
  148. ["00:30", new Date(2012, 4, 12, 0, 30).getTime()],
  149. ["00:45", new Date(2012, 4, 12, 0, 45).getTime()],
  150. ["01:00", new Date(2012, 4, 12, 1, 0).getTime()],
  151. ["01:15", new Date(2012, 4, 12, 1, 15).getTime()]
  152. ])
  153. it 'should override automatic intervals', ->
  154. Morris.labelSeries(
  155. new Date(2011, 11, 12).getTime(),
  156. new Date(2012, 0, 12).getTime(),
  157. 1000,
  158. "year"
  159. ).should.deep.equal([
  160. ["2012", new Date(2012, 0, 1).getTime()]
  161. ])
  162. it 'should apply custom formatters', ->
  163. Morris.labelSeries(
  164. new Date(2012, 0, 1).getTime(),
  165. new Date(2012, 0, 6).getTime(),
  166. 1000,
  167. "day",
  168. (d) -> "#{d.getMonth()+1}/#{d.getDate()}/#{d.getFullYear()}"
  169. ).should.deep.equal([
  170. ["1/1/2012", new Date(2012, 0, 1).getTime()],
  171. ["1/2/2012", new Date(2012, 0, 2).getTime()],
  172. ["1/3/2012", new Date(2012, 0, 3).getTime()],
  173. ["1/4/2012", new Date(2012, 0, 4).getTime()],
  174. ["1/5/2012", new Date(2012, 0, 5).getTime()],
  175. ["1/6/2012", new Date(2012, 0, 6).getTime()]
  176. ])