legend-spec.js 12 KB

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