bar_spec.coffee 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. describe 'Morris.Bar', ->
  2. describe 'when using vertical grid', ->
  3. defaults =
  4. element: 'graph'
  5. data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}]
  6. xkey: 'x'
  7. ykeys: ['y', 'z']
  8. labels: ['Y', 'Z']
  9. barColors: [ '#0b62a4', '#7a92a3']
  10. gridLineColor: '#aaa'
  11. gridStrokeWidth: 0.5
  12. gridTextColor: '#888'
  13. gridTextSize: 12
  14. verticalGridCondition: (index) -> index % 2
  15. verticalGridColor: '#888888'
  16. verticalGridOpacity: '0.2'
  17. describe 'svg structure', ->
  18. it 'should contain extra rectangles for vertical grid', ->
  19. $('#graph').css('height', '250px').css('width', '800px')
  20. chart = Morris.Bar $.extend {}, defaults
  21. $('#graph').find("rect").size().should.equal 6
  22. describe 'svg attributes', ->
  23. it 'should have to bars with verticalGrid.color', ->
  24. chart = Morris.Bar $.extend {}, defaults
  25. $('#graph').find("rect[fill='#{defaults.verticalGridColor}']").size().should.equal 2
  26. it 'should have to bars with verticalGrid.color', ->
  27. chart = Morris.Bar $.extend {}, defaults
  28. $('#graph').find("rect[fill-opacity='#{defaults.verticalGridOpacity}']").size().should.equal 2
  29. describe 'svg structure', ->
  30. defaults =
  31. element: 'graph'
  32. data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}]
  33. xkey: 'x'
  34. ykeys: ['y', 'z']
  35. labels: ['Y', 'Z']
  36. it 'should contain a rect for each bar', ->
  37. chart = Morris.Bar $.extend {}, defaults
  38. $('#graph').find("rect").size().should.equal 4
  39. it 'should contain 5 grid lines', ->
  40. chart = Morris.Bar $.extend {}, defaults
  41. $('#graph').find("path").size().should.equal 5
  42. it 'should contain 7 text elements', ->
  43. chart = Morris.Bar $.extend {}, defaults
  44. $('#graph').find("text").size().should.equal 7
  45. describe 'svg attributes', ->
  46. defaults =
  47. element: 'graph'
  48. data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}]
  49. xkey: 'x'
  50. ykeys: ['y', 'z']
  51. labels: ['Y', 'Z']
  52. barColors: [ '#0b62a4', '#7a92a3']
  53. gridLineColor: '#aaa'
  54. gridStrokeWidth: 0.5
  55. gridTextColor: '#888'
  56. gridTextSize: 12
  57. it 'should have a bar with the first default color', ->
  58. chart = Morris.Bar $.extend {}, defaults
  59. $('#graph').find("rect[fill='#0b62a4']").size().should.equal 2
  60. it 'should have a bar with no stroke', ->
  61. chart = Morris.Bar $.extend {}, defaults
  62. $('#graph').find("rect[stroke='none']").size().should.equal 4
  63. it 'should have text with configured fill color', ->
  64. chart = Morris.Bar $.extend {}, defaults
  65. $('#graph').find("text[fill='#888888']").size().should.equal 7
  66. it 'should have text with configured font size', ->
  67. chart = Morris.Bar $.extend {}, defaults
  68. $('#graph').find("text[font-size='12px']").size().should.equal 7
  69. describe 'when setting bar radius', ->
  70. describe 'svg structure', ->
  71. defaults =
  72. element: 'graph'
  73. data: [{x: 'foo', y: 2, z: 3}, {x: 'bar', y: 4, z: 6}]
  74. xkey: 'x'
  75. ykeys: ['y', 'z']
  76. labels: ['Y', 'Z']
  77. barRadius: [5, 5, 0, 0]
  78. it 'should contain a path for each bar', ->
  79. chart = Morris.Bar $.extend {}, defaults
  80. $('#graph').find("path").size().should.equal 9
  81. it 'should use rects if radius is too big', ->
  82. delete defaults.barStyle
  83. chart = Morris.Bar $.extend {}, defaults,
  84. barRadius: [300, 300, 0, 0]
  85. $('#graph').find("rect").size().should.equal 4
  86. describe 'barSize option', ->
  87. describe 'svg attributes', ->
  88. defaults =
  89. element: 'graph'
  90. barSize: 20
  91. data: [
  92. {x: '2011 Q1', y: 3, z: 2, a: 3}
  93. {x: '2011 Q2', y: 2, z: null, a: 1}
  94. {x: '2011 Q3', y: 0, z: 2, a: 4}
  95. {x: '2011 Q4', y: 2, z: 4, a: 3}
  96. ],
  97. xkey: 'x'
  98. ykeys: ['y', 'z', 'a']
  99. labels: ['Y', 'Z', 'A']
  100. it 'should calc the width if too narrow for barSize', ->
  101. $('#graph').width('200px')
  102. chart = Morris.Bar $.extend {}, defaults
  103. $('#graph').find("rect").filter((i) ->
  104. parseFloat($(@).attr('width'), 10) < 10
  105. ).size().should.equal 11
  106. it 'should set width to @options.barSize if possible', ->
  107. chart = Morris.Bar $.extend {}, defaults
  108. $('#graph').find("rect[width='#{defaults.barSize}']").size().should.equal 11