legend-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. import { d3, initChart } from './c3-helper'
  2. describe('c3 chart legend', function() {
  3. 'use strict'
  4. var chart, args
  5. beforeEach(function(done) {
  6. chart = initChart(chart, args, done)
  7. })
  8. describe('legend when multiple charts rendered', function() {
  9. beforeAll(function() {
  10. args = {
  11. data: {
  12. columns: [
  13. ['data1', 30],
  14. ['data2', 50],
  15. ['data3', 100]
  16. ]
  17. }
  18. }
  19. })
  20. describe('long data names', function() {
  21. beforeAll(function() {
  22. args = {
  23. data: {
  24. columns: [
  25. ['long data name 1', 30],
  26. ['long data name 2', 50],
  27. ['long data name 3', 50]
  28. ]
  29. }
  30. }
  31. })
  32. it('should have properly computed legend width', function() {
  33. var expectedLeft = [148, 226, 384],
  34. expectedWidth = [118, 118, 108]
  35. d3.selectAll('.c3-legend-item').each(function(d, i) {
  36. var rect = (d3.select(this).node() as any).getBoundingClientRect()
  37. expect(rect.left).toBeCloseTo(expectedLeft[i], -2)
  38. expect(rect.width).toBeCloseTo(expectedWidth[i], -2)
  39. })
  40. })
  41. })
  42. })
  43. describe('legend position', function() {
  44. beforeAll(function() {
  45. args = {
  46. data: {
  47. columns: [
  48. ['data1', 30, 200, 100, 400, 150, 250],
  49. ['data2', 50, 20, 10, 40, 15, 25]
  50. ]
  51. }
  52. }
  53. })
  54. it('should be located on the center of chart', function() {
  55. var box = chart.internal.legend.node().getBoundingClientRect()
  56. expect(box.left + box.right).toBe(638)
  57. })
  58. })
  59. describe('legend as inset', function() {
  60. describe('should change the legend to "inset" successfully', function() {
  61. beforeAll(function() {
  62. args = {
  63. data: {
  64. columns: [
  65. ['data1', 30, 200, 100, 400, 150, 250],
  66. ['data2', 50, 20, 10, 40, 15, 25]
  67. ]
  68. },
  69. legend: {
  70. position: 'inset',
  71. inset: {
  72. step: null
  73. }
  74. }
  75. }
  76. })
  77. it('should be positioned properly', function() {
  78. var box = (d3
  79. .select('.c3-legend-background')
  80. .node() as any).getBoundingClientRect()
  81. expect(box.top).toBe(5.5)
  82. expect(box.left).toBeGreaterThan(30)
  83. })
  84. it('should have automatically calculated height', function() {
  85. var box = (d3
  86. .select('.c3-legend-background')
  87. .node() as any).getBoundingClientRect()
  88. expect(box.height).toBe(48)
  89. })
  90. })
  91. describe('should change the legend step to 1 successfully', function() {
  92. beforeAll(function() {
  93. args.legend.inset.step = 1
  94. })
  95. it('should have automatically calculated height', function() {
  96. var box = (d3
  97. .select('.c3-legend-background')
  98. .node() as any).getBoundingClientRect()
  99. expect(box.height).toBe(28)
  100. })
  101. })
  102. describe('should change the legend step to 2 successfully', function() {
  103. beforeAll(function() {
  104. args.legend.inset.step = 2
  105. })
  106. it('should have automatically calculated height', function() {
  107. var box = (d3
  108. .select('.c3-legend-background')
  109. .node() as any).getBoundingClientRect()
  110. expect(box.height).toBe(48)
  111. })
  112. })
  113. describe('with only one series', function() {
  114. beforeAll(function() {
  115. args = {
  116. data: {
  117. columns: [['data1', 30, 200, 100, 400, 150, 250]]
  118. },
  119. legend: {
  120. position: 'inset'
  121. }
  122. }
  123. })
  124. it('should locate legend properly', function() {
  125. var box = (d3
  126. .select('.c3-legend-background')
  127. .node() as any).getBoundingClientRect()
  128. expect(box.height).toBe(28)
  129. expect(box.width).toBeGreaterThan(64)
  130. })
  131. })
  132. })
  133. describe('legend.hide', function() {
  134. beforeAll(function() {
  135. args = {
  136. data: {
  137. columns: [
  138. ['data1', 30, 200, 100, 400, 150, 250],
  139. ['data2', 130, 100, 200, 100, 250, 150]
  140. ]
  141. },
  142. legend: {
  143. hide: true
  144. }
  145. }
  146. })
  147. it('should not show legends', function() {
  148. d3.selectAll('.c3-legend-item').each(function() {
  149. expect(d3.select(this).style('visibility')).toBe('hidden')
  150. })
  151. })
  152. describe('hidden legend', function() {
  153. beforeAll(function() {
  154. args = {
  155. data: {
  156. columns: [
  157. ['data1', 30, 200, 100, 400, 150, 250],
  158. ['data2', 130, 100, 200, 100, 250, 150]
  159. ]
  160. },
  161. legend: {
  162. hide: 'data2'
  163. }
  164. }
  165. })
  166. it('should not show legends', function() {
  167. expect(d3.select('.c3-legend-item-data1').style('visibility')).toBe(
  168. 'visible'
  169. )
  170. expect(d3.select('.c3-legend-item-data2').style('visibility')).toBe(
  171. 'hidden'
  172. )
  173. })
  174. })
  175. })
  176. describe('legend.show', function() {
  177. beforeAll(function() {
  178. args = {
  179. data: {
  180. columns: [
  181. ['data1', 30, 200, 100, 400, 150, 250],
  182. ['data2', 130, 100, 200, 100, 250, 150]
  183. ]
  184. },
  185. legend: {
  186. show: false
  187. }
  188. }
  189. })
  190. it('should not initially have rendered any legend items', function() {
  191. expect(d3.selectAll('.c3-legend-item').empty()).toBe(true)
  192. })
  193. it('allows us to show the legend on showLegend call', function() {
  194. chart.legend.show()
  195. d3.selectAll('.c3-legend-item').each(function() {
  196. expect(d3.select(this).style('visibility')).toBe('visible')
  197. // This selects all the children, but we expect it to be empty
  198. expect((d3.select(this).selectAll('*') as any).length).not.toEqual(0)
  199. })
  200. })
  201. })
  202. describe('with legend.show is true', function() {
  203. beforeAll(function() {
  204. args = {
  205. data: {
  206. columns: [
  207. ['data1', 30, 200, 100, 400, 150, 250],
  208. ['data2', 130, 100, 200, 100, 250, 150]
  209. ]
  210. },
  211. legend: {
  212. show: true
  213. }
  214. }
  215. })
  216. it('should initially have rendered some legend items', function() {
  217. expect(d3.selectAll('.c3-legend-item').empty()).toBe(false)
  218. })
  219. it('should remove rendered every legend items', function() {
  220. chart.legend.hide()
  221. d3.selectAll('.c3-legend-item').each(function() {
  222. expect(d3.select(this).style('visibility')).toBe('hidden')
  223. // This selects all the children, but we expect it to be empty
  224. expect((d3.select(this).selectAll('*') as any).length).toEqual(
  225. undefined
  226. )
  227. })
  228. })
  229. })
  230. describe('custom legend size', function() {
  231. beforeAll(function() {
  232. args = {
  233. data: {
  234. columns: [
  235. ['data1', 30, 200, 100, 400, 150, 250],
  236. ['data2', 130, 100, 200, 100, 250, 150]
  237. ]
  238. },
  239. legend: {
  240. item: {
  241. tile: {
  242. width: 15,
  243. height: 2
  244. }
  245. }
  246. }
  247. }
  248. })
  249. it('renders the legend item with the correct width and height', function() {
  250. d3.selectAll('.c3-legend-item-tile').each(function() {
  251. expect(d3.select(this).style('stroke-width')).toBe(
  252. args.legend.item.tile.height + 'px'
  253. )
  254. var tileWidth =
  255. Number(d3.select(this).attr('x2')) -
  256. Number(d3.select(this).attr('x1'))
  257. expect(tileWidth).toBe(args.legend.item.tile.width)
  258. })
  259. })
  260. })
  261. describe('custom legend padding', function() {
  262. beforeAll(function() {
  263. args = {
  264. data: {
  265. columns: [
  266. ['padded1', 30, 200, 100, 400, 150, 250],
  267. ['padded2', 130, 100, 200, 100, 250, 150]
  268. ]
  269. },
  270. legend: {
  271. padding: 10
  272. }
  273. }
  274. })
  275. it('renders the correct amount of padding on the legend element', function() {
  276. d3.selectAll(
  277. '.c3-legend-item-padded1 .c3-legend-item-tile, .c3-legend-item-padded2 .c3-legend-item-tile'
  278. ).each(function(el, index) {
  279. var itemWidth = (d3.select(this).node() as any).parentNode.getBBox()
  280. .width,
  281. textBoxWidth = (d3
  282. .select((d3.select(this).node() as any).parentNode)
  283. .select('text')
  284. .node() as any).getBBox().width,
  285. tileWidth = 17, // default value is 10, plus 7 more for padding @TODO verify this, seems PhantomJS@^2 adds another 1px to each side
  286. expectedWidth =
  287. textBoxWidth + tileWidth + (index ? 0 : 10) + args.legend.padding
  288. expect(itemWidth).toBe(expectedWidth)
  289. })
  290. })
  291. })
  292. describe('legend item tile coloring with color_treshold', function() {
  293. beforeAll(function() {
  294. args = {
  295. data: {
  296. columns: [
  297. ['padded1', 100],
  298. ['padded2', 90],
  299. ['padded3', 50],
  300. ['padded4', 20]
  301. ]
  302. },
  303. type: 'gauge',
  304. color: {
  305. pattern: ['#FF0000', '#F97600', '#F6C600', '#60B044'],
  306. threshold: {
  307. values: [30, 80, 95]
  308. }
  309. }
  310. }
  311. })
  312. // espacially for gauges with multiple arcs to have the same coloring between legend tiles, tooltip tiles and arc
  313. it('selects the color from color_pattern if color_treshold is given', function() {
  314. var tileColor = []
  315. d3.selectAll('.c3-legend-item-tile').each(function() {
  316. tileColor.push(d3.select(this).style('stroke'))
  317. })
  318. expect(tileColor[0]).toBe('rgb(96, 176, 68)')
  319. expect(tileColor[1]).toBe('rgb(246, 198, 0)')
  320. expect(tileColor[2]).toBe('rgb(249, 118, 0)')
  321. expect(tileColor[3]).toBe('rgb(255, 0, 0)')
  322. })
  323. })
  324. describe('legend item tile coloring with color_treshold (more than one data value)', function() {
  325. beforeAll(function() {
  326. args = {
  327. data: {
  328. columns: [
  329. ['padded1', 40, 60],
  330. ['padded2', 100, -10],
  331. ['padded3', 0, 50],
  332. ['padded4', 20, 0]
  333. ]
  334. },
  335. type: 'gauge',
  336. color: {
  337. pattern: ['#FF0000', '#F97600', '#F6C600', '#60B044'],
  338. threshold: {
  339. values: [30, 80, 95]
  340. }
  341. }
  342. }
  343. })
  344. // espacially for gauges with multiple arcs to have the same coloring between legend tiles, tooltip tiles and arc
  345. it('selects the color from color_pattern if color_treshold is given', function() {
  346. var tileColor = []
  347. d3.selectAll('.c3-legend-item-tile').each(function() {
  348. tileColor.push(d3.select(this).style('stroke'))
  349. })
  350. expect(tileColor[0]).toBe('rgb(96, 176, 68)')
  351. expect(tileColor[1]).toBe('rgb(246, 198, 0)')
  352. expect(tileColor[2]).toBe('rgb(249, 118, 0)')
  353. expect(tileColor[3]).toBe('rgb(255, 0, 0)')
  354. })
  355. })
  356. describe('legend item tile coloring without color_treshold', function() {
  357. beforeAll(function() {
  358. args = {
  359. data: {
  360. columns: [
  361. ['padded1', 100],
  362. ['padded2', 90],
  363. ['padded3', 50],
  364. ['padded4', 20]
  365. ],
  366. colors: {
  367. padded1: '#60b044',
  368. padded4: '#8b008b'
  369. }
  370. },
  371. type: 'gauge'
  372. }
  373. })
  374. it('selects the color from data_colors, data_color or default', function() {
  375. var tileColor = []
  376. d3.selectAll('.c3-legend-item-tile').each(function() {
  377. tileColor.push(d3.select(this).style('stroke'))
  378. })
  379. expect(tileColor[0]).toBe('rgb(96, 176, 68)')
  380. expect(tileColor[1]).toBe('rgb(31, 119, 180)')
  381. expect(tileColor[2]).toBe('rgb(255, 127, 14)')
  382. expect(tileColor[3]).toBe('rgb(139, 0, 139)')
  383. })
  384. })
  385. })