grid-spec.ts 12 KB

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