shape.line-spec.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { parseSvgPath } from './svg-helper';
  2. describe('c3 chart shape line', function () {
  3. 'use strict';
  4. var chart, args;
  5. beforeEach(function (done) {
  6. chart = window.initChart(chart, args, done);
  7. });
  8. describe('shape-rendering for line chart', function () {
  9. beforeAll(function () {
  10. args = {
  11. data: {
  12. columns: [
  13. ['data1', 30, 200, 100, 400, -150, 250],
  14. ['data2', 50, 20, 10, 40, 15, 25],
  15. ['data3', -150, 120, 110, 140, 115, 125]
  16. ],
  17. type: 'line'
  18. }
  19. };
  20. });
  21. it("Should render the lines correctly", function(done) {
  22. setTimeout(function () {
  23. var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
  24. var commands = parseSvgPath( target.select('.c3-line-data1').attr('d'));
  25. expect(commands.length).toBe(6);
  26. done();
  27. }, 500);
  28. });
  29. it("should not have shape-rendering when it's line chart", function () {
  30. d3.selectAll('.c3-line').each(function () {
  31. var style = d3.select(this).style('shape-rendering');
  32. expect(style).toBe('auto');
  33. });
  34. });
  35. describe('should change to step chart', function () {
  36. beforeAll(function(){
  37. args.data.type = 'step';
  38. });
  39. it("should have shape-rendering = crispedges when it's step chart", function () {
  40. d3.selectAll('.c3-line').each(function () {
  41. var style = d3.select(this).style('shape-rendering').toLowerCase();
  42. expect(style).toBe('crispedges');
  43. });
  44. });
  45. });
  46. describe('should change to spline chart', function () {
  47. beforeAll(function(){
  48. args.data.type = 'spline';
  49. });
  50. it('should use cardinal interpolation by default', function () {
  51. expect(chart.internal.config.spline_interpolation_type).toBe('cardinal');
  52. });
  53. });
  54. });
  55. describe('point.show option', function () {
  56. describe('should change args to include null data', function () {
  57. beforeAll(function(){
  58. args = {
  59. data: {
  60. columns: [
  61. ['data1', 30, null, 100, 400, -150, 250],
  62. ['data2', 50, 20, 10, 40, 15, 25],
  63. ['data3', -150, 120, 110, 140, 115, 125]
  64. ],
  65. type: 'line'
  66. }
  67. };
  68. });
  69. it('should not show the circle for null', function (done) {
  70. setTimeout(function () {
  71. var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
  72. expect(+target.select('.c3-circle-0').style('opacity')).toBe(1);
  73. expect(+target.select('.c3-circle-1').style('opacity')).toBe(0);
  74. expect(+target.select('.c3-circle-2').style('opacity')).toBe(1);
  75. done();
  76. }, 500);
  77. });
  78. it('should not draw a line segment for null data', function(done) {
  79. setTimeout(function () {
  80. var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
  81. var commands = parseSvgPath( target.select('.c3-line-data1').attr('d'));
  82. var segments = 0;
  83. for(var i = 0; i < commands.length; i++) {
  84. (commands[i].command === 'L') ? segments++ : null;
  85. }
  86. expect(segments).toBe(3);
  87. done();
  88. }, 500);
  89. });
  90. // it('should change args to include null data on scatter plot', function () {
  91. // args = {
  92. // data: {
  93. // columns: [
  94. // ['data1', 30, null, 100, 400, -150, 250],
  95. // ['data2', 50, 20, 10, 40, 15, 25],
  96. // ['data3', -150, 120, 110, 140, 115, 125]
  97. // ],
  98. // type: 'scatter'
  99. // }
  100. // };
  101. // expect(true).toBeTruthy();
  102. // });
  103. // it('should not show the circle for null', function (done) {
  104. // setTimeout(function () {
  105. // var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
  106. // expect(+target.select('.c3-circle-0').style('opacity')).toBe(0.5);
  107. // expect(+target.select('.c3-circle-1').style('opacity')).toBe(0);
  108. // expect(+target.select('.c3-circle-2').style('opacity')).toBe(0.5);
  109. // done();
  110. // }, 500);
  111. // });
  112. });
  113. describe('should allow passing a function', function() {
  114. beforeAll(function(){
  115. args = {
  116. data: {
  117. columns: [
  118. ['data1', 30, 50, 100]
  119. ],
  120. type: 'line'
  121. },
  122. point: {
  123. show: function(d) {
  124. return d.value > 50;
  125. }
  126. }
  127. };
  128. });
  129. it('should show point if function returns true', function() {
  130. var target = chart.internal.main.select('.c3-chart-line.c3-target-data1');
  131. expect(+target.select('.c3-circle-0').style('opacity')).toBe(0);
  132. expect(+target.select('.c3-circle-1').style('opacity')).toBe(0);
  133. expect(+target.select('.c3-circle-2').style('opacity')).toBe(1);
  134. });
  135. });
  136. });
  137. describe('spline.interpolation option', function () {
  138. beforeAll(function () {
  139. args = {
  140. data: {
  141. columns: [
  142. ['data1', 30, 200, 100, 400, -150, 250],
  143. ['data2', 50, 20, 10, 40, 15, 25],
  144. ['data3', -150, 120, 110, 140, 115, 125]
  145. ],
  146. type: 'spline'
  147. },
  148. spline: {
  149. interpolation: {
  150. type: 'monotone'
  151. }
  152. }
  153. };
  154. });
  155. it('updates interpolation function', function() {
  156. expect(chart.internal.getInterpolate(chart.data()[0])).toBe(d3.curveMonotoneX);
  157. });
  158. describe('should not use a non-valid interpolation', function () {
  159. beforeAll(function(){
  160. args.spline.interpolation.type = 'foo';
  161. });
  162. it('should use cardinal interpolation when given option is not valid', function() {
  163. expect(chart.internal.getInterpolate(chart.data()[0])).toBe(d3.curveCardinal);
  164. });
  165. });
  166. });
  167. });