core.layouts.tests.js 15 KB

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