grid-spec.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. describe('c3 chart grid', function () {
  2. 'use strict';
  3. var chart, args;
  4. beforeEach(function (done) {
  5. chart = window.initChart(chart, args, done);
  6. });
  7. describe('y grid show', function () {
  8. beforeAll(function () {
  9. args = {
  10. data: {
  11. columns: [
  12. ['data1', 30, 200, 100, 400, 150, 250]
  13. ]
  14. },
  15. axis: {
  16. y: {
  17. tick: {
  18. }
  19. }
  20. },
  21. grid: {
  22. y: {
  23. show: false
  24. }
  25. }
  26. };
  27. });
  28. it('should not show y grids', function () {
  29. expect(chart.internal.main.select('.c3-ygrids').size()).toBe(0);
  30. });
  31. describe('with y grids', function () {
  32. beforeAll(function(){
  33. args.grid.y.show = true;
  34. });
  35. it('should show y grids', function () {
  36. var ygrids = chart.internal.main.select('.c3-ygrids');
  37. expect(ygrids.size()).toBe(1);
  38. expect(ygrids.selectAll('.c3-ygrid').size()).toBe(9);
  39. });
  40. });
  41. describe('with only 3 y grids', function () {
  42. beforeAll(function(){
  43. args.grid.y.ticks = 3;
  44. });
  45. it('should show only 3 y grids', function () {
  46. var ygrids = chart.internal.main.select('.c3-ygrids');
  47. expect(ygrids.size()).toBe(1);
  48. expect(ygrids.selectAll('.c3-ygrid').size()).toBe(3);
  49. });
  50. });
  51. describe('with y grids depending on y axis ticks', function () {
  52. beforeAll(function(){
  53. args.axis.y.tick.count = 5;
  54. });
  55. it('should show grids depending on y axis ticks', function () {
  56. var ygrids = chart.internal.main.select('.c3-ygrids'),
  57. expectedYs = [];
  58. ygrids.selectAll('.c3-ygrid').each(function (d, i) {
  59. expectedYs[i] = Math.ceil(+d3.select(this).attr('y1'));
  60. });
  61. expect(ygrids.size()).toBe(1);
  62. expect(ygrids.selectAll('.c3-ygrid').size()).toBe(5);
  63. chart.internal.main.select('.c3-axis-y').selectAll('.tick').each(function (d, i) {
  64. var t = d3.select(this).attr('transform');
  65. expect(t).toBe('translate(0,' + expectedYs[i] + ')');
  66. });
  67. });
  68. });
  69. });
  70. describe('y grid lines', function () {
  71. describe('position', function () {
  72. beforeAll(function () {
  73. args = {
  74. data: {
  75. columns: [
  76. ['data1', 10, 200, 100, 400, 150, 250]
  77. ]
  78. },
  79. grid: {
  80. y: {
  81. lines: [
  82. {value: 30, text: 'Label 30', position: 'start'},
  83. {value: 145, text: 'Label 145', position: 'middle'},
  84. {value: 225, text: 'Label 225'}
  85. ]
  86. }
  87. }
  88. };
  89. });
  90. it('should show 3 grid lines', function () {
  91. expect(chart.internal.main.selectAll('.c3-ygrid-lines .c3-ygrid-line').size()).toBe(3);
  92. });
  93. it('should locate grid lines properly', function () {
  94. var lines = chart.internal.main.selectAll('.c3-ygrid-lines .c3-ygrid-line'),
  95. expectedY1s = [373, 268, 196];
  96. lines.each(function (d, i) {
  97. var y1 = d3.select(this).select('line').attr('y1');
  98. expect(y1).toBeCloseTo(expectedY1s[i], -2);
  99. });
  100. });
  101. it('should locate grid texts properly', function () {
  102. var lines = chart.internal.main.selectAll('.c3-ygrid-lines .c3-ygrid-line'),
  103. expectedPositions = ['start', 'middle', 'end'],
  104. expectedDxs = [4, 0, -4];
  105. lines.each(function (d, i) {
  106. var text = d3.select(this).select('text'),
  107. textAnchor = text.attr('text-anchor'),
  108. dx = text.attr('dx');
  109. expect(textAnchor).toBe(expectedPositions[i]);
  110. expect(+dx).toBe(expectedDxs[i]);
  111. });
  112. });
  113. describe('three gridlines', function () {
  114. beforeAll(function(){
  115. args = {
  116. data: {
  117. columns: [
  118. ['data1', 10, 200, 100, 400, 150, 250]
  119. ]
  120. },
  121. axis: {
  122. rotated: true
  123. },
  124. grid: {
  125. y: {
  126. lines: [
  127. {value: 30, text: 'Label 30', position: 'start'},
  128. {value: 145, text: 'Label 145', position: 'middle'},
  129. {value: 225, text: 'Label 225'}
  130. ]
  131. }
  132. }
  133. };
  134. });
  135. it('should show 3 grid lines', function () {
  136. expect(chart.internal.main.selectAll('.c3-ygrid-lines .c3-ygrid-line').size()).toBe(3);
  137. });
  138. it('should locate grid lines properly', function () {
  139. var lines = chart.internal.main.selectAll('.c3-ygrid-lines .c3-ygrid-line'),
  140. expectedX1s = [75, 220, 321];
  141. lines.each(function (d, i) {
  142. var x1 = d3.select(this).select('line').attr('x1');
  143. expect(x1).toBeCloseTo(expectedX1s[i], -2);
  144. });
  145. });
  146. it('should locate grid texts properly', function () {
  147. var lines = chart.internal.main.selectAll('.c3-ygrid-lines .c3-ygrid-line'),
  148. expectedPositions = ['start', 'middle', 'end'],
  149. expectedDxs = [4, 0, -4];
  150. lines.each(function (d, i) {
  151. var text = d3.select(this).select('text'),
  152. textAnchor = text.attr('text-anchor'),
  153. dx = text.attr('dx');
  154. expect(textAnchor).toBe(expectedPositions[i]);
  155. expect(+dx).toBe(expectedDxs[i]);
  156. });
  157. });
  158. });
  159. });
  160. });
  161. describe('x grid lines', function () {
  162. describe('position', function () {
  163. beforeAll(function () { // 'should have correct height',
  164. args = {
  165. data: {
  166. columns: [
  167. ['data1', 30, 200, 100, 400],
  168. ]
  169. },
  170. grid: {
  171. x: {
  172. lines: [
  173. {value: 1, text: 'Label 1', position: 'start'},
  174. {value: 2, text: 'Label 2', position: 'middle'},
  175. {value: 3, text: 'Label 3'},
  176. ]
  177. }
  178. },
  179. };
  180. });
  181. it('should show 3 grid lines', function () {
  182. expect(chart.internal.main.selectAll('.c3-xgrid-lines .c3-xgrid-line').size()).toBe(3);
  183. });
  184. it('should locate grid lines properly', function () {
  185. var lines = chart.internal.main.selectAll('.c3-xgrid-lines .c3-xgrid-line'),
  186. expectedX1s = [202, 397, 593];
  187. lines.each(function (d, i) {
  188. var x1 = d3.select(this).select('line').attr('x1');
  189. expect(x1).toBeCloseTo(expectedX1s[i], -2);
  190. });
  191. });
  192. it('should locate grid texts properly', function () {
  193. var lines = chart.internal.main.selectAll('.c3-xgrid-lines .c3-xgrid-line'),
  194. expectedPositions = ['start', 'middle', 'end'],
  195. expectedDxs = [4, 0, -4];
  196. lines.each(function (d, i) {
  197. var text = d3.select(this).select('text'),
  198. textAnchor = text.attr('text-anchor'),
  199. dx = text.attr('dx');
  200. expect(textAnchor).toBe(expectedPositions[i]);
  201. expect(+dx).toBe(expectedDxs[i]);
  202. });
  203. });
  204. describe('three grid lines', function () {
  205. beforeAll(function(){
  206. args = {
  207. data: {
  208. columns: [
  209. ['data1', 30, 200, 100, 400],
  210. ]
  211. },
  212. axis: {
  213. rotated: true
  214. },
  215. grid: {
  216. x: {
  217. lines: [
  218. {value: 1, text: 'Label 1', position: 'start'},
  219. {value: 2, text: 'Label 2', position: 'middle'},
  220. {value: 3, text: 'Label 3'},
  221. ]
  222. }
  223. },
  224. };
  225. });
  226. it('should show 3 grid lines', function () {
  227. expect(chart.internal.main.selectAll('.c3-xgrid-lines .c3-xgrid-line').size()).toBe(3);
  228. });
  229. it('should locate grid lines properly', function () {
  230. var lines = chart.internal.main.selectAll('.c3-xgrid-lines .c3-xgrid-line'),
  231. expectedY1s = [144, 283, 421];
  232. lines.each(function (d, i) {
  233. var y1 = d3.select(this).select('line').attr('y1');
  234. expect(y1).toBeCloseTo(expectedY1s[i], -2);
  235. });
  236. });
  237. it('should locate grid texts properly', function () {
  238. var lines = chart.internal.main.selectAll('.c3-xgrid-lines .c3-xgrid-line'),
  239. expectedPositions = ['start', 'middle', 'end'],
  240. expectedDxs = [4, 0, -4];
  241. lines.each(function (d, i) {
  242. var text = d3.select(this).select('text'),
  243. textAnchor = text.attr('text-anchor'),
  244. dx = text.attr('dx');
  245. expect(textAnchor).toBe(expectedPositions[i]);
  246. expect(+dx).toBe(expectedDxs[i]);
  247. });
  248. });
  249. });
  250. });
  251. describe('with padding.top', function () {
  252. describe('should have correct height', function () {
  253. beforeAll(function(){
  254. args = {
  255. data: {
  256. columns: [
  257. ['data1', 30, 200, 100, 400],
  258. ]
  259. },
  260. grid: {
  261. x: {
  262. lines: [
  263. {value: 3, text: 'Label 3'}
  264. ]
  265. }
  266. },
  267. padding: {
  268. top: 50
  269. }
  270. };
  271. });
  272. it('should show x grid lines', function () {
  273. var lines = chart.internal.main.select('.c3-xgrid-lines .c3-xgrid-line'),
  274. expectedX1 = 593,
  275. expectedText = ['Label 3'];
  276. lines.each(function (id, i) {
  277. var line = d3.select(this),
  278. l = line.select('line'),
  279. t = line.select('text');
  280. expect(+l.attr('x1')).toBeCloseTo(expectedX1, -2);
  281. expect(t.text()).toBe(expectedText[i]);
  282. });
  283. });
  284. });
  285. });
  286. describe('on category axis', function () {
  287. beforeAll(function () {
  288. args = {
  289. data: {
  290. x: 'x',
  291. columns: [
  292. ['x', 'a', 'b', 'c', 'd'],
  293. ['data1', 30, 200, 100, 400],
  294. ]
  295. },
  296. axis: {
  297. x: {
  298. type: 'category'
  299. }
  300. },
  301. grid: {
  302. x: {
  303. lines: [
  304. {value: 3, text: 'Label 3'},
  305. {value: 'a', text: 'Label a'}
  306. ]
  307. }
  308. }
  309. };
  310. });
  311. it('should show x grid lines', function () {
  312. var lines = chart.internal.main.selectAll('.c3-xgrid-lines .c3-xgrid-line'),
  313. expectedX1 = [515, 74],
  314. expectedText = ['Label 3', 'Label a'];
  315. lines.each(function (id, i) {
  316. var line = d3.select(this),
  317. l = line.select('line'),
  318. t = line.select('text');
  319. expect(+l.attr('x1')).toBeCloseTo(expectedX1[i], -2);
  320. expect(t.text()).toBe(expectedText[i]);
  321. });
  322. });
  323. });
  324. });
  325. });