shape.line-spec.ts 7.0 KB

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