plugin.legend.tests.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // Test the rectangle element
  2. describe('Legend block tests', function() {
  3. it('should have the correct default config', function() {
  4. expect(Chart.defaults.global.legend).toEqual({
  5. display: true,
  6. position: 'top',
  7. fullWidth: true, // marks that this box should take the full width of the canvas (pushing down other boxes)
  8. reverse: false,
  9. weight: 1000,
  10. // a callback that will handle
  11. onClick: jasmine.any(Function),
  12. onHover: null,
  13. labels: {
  14. boxWidth: 40,
  15. padding: 10,
  16. generateLabels: jasmine.any(Function)
  17. }
  18. });
  19. });
  20. it('should update correctly', function() {
  21. var chart = window.acquireChart({
  22. type: 'bar',
  23. data: {
  24. datasets: [{
  25. label: 'dataset1',
  26. backgroundColor: '#f31',
  27. borderCapStyle: 'butt',
  28. borderDash: [2, 2],
  29. borderDashOffset: 5.5,
  30. data: []
  31. }, {
  32. label: 'dataset2',
  33. hidden: true,
  34. borderJoinStyle: 'miter',
  35. data: []
  36. }, {
  37. label: 'dataset3',
  38. borderWidth: 10,
  39. borderColor: 'green',
  40. pointStyle: 'crossRot',
  41. data: []
  42. }],
  43. labels: []
  44. }
  45. });
  46. expect(chart.legend.legendItems).toEqual([{
  47. text: 'dataset1',
  48. fillStyle: '#f31',
  49. hidden: false,
  50. lineCap: 'butt',
  51. lineDash: [2, 2],
  52. lineDashOffset: 5.5,
  53. lineJoin: undefined,
  54. lineWidth: undefined,
  55. strokeStyle: undefined,
  56. pointStyle: undefined,
  57. datasetIndex: 0
  58. }, {
  59. text: 'dataset2',
  60. fillStyle: undefined,
  61. hidden: true,
  62. lineCap: undefined,
  63. lineDash: undefined,
  64. lineDashOffset: undefined,
  65. lineJoin: 'miter',
  66. lineWidth: undefined,
  67. strokeStyle: undefined,
  68. pointStyle: undefined,
  69. datasetIndex: 1
  70. }, {
  71. text: 'dataset3',
  72. fillStyle: undefined,
  73. hidden: false,
  74. lineCap: undefined,
  75. lineDash: undefined,
  76. lineDashOffset: undefined,
  77. lineJoin: undefined,
  78. lineWidth: 10,
  79. strokeStyle: 'green',
  80. pointStyle: 'crossRot',
  81. datasetIndex: 2
  82. }]);
  83. });
  84. it('should filter items', function() {
  85. var chart = window.acquireChart({
  86. type: 'bar',
  87. data: {
  88. datasets: [{
  89. label: 'dataset1',
  90. backgroundColor: '#f31',
  91. borderCapStyle: 'butt',
  92. borderDash: [2, 2],
  93. borderDashOffset: 5.5,
  94. data: []
  95. }, {
  96. label: 'dataset2',
  97. hidden: true,
  98. borderJoinStyle: 'miter',
  99. data: [],
  100. legendHidden: true
  101. }, {
  102. label: 'dataset3',
  103. borderWidth: 10,
  104. borderColor: 'green',
  105. pointStyle: 'crossRot',
  106. data: []
  107. }],
  108. labels: []
  109. },
  110. options: {
  111. legend: {
  112. labels: {
  113. filter: function(legendItem, data) {
  114. var dataset = data.datasets[legendItem.datasetIndex];
  115. return !dataset.legendHidden;
  116. }
  117. }
  118. }
  119. }
  120. });
  121. expect(chart.legend.legendItems).toEqual([{
  122. text: 'dataset1',
  123. fillStyle: '#f31',
  124. hidden: false,
  125. lineCap: 'butt',
  126. lineDash: [2, 2],
  127. lineDashOffset: 5.5,
  128. lineJoin: undefined,
  129. lineWidth: undefined,
  130. strokeStyle: undefined,
  131. pointStyle: undefined,
  132. datasetIndex: 0
  133. }, {
  134. text: 'dataset3',
  135. fillStyle: undefined,
  136. hidden: false,
  137. lineCap: undefined,
  138. lineDash: undefined,
  139. lineDashOffset: undefined,
  140. lineJoin: undefined,
  141. lineWidth: 10,
  142. strokeStyle: 'green',
  143. pointStyle: 'crossRot',
  144. datasetIndex: 2
  145. }]);
  146. });
  147. it('should not throw when the label options are missing', function() {
  148. var makeChart = function() {
  149. window.acquireChart({
  150. type: 'bar',
  151. data: {
  152. datasets: [{
  153. label: 'dataset1',
  154. backgroundColor: '#f31',
  155. borderCapStyle: 'butt',
  156. borderDash: [2, 2],
  157. borderDashOffset: 5.5,
  158. data: []
  159. }],
  160. labels: []
  161. },
  162. options: {
  163. legend: {
  164. labels: false,
  165. }
  166. }
  167. });
  168. };
  169. expect(makeChart).not.toThrow();
  170. });
  171. it('should draw correctly', function() {
  172. var chart = window.acquireChart({
  173. type: 'bar',
  174. data: {
  175. datasets: [{
  176. label: 'dataset1',
  177. backgroundColor: '#f31',
  178. borderCapStyle: 'butt',
  179. borderDash: [2, 2],
  180. borderDashOffset: 5.5,
  181. data: []
  182. }, {
  183. label: 'dataset2',
  184. hidden: true,
  185. borderJoinStyle: 'miter',
  186. data: []
  187. }, {
  188. label: 'dataset3',
  189. borderWidth: 10,
  190. borderColor: 'green',
  191. data: []
  192. }],
  193. labels: []
  194. }
  195. });
  196. expect(chart.legend.legendHitBoxes.length).toBe(3);
  197. [
  198. {h: 12, l: 101, t: 10, w: 93},
  199. {h: 12, l: 205, t: 10, w: 93},
  200. {h: 12, l: 308, t: 10, w: 93}
  201. ].forEach(function(expected, i) {
  202. expect(chart.legend.legendHitBoxes[i].height).toBeCloseToPixel(expected.h);
  203. expect(chart.legend.legendHitBoxes[i].left).toBeCloseToPixel(expected.l);
  204. expect(chart.legend.legendHitBoxes[i].top).toBeCloseToPixel(expected.t);
  205. expect(chart.legend.legendHitBoxes[i].width).toBeCloseToPixel(expected.w);
  206. });
  207. // NOTE(SB) We should get ride of the following tests and use image diff instead.
  208. // For now, as discussed with Evert Timberg, simply comment out.
  209. // See http://humblesoftware.github.io/js-imagediff/test.html
  210. /* chart.legend.ctx = window.createMockContext();
  211. chart.update();
  212. expect(chart.legend.ctx .getCalls()).toEqual([{
  213. "name": "measureText",
  214. "args": ["dataset1"]
  215. }, {
  216. "name": "measureText",
  217. "args": ["dataset2"]
  218. }, {
  219. "name": "measureText",
  220. "args": ["dataset3"]
  221. }, {
  222. "name": "measureText",
  223. "args": ["dataset1"]
  224. }, {
  225. "name": "measureText",
  226. "args": ["dataset2"]
  227. }, {
  228. "name": "measureText",
  229. "args": ["dataset3"]
  230. }, {
  231. "name": "setLineWidth",
  232. "args": [0.5]
  233. }, {
  234. "name": "setStrokeStyle",
  235. "args": ["#666"]
  236. }, {
  237. "name": "setFillStyle",
  238. "args": ["#666"]
  239. }, {
  240. "name": "measureText",
  241. "args": ["dataset1"]
  242. }, {
  243. "name": "save",
  244. "args": []
  245. }, {
  246. "name": "setFillStyle",
  247. "args": ["#f31"]
  248. }, {
  249. "name": "setLineCap",
  250. "args": ["butt"]
  251. }, {
  252. "name": "setLineDashOffset",
  253. "args": [5.5]
  254. }, {
  255. "name": "setLineJoin",
  256. "args": ["miter"]
  257. }, {
  258. "name": "setLineWidth",
  259. "args": [3]
  260. }, {
  261. "name": "setStrokeStyle",
  262. "args": ["rgba(0,0,0,0.1)"]
  263. }, {
  264. "name": "setLineDash",
  265. "args": [
  266. [2, 2]
  267. ]
  268. }, {
  269. "name": "strokeRect",
  270. "args": [114, 110, 40, 12]
  271. }, {
  272. "name": "fillRect",
  273. "args": [114, 110, 40, 12]
  274. }, {
  275. "name": "restore",
  276. "args": []
  277. }, {
  278. "name": "fillText",
  279. "args": ["dataset1", 160, 110]
  280. }, {
  281. "name": "measureText",
  282. "args": ["dataset2"]
  283. }, {
  284. "name": "save",
  285. "args": []
  286. }, {
  287. "name": "setFillStyle",
  288. "args": ["rgba(0,0,0,0.1)"]
  289. }, {
  290. "name": "setLineCap",
  291. "args": ["butt"]
  292. }, {
  293. "name": "setLineDashOffset",
  294. "args": [0]
  295. }, {
  296. "name": "setLineJoin",
  297. "args": ["miter"]
  298. }, {
  299. "name": "setLineWidth",
  300. "args": [3]
  301. }, {
  302. "name": "setStrokeStyle",
  303. "args": ["rgba(0,0,0,0.1)"]
  304. }, {
  305. "name": "setLineDash",
  306. "args": [
  307. []
  308. ]
  309. }, {
  310. "name": "strokeRect",
  311. "args": [250, 110, 40, 12]
  312. }, {
  313. "name": "fillRect",
  314. "args": [250, 110, 40, 12]
  315. }, {
  316. "name": "restore",
  317. "args": []
  318. }, {
  319. "name": "fillText",
  320. "args": ["dataset2", 296, 110]
  321. }, {
  322. "name": "beginPath",
  323. "args": []
  324. }, {
  325. "name": "setLineWidth",
  326. "args": [2]
  327. }, {
  328. "name": "moveTo",
  329. "args": [296, 116]
  330. }, {
  331. "name": "lineTo",
  332. "args": [376, 116]
  333. }, {
  334. "name": "stroke",
  335. "args": []
  336. }, {
  337. "name": "measureText",
  338. "args": ["dataset3"]
  339. }, {
  340. "name": "save",
  341. "args": []
  342. }, {
  343. "name": "setFillStyle",
  344. "args": ["rgba(0,0,0,0.1)"]
  345. }, {
  346. "name": "setLineCap",
  347. "args": ["butt"]
  348. }, {
  349. "name": "setLineDashOffset",
  350. "args": [0]
  351. }, {
  352. "name": "setLineJoin",
  353. "args": ["miter"]
  354. }, {
  355. "name": "setLineWidth",
  356. "args": [10]
  357. }, {
  358. "name": "setStrokeStyle",
  359. "args": ["green"]
  360. }, {
  361. "name": "setLineDash",
  362. "args": [
  363. []
  364. ]
  365. }, {
  366. "name": "strokeRect",
  367. "args": [182, 132, 40, 12]
  368. }, {
  369. "name": "fillRect",
  370. "args": [182, 132, 40, 12]
  371. }, {
  372. "name": "restore",
  373. "args": []
  374. }, {
  375. "name": "fillText",
  376. "args": ["dataset3", 228, 132]
  377. }]);*/
  378. });
  379. describe('config update', function() {
  380. it ('should update the options', function() {
  381. var chart = acquireChart({
  382. type: 'line',
  383. data: {
  384. labels: ['A', 'B', 'C', 'D'],
  385. datasets: [{
  386. data: [10, 20, 30, 100]
  387. }]
  388. },
  389. options: {
  390. legend: {
  391. display: true
  392. }
  393. }
  394. });
  395. expect(chart.legend.options.display).toBe(true);
  396. chart.options.legend.display = false;
  397. chart.update();
  398. expect(chart.legend.options.display).toBe(false);
  399. });
  400. it ('should update the associated layout item', function() {
  401. var chart = acquireChart({
  402. type: 'line',
  403. data: {},
  404. options: {
  405. legend: {
  406. fullWidth: true,
  407. position: 'top',
  408. weight: 150
  409. }
  410. }
  411. });
  412. expect(chart.legend.fullWidth).toBe(true);
  413. expect(chart.legend.position).toBe('top');
  414. expect(chart.legend.weight).toBe(150);
  415. chart.options.legend.fullWidth = false;
  416. chart.options.legend.position = 'left';
  417. chart.options.legend.weight = 42;
  418. chart.update();
  419. expect(chart.legend.fullWidth).toBe(false);
  420. expect(chart.legend.position).toBe('left');
  421. expect(chart.legend.weight).toBe(42);
  422. });
  423. it ('should remove the legend if the new options are false', function() {
  424. var chart = acquireChart({
  425. type: 'line',
  426. data: {
  427. labels: ['A', 'B', 'C', 'D'],
  428. datasets: [{
  429. data: [10, 20, 30, 100]
  430. }]
  431. }
  432. });
  433. expect(chart.legend).not.toBe(undefined);
  434. chart.options.legend = false;
  435. chart.update();
  436. expect(chart.legend).toBe(undefined);
  437. });
  438. it ('should create the legend if the legend options are changed to exist', function() {
  439. var chart = acquireChart({
  440. type: 'line',
  441. data: {
  442. labels: ['A', 'B', 'C', 'D'],
  443. datasets: [{
  444. data: [10, 20, 30, 100]
  445. }]
  446. },
  447. options: {
  448. legend: false
  449. }
  450. });
  451. expect(chart.legend).toBe(undefined);
  452. chart.options.legend = {};
  453. chart.update();
  454. expect(chart.legend).not.toBe(undefined);
  455. expect(chart.legend.options).toEqual(jasmine.objectContaining(Chart.defaults.global.legend));
  456. });
  457. });
  458. });