core.layouts.tests.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. describe('Chart.layouts', function() {
  2. it('should be exposed through Chart.layouts', function() {
  3. expect(Chart.layouts).toBeDefined();
  4. expect(typeof Chart.layouts).toBe('object');
  5. expect(Chart.layouts.defaults).toBeDefined();
  6. expect(Chart.layouts.addBox).toBeDefined();
  7. expect(Chart.layouts.removeBox).toBeDefined();
  8. expect(Chart.layouts.configure).toBeDefined();
  9. expect(Chart.layouts.update).toBeDefined();
  10. });
  11. it('should fit a simple chart with 2 scales', function() {
  12. var chart = window.acquireChart({
  13. type: 'bar',
  14. data: {
  15. datasets: [
  16. {data: [10, 5, 0, 25, 78, -10]}
  17. ],
  18. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  19. },
  20. options: {
  21. scales: {
  22. xAxes: [{
  23. id: 'xScale',
  24. type: 'category'
  25. }],
  26. yAxes: [{
  27. id: 'yScale',
  28. type: 'linear'
  29. }]
  30. }
  31. }
  32. }, {
  33. canvas: {
  34. height: 150,
  35. width: 250
  36. }
  37. });
  38. expect(chart.chartArea.bottom).toBeCloseToPixel(120);
  39. expect(chart.chartArea.left).toBeCloseToPixel(34);
  40. expect(chart.chartArea.right).toBeCloseToPixel(247);
  41. expect(chart.chartArea.top).toBeCloseToPixel(32);
  42. // Is xScale at the right spot
  43. expect(chart.scales.xScale.bottom).toBeCloseToPixel(150);
  44. expect(chart.scales.xScale.left).toBeCloseToPixel(34);
  45. expect(chart.scales.xScale.right).toBeCloseToPixel(247);
  46. expect(chart.scales.xScale.top).toBeCloseToPixel(120);
  47. expect(chart.scales.xScale.labelRotation).toBeCloseTo(0);
  48. // Is yScale at the right spot
  49. expect(chart.scales.yScale.bottom).toBeCloseToPixel(120);
  50. expect(chart.scales.yScale.left).toBeCloseToPixel(0);
  51. expect(chart.scales.yScale.right).toBeCloseToPixel(34);
  52. expect(chart.scales.yScale.top).toBeCloseToPixel(32);
  53. expect(chart.scales.yScale.labelRotation).toBeCloseTo(0);
  54. });
  55. it('should fit scales that are in the top and right positions', function() {
  56. var chart = window.acquireChart({
  57. type: 'bar',
  58. data: {
  59. datasets: [
  60. {data: [10, 5, 0, 25, 78, -10]}
  61. ],
  62. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  63. },
  64. options: {
  65. scales: {
  66. xAxes: [{
  67. id: 'xScale',
  68. type: 'category',
  69. position: 'top'
  70. }],
  71. yAxes: [{
  72. id: 'yScale',
  73. type: 'linear',
  74. position: 'right'
  75. }]
  76. }
  77. }
  78. }, {
  79. canvas: {
  80. height: 150,
  81. width: 250
  82. }
  83. });
  84. expect(chart.chartArea.bottom).toBeCloseToPixel(142);
  85. expect(chart.chartArea.left).toBeCloseToPixel(3);
  86. expect(chart.chartArea.right).toBeCloseToPixel(216);
  87. expect(chart.chartArea.top).toBeCloseToPixel(62);
  88. // Is xScale at the right spot
  89. expect(chart.scales.xScale.bottom).toBeCloseToPixel(62);
  90. expect(chart.scales.xScale.left).toBeCloseToPixel(3);
  91. expect(chart.scales.xScale.right).toBeCloseToPixel(216);
  92. expect(chart.scales.xScale.top).toBeCloseToPixel(32);
  93. expect(chart.scales.xScale.labelRotation).toBeCloseTo(0);
  94. // Is yScale at the right spot
  95. expect(chart.scales.yScale.bottom).toBeCloseToPixel(142);
  96. expect(chart.scales.yScale.left).toBeCloseToPixel(216);
  97. expect(chart.scales.yScale.right).toBeCloseToPixel(250);
  98. expect(chart.scales.yScale.top).toBeCloseToPixel(62);
  99. expect(chart.scales.yScale.labelRotation).toBeCloseTo(0);
  100. });
  101. it('should fit scales with long labels correctly', function() {
  102. var chart = window.acquireChart({
  103. type: 'line',
  104. data: {
  105. datasets: [
  106. {data: [10, 5, 0, 25, 78, -10]}
  107. ],
  108. labels: ['tick1 is very long one', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6 is very long one']
  109. },
  110. options: {
  111. legend: {
  112. display: false
  113. },
  114. scales: {
  115. xAxes: [{
  116. id: 'xScale',
  117. type: 'category',
  118. ticks: {
  119. maxRotation: 0,
  120. autoSkip: false
  121. }
  122. }],
  123. yAxes: [{
  124. id: 'yScale',
  125. type: 'linear',
  126. position: 'right'
  127. }]
  128. }
  129. }
  130. }, {
  131. canvas: {
  132. height: 150,
  133. width: 512
  134. }
  135. });
  136. expect(chart.chartArea.bottom).toBeCloseToPixel(120);
  137. expect(chart.chartArea.left).toBeCloseToPixel(60);
  138. expect(chart.chartArea.right).toBeCloseToPixel(452);
  139. expect(chart.chartArea.top).toBeCloseToPixel(7);
  140. // Is xScale at the right spot
  141. expect(chart.scales.xScale.bottom).toBeCloseToPixel(150);
  142. expect(chart.scales.xScale.left).toBeCloseToPixel(60);
  143. expect(chart.scales.xScale.right).toBeCloseToPixel(452);
  144. expect(chart.scales.xScale.top).toBeCloseToPixel(120);
  145. expect(chart.scales.xScale.labelRotation).toBeCloseTo(0);
  146. expect(chart.scales.xScale.height).toBeCloseToPixel(30);
  147. expect(chart.scales.xScale.paddingLeft).toBeCloseToPixel(60);
  148. expect(chart.scales.xScale.paddingTop).toBeCloseToPixel(0);
  149. expect(chart.scales.xScale.paddingRight).toBeCloseToPixel(60);
  150. expect(chart.scales.xScale.paddingBottom).toBeCloseToPixel(0);
  151. // Is yScale at the right spot
  152. expect(chart.scales.yScale.bottom).toBeCloseToPixel(120);
  153. expect(chart.scales.yScale.left).toBeCloseToPixel(452);
  154. expect(chart.scales.yScale.right).toBeCloseToPixel(486);
  155. expect(chart.scales.yScale.top).toBeCloseToPixel(7);
  156. expect(chart.scales.yScale.labelRotation).toBeCloseTo(0);
  157. expect(chart.scales.yScale.width).toBeCloseToPixel(34);
  158. expect(chart.scales.yScale.paddingLeft).toBeCloseToPixel(0);
  159. expect(chart.scales.yScale.paddingTop).toBeCloseToPixel(7);
  160. expect(chart.scales.yScale.paddingRight).toBeCloseToPixel(0);
  161. expect(chart.scales.yScale.paddingBottom).toBeCloseToPixel(7);
  162. });
  163. it('should fit scales that overlap the chart area', function() {
  164. var chart = window.acquireChart({
  165. type: 'radar',
  166. data: {
  167. datasets: [{
  168. data: [10, 5, 0, 25, 78, -10]
  169. }, {
  170. data: [-19, -20, 0, -99, -50, 0]
  171. }],
  172. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  173. }
  174. });
  175. expect(chart.chartArea.bottom).toBeCloseToPixel(512);
  176. expect(chart.chartArea.left).toBeCloseToPixel(0);
  177. expect(chart.chartArea.right).toBeCloseToPixel(512);
  178. expect(chart.chartArea.top).toBeCloseToPixel(32);
  179. expect(chart.scale.bottom).toBeCloseToPixel(512);
  180. expect(chart.scale.left).toBeCloseToPixel(0);
  181. expect(chart.scale.right).toBeCloseToPixel(512);
  182. expect(chart.scale.top).toBeCloseToPixel(32);
  183. expect(chart.scale.width).toBeCloseToPixel(512);
  184. expect(chart.scale.height).toBeCloseToPixel(480);
  185. });
  186. it('should fit multiple axes in the same position', function() {
  187. var chart = window.acquireChart({
  188. type: 'bar',
  189. data: {
  190. datasets: [{
  191. yAxisID: 'yScale1',
  192. data: [10, 5, 0, 25, 78, -10]
  193. }, {
  194. yAxisID: 'yScale2',
  195. data: [-19, -20, 0, -99, -50, 0]
  196. }],
  197. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  198. },
  199. options: {
  200. scales: {
  201. xAxes: [{
  202. id: 'xScale',
  203. type: 'category'
  204. }],
  205. yAxes: [{
  206. id: 'yScale1',
  207. type: 'linear'
  208. }, {
  209. id: 'yScale2',
  210. type: 'linear'
  211. }]
  212. }
  213. }
  214. }, {
  215. canvas: {
  216. height: 150,
  217. width: 250
  218. }
  219. });
  220. expect(chart.chartArea.bottom).toBeCloseToPixel(118);
  221. expect(chart.chartArea.left).toBeCloseToPixel(73);
  222. expect(chart.chartArea.right).toBeCloseToPixel(247);
  223. expect(chart.chartArea.top).toBeCloseToPixel(32);
  224. // Is xScale at the right spot
  225. expect(chart.scales.xScale.bottom).toBeCloseToPixel(150);
  226. expect(chart.scales.xScale.left).toBeCloseToPixel(73);
  227. expect(chart.scales.xScale.right).toBeCloseToPixel(247);
  228. expect(chart.scales.xScale.top).toBeCloseToPixel(118);
  229. expect(chart.scales.xScale.labelRotation).toBeCloseTo(40, -1);
  230. // Are yScales at the right spot
  231. expect(chart.scales.yScale1.bottom).toBeCloseToPixel(118);
  232. expect(chart.scales.yScale1.left).toBeCloseToPixel(41);
  233. expect(chart.scales.yScale1.right).toBeCloseToPixel(73);
  234. expect(chart.scales.yScale1.top).toBeCloseToPixel(32);
  235. expect(chart.scales.yScale1.labelRotation).toBeCloseTo(0);
  236. expect(chart.scales.yScale2.bottom).toBeCloseToPixel(118);
  237. expect(chart.scales.yScale2.left).toBeCloseToPixel(0);
  238. expect(chart.scales.yScale2.right).toBeCloseToPixel(41);
  239. expect(chart.scales.yScale2.top).toBeCloseToPixel(32);
  240. expect(chart.scales.yScale2.labelRotation).toBeCloseTo(0);
  241. });
  242. it ('should fit a full width box correctly', function() {
  243. var chart = window.acquireChart({
  244. type: 'bar',
  245. data: {
  246. datasets: [{
  247. xAxisID: 'xScale1',
  248. data: [10, 5, 0, 25, 78, -10]
  249. }, {
  250. xAxisID: 'xScale2',
  251. data: [-19, -20, 0, -99, -50, 0]
  252. }],
  253. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  254. },
  255. options: {
  256. scales: {
  257. xAxes: [{
  258. id: 'xScale1',
  259. type: 'category'
  260. }, {
  261. id: 'xScale2',
  262. type: 'category',
  263. position: 'top',
  264. fullWidth: true
  265. }],
  266. yAxes: [{
  267. id: 'yScale',
  268. type: 'linear'
  269. }]
  270. }
  271. }
  272. });
  273. expect(chart.chartArea.bottom).toBeCloseToPixel(484);
  274. expect(chart.chartArea.left).toBeCloseToPixel(40);
  275. expect(chart.chartArea.right).toBeCloseToPixel(496);
  276. expect(chart.chartArea.top).toBeCloseToPixel(62);
  277. // Are xScales at the right spot
  278. expect(chart.scales.xScale1.bottom).toBeCloseToPixel(512);
  279. expect(chart.scales.xScale1.left).toBeCloseToPixel(40);
  280. expect(chart.scales.xScale1.right).toBeCloseToPixel(496);
  281. expect(chart.scales.xScale1.top).toBeCloseToPixel(484);
  282. expect(chart.scales.xScale2.bottom).toBeCloseToPixel(62);
  283. expect(chart.scales.xScale2.left).toBeCloseToPixel(0);
  284. expect(chart.scales.xScale2.right).toBeCloseToPixel(512);
  285. expect(chart.scales.xScale2.top).toBeCloseToPixel(32);
  286. // Is yScale at the right spot
  287. expect(chart.scales.yScale.bottom).toBeCloseToPixel(484);
  288. expect(chart.scales.yScale.left).toBeCloseToPixel(0);
  289. expect(chart.scales.yScale.right).toBeCloseToPixel(40);
  290. expect(chart.scales.yScale.top).toBeCloseToPixel(62);
  291. });
  292. describe('padding settings', function() {
  293. it('should apply a single padding to all dimensions', function() {
  294. var chart = window.acquireChart({
  295. type: 'bar',
  296. data: {
  297. datasets: [
  298. {
  299. data: [10, 5, 0, 25, 78, -10]
  300. }
  301. ],
  302. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  303. },
  304. options: {
  305. scales: {
  306. xAxes: [{
  307. id: 'xScale',
  308. type: 'category',
  309. display: false
  310. }],
  311. yAxes: [{
  312. id: 'yScale',
  313. type: 'linear',
  314. display: false
  315. }]
  316. },
  317. legend: {
  318. display: false
  319. },
  320. title: {
  321. display: false
  322. },
  323. layout: {
  324. padding: 10
  325. }
  326. }
  327. }, {
  328. canvas: {
  329. height: 150,
  330. width: 250
  331. }
  332. });
  333. expect(chart.chartArea.bottom).toBeCloseToPixel(140);
  334. expect(chart.chartArea.left).toBeCloseToPixel(10);
  335. expect(chart.chartArea.right).toBeCloseToPixel(240);
  336. expect(chart.chartArea.top).toBeCloseToPixel(10);
  337. });
  338. it('should apply padding in all positions', function() {
  339. var chart = window.acquireChart({
  340. type: 'bar',
  341. data: {
  342. datasets: [
  343. {
  344. data: [10, 5, 0, 25, 78, -10]
  345. }
  346. ],
  347. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  348. },
  349. options: {
  350. scales: {
  351. xAxes: [{
  352. id: 'xScale',
  353. type: 'category',
  354. display: false
  355. }],
  356. yAxes: [{
  357. id: 'yScale',
  358. type: 'linear',
  359. display: false
  360. }]
  361. },
  362. legend: {
  363. display: false
  364. },
  365. title: {
  366. display: false
  367. },
  368. layout: {
  369. padding: {
  370. left: 5,
  371. right: 15,
  372. top: 8,
  373. bottom: 12
  374. }
  375. }
  376. }
  377. }, {
  378. canvas: {
  379. height: 150,
  380. width: 250
  381. }
  382. });
  383. expect(chart.chartArea.bottom).toBeCloseToPixel(138);
  384. expect(chart.chartArea.left).toBeCloseToPixel(5);
  385. expect(chart.chartArea.right).toBeCloseToPixel(235);
  386. expect(chart.chartArea.top).toBeCloseToPixel(8);
  387. });
  388. it('should default to 0 padding if no dimensions specified', function() {
  389. var chart = window.acquireChart({
  390. type: 'bar',
  391. data: {
  392. datasets: [
  393. {
  394. data: [10, 5, 0, 25, 78, -10]
  395. }
  396. ],
  397. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  398. },
  399. options: {
  400. scales: {
  401. xAxes: [{
  402. id: 'xScale',
  403. type: 'category',
  404. display: false
  405. }],
  406. yAxes: [{
  407. id: 'yScale',
  408. type: 'linear',
  409. display: false
  410. }]
  411. },
  412. legend: {
  413. display: false
  414. },
  415. title: {
  416. display: false
  417. },
  418. layout: {
  419. padding: {}
  420. }
  421. }
  422. }, {
  423. canvas: {
  424. height: 150,
  425. width: 250
  426. }
  427. });
  428. expect(chart.chartArea.bottom).toBeCloseToPixel(150);
  429. expect(chart.chartArea.left).toBeCloseToPixel(0);
  430. expect(chart.chartArea.right).toBeCloseToPixel(250);
  431. expect(chart.chartArea.top).toBeCloseToPixel(0);
  432. });
  433. });
  434. describe('ordering by weight', function() {
  435. it('should keep higher weights outside', function() {
  436. var chart = window.acquireChart({
  437. type: 'bar',
  438. data: {
  439. datasets: [
  440. {
  441. data: [10, 5, 0, 25, 78, -10]
  442. }
  443. ],
  444. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  445. },
  446. options: {
  447. legend: {
  448. display: true,
  449. position: 'left',
  450. },
  451. title: {
  452. display: true,
  453. position: 'bottom',
  454. },
  455. },
  456. }, {
  457. canvas: {
  458. height: 150,
  459. width: 250
  460. }
  461. });
  462. var xAxis = chart.scales['x-axis-0'];
  463. var yAxis = chart.scales['y-axis-0'];
  464. var legend = chart.legend;
  465. var title = chart.titleBlock;
  466. expect(yAxis.left).toBe(legend.right);
  467. expect(xAxis.bottom).toBe(title.top);
  468. });
  469. it('should correctly set weights of scales and order them', function() {
  470. var chart = window.acquireChart({
  471. type: 'bar',
  472. data: {
  473. datasets: [
  474. {
  475. data: [10, 5, 0, 25, 78, -10]
  476. }
  477. ],
  478. labels: ['tick1', 'tick2', 'tick3', 'tick4', 'tick5', 'tick6']
  479. },
  480. options: {
  481. scales: {
  482. xAxes: [{
  483. id: 'xScale0',
  484. type: 'category',
  485. display: true,
  486. weight: 1
  487. }, {
  488. id: 'xScale1',
  489. type: 'category',
  490. display: true,
  491. weight: 2
  492. }, {
  493. id: 'xScale2',
  494. type: 'category',
  495. display: true
  496. }, {
  497. id: 'xScale3',
  498. type: 'category',
  499. display: true,
  500. position: 'top',
  501. weight: 1
  502. }, {
  503. id: 'xScale4',
  504. type: 'category',
  505. display: true,
  506. position: 'top',
  507. weight: 2
  508. }],
  509. yAxes: [{
  510. id: 'yScale0',
  511. type: 'linear',
  512. display: true,
  513. weight: 1
  514. }, {
  515. id: 'yScale1',
  516. type: 'linear',
  517. display: true,
  518. weight: 2
  519. }, {
  520. id: 'yScale2',
  521. type: 'linear',
  522. display: true
  523. }, {
  524. id: 'yScale3',
  525. type: 'linear',
  526. display: true,
  527. position: 'right',
  528. weight: 1
  529. }, {
  530. id: 'yScale4',
  531. type: 'linear',
  532. display: true,
  533. position: 'right',
  534. weight: 2
  535. }]
  536. }
  537. }
  538. }, {
  539. canvas: {
  540. height: 150,
  541. width: 250
  542. }
  543. });
  544. var xScale0 = chart.scales.xScale0;
  545. var xScale1 = chart.scales.xScale1;
  546. var xScale2 = chart.scales.xScale2;
  547. var xScale3 = chart.scales.xScale3;
  548. var xScale4 = chart.scales.xScale4;
  549. var yScale0 = chart.scales.yScale0;
  550. var yScale1 = chart.scales.yScale1;
  551. var yScale2 = chart.scales.yScale2;
  552. var yScale3 = chart.scales.yScale3;
  553. var yScale4 = chart.scales.yScale4;
  554. expect(xScale0.weight).toBe(1);
  555. expect(xScale1.weight).toBe(2);
  556. expect(xScale2.weight).toBe(0);
  557. expect(xScale3.weight).toBe(1);
  558. expect(xScale4.weight).toBe(2);
  559. expect(yScale0.weight).toBe(1);
  560. expect(yScale1.weight).toBe(2);
  561. expect(yScale2.weight).toBe(0);
  562. expect(yScale3.weight).toBe(1);
  563. expect(yScale4.weight).toBe(2);
  564. var isOrderCorrect = false;
  565. // bottom axes
  566. isOrderCorrect = xScale2.top < xScale0.top && xScale0.top < xScale1.top;
  567. expect(isOrderCorrect).toBe(true);
  568. // top axes
  569. isOrderCorrect = xScale4.top < xScale3.top;
  570. expect(isOrderCorrect).toBe(true);
  571. // left axes
  572. isOrderCorrect = yScale1.left < yScale0.left && yScale0.left < yScale2.left;
  573. expect(isOrderCorrect).toBe(true);
  574. // right axes
  575. isOrderCorrect = yScale3.left < yScale4.left;
  576. expect(isOrderCorrect).toBe(true);
  577. });
  578. });
  579. describe('box sizing', function() {
  580. it('should correctly compute y-axis width to fit labels', function() {
  581. var chart = window.acquireChart({
  582. type: 'bar',
  583. data: {
  584. labels: ['tick 1', 'tick 2', 'tick 3', 'tick 4', 'tick 5'],
  585. datasets: [{
  586. data: [0, 2.25, 1.5, 1.25, 2.5]
  587. }],
  588. },
  589. options: {
  590. legend: {
  591. display: false,
  592. },
  593. },
  594. }, {
  595. canvas: {
  596. height: 256,
  597. width: 256
  598. }
  599. });
  600. var yAxis = chart.scales['y-axis-0'];
  601. // issue #4441: y-axis labels partially hidden.
  602. // minimum horizontal space required to fit labels
  603. expect(yAxis.width).toBeCloseToPixel(33);
  604. expect(yAxis.ticks).toEqual(['2.5', '2.0', '1.5', '1.0', '0.5', '0']);
  605. });
  606. });
  607. });