api.grid-spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { d3, initChart } from './c3-helper'
  2. describe('c3 api grid', function() {
  3. 'use strict'
  4. var chart, args
  5. beforeEach(function(done) {
  6. chart = initChart(chart, args, done)
  7. })
  8. describe('ygrid.add and ygrid.remove', function() {
  9. beforeAll(function() {
  10. args = {
  11. data: {
  12. columns: [['data1', 30, 200, 100, 400, 150, 250]]
  13. }
  14. }
  15. })
  16. it('updates y grids', function(done) {
  17. var main = chart.internal.main,
  18. expectedGrids = [
  19. {
  20. value: 100,
  21. text: 'Pressure Low'
  22. },
  23. {
  24. value: 200,
  25. text: 'Pressure High'
  26. }
  27. ],
  28. grids
  29. // Call ygrids.add
  30. chart.ygrids.add(expectedGrids)
  31. setTimeout(function() {
  32. grids = main.selectAll('.c3-ygrid-line')
  33. expect(grids.size()).toBe(expectedGrids.length)
  34. grids.each(function(d, i) {
  35. var y = +d3
  36. .select(this)
  37. .select('line')
  38. .attr('y1'),
  39. text = d3
  40. .select(this)
  41. .select('text')
  42. .text(),
  43. expectedY = Math.round(chart.internal.y(expectedGrids[i].value)),
  44. expectedText = expectedGrids[i].text
  45. expect(y).toBe(expectedY)
  46. expect(text).toBe(expectedText)
  47. })
  48. // Call ygrids.remove
  49. chart.ygrids.remove(expectedGrids)
  50. setTimeout(function() {
  51. grids = main.selectAll('.c3-ygrid-line')
  52. expect(grids.size()).toBe(0)
  53. }, 500)
  54. }, 500)
  55. setTimeout(function() {
  56. done()
  57. }, 1200)
  58. })
  59. it('updates x ygrids even if zoomed', function(done) {
  60. var main = chart.internal.main,
  61. expectedGrids = [
  62. {
  63. value: 0,
  64. text: 'Pressure Low'
  65. },
  66. {
  67. value: 1,
  68. text: 'Pressure High'
  69. }
  70. ],
  71. grids,
  72. domain
  73. chart.zoom([0, 2])
  74. setTimeout(function() {
  75. // Call xgrids
  76. chart.xgrids(expectedGrids)
  77. setTimeout(function() {
  78. grids = main.selectAll('.c3-xgrid-line')
  79. expect(grids.size()).toBe(expectedGrids.length)
  80. grids.each(function(d, i) {
  81. var x = +d3
  82. .select(this)
  83. .select('line')
  84. .attr('x1'),
  85. text = d3
  86. .select(this)
  87. .select('text')
  88. .text(),
  89. expectedX = Math.round(chart.internal.x(expectedGrids[i].value)),
  90. expectedText = expectedGrids[i].text
  91. expect(x).toBe(expectedX)
  92. expect(text).toBe(expectedText)
  93. })
  94. // check if it was not rescaled
  95. domain = chart.internal.y.domain()
  96. expect(domain[0]).toBeLessThan(0)
  97. expect(domain[1]).toBeGreaterThan(400)
  98. // Call xgrids.remove
  99. chart.xgrids.remove(expectedGrids)
  100. setTimeout(function() {
  101. grids = main.selectAll('.c3-xgrid-line')
  102. expect(grids.size()).toBe(0)
  103. }, 500) // for xgrids.remove()
  104. }, 500) // for xgrids()
  105. }, 500) // for zoom
  106. setTimeout(function() {
  107. done()
  108. }, 1700)
  109. })
  110. })
  111. })