api.grid-spec.js 3.9 KB

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