scale.linear.tests.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. describe('Linear Scale', function() {
  2. it('Should register the constructor with the scale service', function() {
  3. var Constructor = Chart.scaleService.getScaleConstructor('linear');
  4. expect(Constructor).not.toBe(undefined);
  5. expect(typeof Constructor).toBe('function');
  6. });
  7. it('Should have the correct default config', function() {
  8. var defaultConfig = Chart.scaleService.getScaleDefaults('linear');
  9. expect(defaultConfig).toEqual({
  10. display: true,
  11. gridLines: {
  12. color: 'rgba(0, 0, 0, 0.1)',
  13. drawBorder: true,
  14. drawOnChartArea: true,
  15. drawTicks: true, // draw ticks extending towards the label
  16. tickMarkLength: 10,
  17. lineWidth: 1,
  18. offsetGridLines: false,
  19. display: true,
  20. zeroLineColor: 'rgba(0,0,0,0.25)',
  21. zeroLineWidth: 1,
  22. zeroLineBorderDash: [],
  23. zeroLineBorderDashOffset: 0.0,
  24. borderDash: [],
  25. borderDashOffset: 0.0
  26. },
  27. position: 'left',
  28. offset: false,
  29. scaleLabel: Chart.defaults.scale.scaleLabel,
  30. ticks: {
  31. beginAtZero: false,
  32. minRotation: 0,
  33. maxRotation: 50,
  34. mirror: false,
  35. padding: 0,
  36. reverse: false,
  37. display: true,
  38. callback: defaultConfig.ticks.callback, // make this work nicer, then check below
  39. autoSkip: true,
  40. autoSkipPadding: 0,
  41. labelOffset: 0,
  42. minor: {},
  43. major: {},
  44. }
  45. });
  46. expect(defaultConfig.ticks.callback).toEqual(jasmine.any(Function));
  47. });
  48. it('Should correctly determine the max & min data values', function() {
  49. var chart = window.acquireChart({
  50. type: 'bar',
  51. data: {
  52. datasets: [{
  53. yAxisID: 'yScale0',
  54. data: [10, 5, 0, -5, 78, -100]
  55. }, {
  56. yAxisID: 'yScale1',
  57. data: [-1000, 1000],
  58. }, {
  59. yAxisID: 'yScale0',
  60. data: [150]
  61. }],
  62. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  63. },
  64. options: {
  65. scales: {
  66. yAxes: [{
  67. id: 'yScale0',
  68. type: 'linear'
  69. }, {
  70. id: 'yScale1',
  71. type: 'linear'
  72. }]
  73. }
  74. }
  75. });
  76. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  77. expect(chart.scales.yScale0.min).toBe(-100);
  78. expect(chart.scales.yScale0.max).toBe(150);
  79. });
  80. it('Should correctly determine the max & min of string data values', function() {
  81. var chart = window.acquireChart({
  82. type: 'bar',
  83. data: {
  84. datasets: [{
  85. yAxisID: 'yScale0',
  86. data: ['10', '5', '0', '-5', '78', '-100']
  87. }, {
  88. yAxisID: 'yScale1',
  89. data: ['-1000', '1000'],
  90. }, {
  91. yAxisID: 'yScale0',
  92. data: ['150']
  93. }],
  94. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  95. },
  96. options: {
  97. scales: {
  98. yAxes: [{
  99. id: 'yScale0',
  100. type: 'linear'
  101. }, {
  102. id: 'yScale1',
  103. type: 'linear'
  104. }]
  105. }
  106. }
  107. });
  108. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  109. expect(chart.scales.yScale0.min).toBe(-100);
  110. expect(chart.scales.yScale0.max).toBe(150);
  111. });
  112. it('Should correctly determine the max & min when no values provided and suggested minimum and maximum are set', function() {
  113. var chart = window.acquireChart({
  114. type: 'bar',
  115. data: {
  116. datasets: [{
  117. yAxisID: 'yScale0',
  118. data: []
  119. }],
  120. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  121. },
  122. options: {
  123. scales: {
  124. yAxes: [{
  125. id: 'yScale0',
  126. type: 'linear',
  127. ticks: {
  128. suggestedMin: -10,
  129. suggestedMax: 15
  130. }
  131. }]
  132. }
  133. }
  134. });
  135. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  136. expect(chart.scales.yScale0.min).toBe(-10);
  137. expect(chart.scales.yScale0.max).toBe(15);
  138. });
  139. it('Should correctly determine the max & min data values ignoring hidden datasets', function() {
  140. var chart = window.acquireChart({
  141. type: 'bar',
  142. data: {
  143. datasets: [{
  144. yAxisID: 'yScale0',
  145. data: ['10', '5', '0', '-5', '78', '-100']
  146. }, {
  147. yAxisID: 'yScale1',
  148. data: ['-1000', '1000'],
  149. }, {
  150. yAxisID: 'yScale0',
  151. data: ['150'],
  152. hidden: true
  153. }],
  154. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  155. },
  156. options: {
  157. scales: {
  158. yAxes: [{
  159. id: 'yScale0',
  160. type: 'linear'
  161. }, {
  162. id: 'yScale1',
  163. type: 'linear'
  164. }]
  165. }
  166. }
  167. });
  168. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  169. expect(chart.scales.yScale0.min).toBe(-100);
  170. expect(chart.scales.yScale0.max).toBe(80);
  171. });
  172. it('Should correctly determine the max & min data values ignoring data that is NaN', function() {
  173. var chart = window.acquireChart({
  174. type: 'bar',
  175. data: {
  176. datasets: [{
  177. yAxisID: 'yScale0',
  178. data: [null, 90, NaN, undefined, 45, 30, Infinity, -Infinity]
  179. }],
  180. labels: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
  181. },
  182. options: {
  183. scales: {
  184. yAxes: [{
  185. id: 'yScale0',
  186. type: 'linear'
  187. }]
  188. }
  189. }
  190. });
  191. expect(chart.scales.yScale0.min).toBe(30);
  192. expect(chart.scales.yScale0.max).toBe(90);
  193. // Scale is now stacked
  194. chart.scales.yScale0.options.stacked = true;
  195. chart.update();
  196. expect(chart.scales.yScale0.min).toBe(0);
  197. expect(chart.scales.yScale0.max).toBe(90);
  198. });
  199. it('Should correctly determine the max & min for scatter data', function() {
  200. var chart = window.acquireChart({
  201. type: 'line',
  202. data: {
  203. datasets: [{
  204. xAxisID: 'xScale0',
  205. yAxisID: 'yScale0',
  206. data: [{
  207. x: 10,
  208. y: 100
  209. }, {
  210. x: -10,
  211. y: 0
  212. }, {
  213. x: 0,
  214. y: 0
  215. }, {
  216. x: 99,
  217. y: 7
  218. }]
  219. }],
  220. },
  221. options: {
  222. scales: {
  223. xAxes: [{
  224. id: 'xScale0',
  225. type: 'linear',
  226. position: 'bottom'
  227. }],
  228. yAxes: [{
  229. id: 'yScale0',
  230. type: 'linear'
  231. }]
  232. }
  233. }
  234. });
  235. chart.update();
  236. expect(chart.scales.xScale0.min).toBe(-20);
  237. expect(chart.scales.xScale0.max).toBe(100);
  238. expect(chart.scales.yScale0.min).toBe(0);
  239. expect(chart.scales.yScale0.max).toBe(100);
  240. });
  241. it('Should correctly get the label for the given index', function() {
  242. var chart = window.acquireChart({
  243. type: 'line',
  244. data: {
  245. datasets: [{
  246. xAxisID: 'xScale0',
  247. yAxisID: 'yScale0',
  248. data: [{
  249. x: 10,
  250. y: 100
  251. }, {
  252. x: -10,
  253. y: 0
  254. }, {
  255. x: 0,
  256. y: 0
  257. }, {
  258. x: 99,
  259. y: 7
  260. }]
  261. }],
  262. },
  263. options: {
  264. scales: {
  265. xAxes: [{
  266. id: 'xScale0',
  267. type: 'linear',
  268. position: 'bottom'
  269. }],
  270. yAxes: [{
  271. id: 'yScale0',
  272. type: 'linear'
  273. }]
  274. }
  275. }
  276. });
  277. chart.update();
  278. expect(chart.scales.yScale0.getLabelForIndex(3, 0)).toBe(7);
  279. });
  280. it('Should correctly determine the min and max data values when stacked mode is turned on', function() {
  281. var chart = window.acquireChart({
  282. type: 'line',
  283. data: {
  284. datasets: [{
  285. yAxisID: 'yScale0',
  286. data: [10, 5, 0, -5, 78, -100],
  287. type: 'bar'
  288. }, {
  289. yAxisID: 'yScale1',
  290. data: [-1000, 1000],
  291. }, {
  292. yAxisID: 'yScale0',
  293. data: [150, 0, 0, -100, -10, 9],
  294. type: 'bar'
  295. }, {
  296. yAxisID: 'yScale0',
  297. data: [10, 10, 10, 10, 10, 10],
  298. type: 'line'
  299. }],
  300. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  301. },
  302. options: {
  303. scales: {
  304. yAxes: [{
  305. id: 'yScale0',
  306. type: 'linear',
  307. stacked: true
  308. }, {
  309. id: 'yScale1',
  310. type: 'linear'
  311. }]
  312. }
  313. }
  314. });
  315. chart.update();
  316. expect(chart.scales.yScale0.min).toBe(-150);
  317. expect(chart.scales.yScale0.max).toBe(200);
  318. });
  319. it('Should correctly determine the min and max data values when stacked mode is turned on and there are hidden datasets', function() {
  320. var chart = window.acquireChart({
  321. type: 'bar',
  322. data: {
  323. datasets: [{
  324. yAxisID: 'yScale0',
  325. data: [10, 5, 0, -5, 78, -100],
  326. }, {
  327. yAxisID: 'yScale1',
  328. data: [-1000, 1000],
  329. }, {
  330. yAxisID: 'yScale0',
  331. data: [150, 0, 0, -100, -10, 9],
  332. }, {
  333. yAxisID: 'yScale0',
  334. data: [10, 20, 30, 40, 50, 60],
  335. hidden: true
  336. }],
  337. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  338. },
  339. options: {
  340. scales: {
  341. yAxes: [{
  342. id: 'yScale0',
  343. type: 'linear',
  344. stacked: true
  345. }, {
  346. id: 'yScale1',
  347. type: 'linear'
  348. }]
  349. }
  350. }
  351. });
  352. chart.update();
  353. expect(chart.scales.yScale0.min).toBe(-150);
  354. expect(chart.scales.yScale0.max).toBe(200);
  355. });
  356. it('Should correctly determine the min and max data values when stacked mode is turned on there are multiple types of datasets', function() {
  357. var chart = window.acquireChart({
  358. type: 'bar',
  359. data: {
  360. datasets: [{
  361. yAxisID: 'yScale0',
  362. type: 'bar',
  363. data: [10, 5, 0, -5, 78, -100]
  364. }, {
  365. type: 'line',
  366. data: [10, 10, 10, 10, 10, 10],
  367. }, {
  368. type: 'bar',
  369. data: [150, 0, 0, -100, -10, 9]
  370. }],
  371. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  372. },
  373. options: {
  374. scales: {
  375. yAxes: [{
  376. id: 'yScale0',
  377. type: 'linear',
  378. stacked: true
  379. }]
  380. }
  381. }
  382. });
  383. chart.scales.yScale0.determineDataLimits();
  384. expect(chart.scales.yScale0.min).toBe(-105);
  385. expect(chart.scales.yScale0.max).toBe(160);
  386. });
  387. it('Should ensure that the scale has a max and min that are not equal', function() {
  388. var chart = window.acquireChart({
  389. type: 'bar',
  390. data: {
  391. datasets: [],
  392. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  393. },
  394. options: {
  395. scales: {
  396. yAxes: [{
  397. id: 'yScale0',
  398. type: 'linear'
  399. }]
  400. }
  401. }
  402. });
  403. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  404. expect(chart.scales.yScale0.min).toBe(-1);
  405. expect(chart.scales.yScale0.max).toBe(1);
  406. });
  407. it('Should ensure that the scale has a max and min that are not equal when beginAtZero is set', function() {
  408. var chart = window.acquireChart({
  409. type: 'bar',
  410. data: {
  411. datasets: [],
  412. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  413. },
  414. options: {
  415. scales: {
  416. yAxes: [{
  417. id: 'yScale0',
  418. type: 'linear',
  419. ticks: {
  420. beginAtZero: true
  421. }
  422. }]
  423. }
  424. }
  425. });
  426. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  427. expect(chart.scales.yScale0.min).toBe(0);
  428. expect(chart.scales.yScale0.max).toBe(1);
  429. });
  430. it('Should use the suggestedMin and suggestedMax options', function() {
  431. var chart = window.acquireChart({
  432. type: 'bar',
  433. data: {
  434. datasets: [{
  435. yAxisID: 'yScale0',
  436. data: [1, 1, 1, 2, 1, 0]
  437. }],
  438. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  439. },
  440. options: {
  441. scales: {
  442. yAxes: [{
  443. id: 'yScale0',
  444. type: 'linear',
  445. ticks: {
  446. suggestedMax: 10,
  447. suggestedMin: -10
  448. }
  449. }]
  450. }
  451. }
  452. });
  453. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  454. expect(chart.scales.yScale0.min).toBe(-10);
  455. expect(chart.scales.yScale0.max).toBe(10);
  456. });
  457. it('Should use the min and max options', function() {
  458. var chart = window.acquireChart({
  459. type: 'bar',
  460. data: {
  461. datasets: [{
  462. yAxisID: 'yScale0',
  463. data: [1, 1, 1, 2, 1, 0]
  464. }],
  465. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  466. },
  467. options: {
  468. scales: {
  469. yAxes: [{
  470. id: 'yScale0',
  471. type: 'linear',
  472. ticks: {
  473. max: 1010,
  474. min: -1010
  475. }
  476. }]
  477. }
  478. }
  479. });
  480. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  481. expect(chart.scales.yScale0.min).toBe(-1010);
  482. expect(chart.scales.yScale0.max).toBe(1010);
  483. expect(chart.scales.yScale0.ticks[0]).toBe('1010');
  484. expect(chart.scales.yScale0.ticks[chart.scales.yScale0.ticks.length - 1]).toBe('-1010');
  485. });
  486. it('Should use min, max and stepSize to create fixed spaced ticks', function() {
  487. var chart = window.acquireChart({
  488. type: 'bar',
  489. data: {
  490. datasets: [{
  491. yAxisID: 'yScale0',
  492. data: [10, 3, 6, 8, 3, 1]
  493. }],
  494. labels: ['a', 'b', 'c', 'd', 'e', 'f']
  495. },
  496. options: {
  497. scales: {
  498. yAxes: [{
  499. id: 'yScale0',
  500. type: 'linear',
  501. ticks: {
  502. min: 1,
  503. max: 11,
  504. stepSize: 2
  505. }
  506. }]
  507. }
  508. }
  509. });
  510. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  511. expect(chart.scales.yScale0.min).toBe(1);
  512. expect(chart.scales.yScale0.max).toBe(11);
  513. expect(chart.scales.yScale0.ticks).toEqual(['11', '9', '7', '5', '3', '1']);
  514. });
  515. it('should forcibly include 0 in the range if the beginAtZero option is used', function() {
  516. var chart = window.acquireChart({
  517. type: 'bar',
  518. data: {
  519. datasets: [{
  520. yAxisID: 'yScale0',
  521. data: [20, 30, 40, 50]
  522. }],
  523. labels: ['a', 'b', 'c', 'd']
  524. },
  525. options: {
  526. scales: {
  527. yAxes: [{
  528. id: 'yScale0',
  529. type: 'linear',
  530. }]
  531. }
  532. }
  533. });
  534. expect(chart.scales.yScale0).not.toEqual(undefined); // must construct
  535. expect(chart.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20']);
  536. chart.scales.yScale0.options.ticks.beginAtZero = true;
  537. chart.update();
  538. expect(chart.scales.yScale0.ticks).toEqual(['50', '45', '40', '35', '30', '25', '20', '15', '10', '5', '0']);
  539. chart.data.datasets[0].data = [-20, -30, -40, -50];
  540. chart.update();
  541. expect(chart.scales.yScale0.ticks).toEqual(['0', '-5', '-10', '-15', '-20', '-25', '-30', '-35', '-40', '-45', '-50']);
  542. chart.scales.yScale0.options.ticks.beginAtZero = false;
  543. chart.update();
  544. expect(chart.scales.yScale0.ticks).toEqual(['-20', '-25', '-30', '-35', '-40', '-45', '-50']);
  545. });
  546. it('Should generate tick marks in the correct order in reversed mode', function() {
  547. var chart = window.acquireChart({
  548. type: 'bar',
  549. data: {
  550. datasets: [{
  551. yAxisID: 'yScale0',
  552. data: [10, 5, 0, 25, 78]
  553. }],
  554. labels: ['a', 'b', 'c', 'd']
  555. },
  556. options: {
  557. scales: {
  558. yAxes: [{
  559. id: 'yScale0',
  560. type: 'linear',
  561. ticks: {
  562. reverse: true
  563. }
  564. }]
  565. }
  566. }
  567. });
  568. expect(chart.scales.yScale0.ticks).toEqual(['0', '10', '20', '30', '40', '50', '60', '70', '80']);
  569. expect(chart.scales.yScale0.start).toBe(80);
  570. expect(chart.scales.yScale0.end).toBe(0);
  571. });
  572. it('should use the correct number of decimal places in the default format function', function() {
  573. var chart = window.acquireChart({
  574. type: 'bar',
  575. data: {
  576. datasets: [{
  577. yAxisID: 'yScale0',
  578. data: [0.06, 0.005, 0, 0.025, 0.0078]
  579. }],
  580. labels: ['a', 'b', 'c', 'd']
  581. },
  582. options: {
  583. scales: {
  584. yAxes: [{
  585. id: 'yScale0',
  586. type: 'linear',
  587. }]
  588. }
  589. }
  590. });
  591. expect(chart.scales.yScale0.ticks).toEqual(['0.06', '0.05', '0.04', '0.03', '0.02', '0.01', '0']);
  592. });
  593. it('Should build labels using the user supplied callback', function() {
  594. var chart = window.acquireChart({
  595. type: 'bar',
  596. data: {
  597. datasets: [{
  598. yAxisID: 'yScale0',
  599. data: [10, 5, 0, 25, 78]
  600. }],
  601. labels: ['a', 'b', 'c', 'd']
  602. },
  603. options: {
  604. scales: {
  605. yAxes: [{
  606. id: 'yScale0',
  607. type: 'linear',
  608. ticks: {
  609. callback: function(value, index) {
  610. return index.toString();
  611. }
  612. }
  613. }]
  614. }
  615. }
  616. });
  617. // Just the index
  618. expect(chart.scales.yScale0.ticks).toEqual(['0', '1', '2', '3', '4', '5', '6', '7', '8']);
  619. });
  620. it('Should get the correct pixel value for a point', function() {
  621. var chart = window.acquireChart({
  622. type: 'line',
  623. data: {
  624. datasets: [{
  625. xAxisID: 'xScale0',
  626. yAxisID: 'yScale0',
  627. data: []
  628. }],
  629. },
  630. options: {
  631. scales: {
  632. xAxes: [{
  633. id: 'xScale0',
  634. type: 'linear',
  635. position: 'bottom'
  636. }],
  637. yAxes: [{
  638. id: 'yScale0',
  639. type: 'linear'
  640. }]
  641. }
  642. }
  643. });
  644. var xScale = chart.scales.xScale0;
  645. expect(xScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(501); // right - paddingRight
  646. expect(xScale.getPixelForValue(-1, 0, 0)).toBeCloseToPixel(31 + 6); // left + paddingLeft + lineSpace
  647. expect(xScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(266 + 6 / 2); // halfway*/
  648. expect(xScale.getValueForPixel(501)).toBeCloseTo(1, 1e-2);
  649. expect(xScale.getValueForPixel(31)).toBeCloseTo(-1, 1e-2);
  650. expect(xScale.getValueForPixel(266)).toBeCloseTo(0, 1e-2);
  651. var yScale = chart.scales.yScale0;
  652. expect(yScale.getPixelForValue(1, 0, 0)).toBeCloseToPixel(32); // right - paddingRight
  653. expect(yScale.getPixelForValue(-1, 0, 0)).toBeCloseToPixel(484); // left + paddingLeft
  654. expect(yScale.getPixelForValue(0, 0, 0)).toBeCloseToPixel(258); // halfway*/
  655. expect(yScale.getValueForPixel(32)).toBe(1);
  656. expect(yScale.getValueForPixel(484)).toBe(-1);
  657. expect(yScale.getValueForPixel(258)).toBe(0);
  658. });
  659. it('should fit correctly', function() {
  660. var chart = window.acquireChart({
  661. type: 'line',
  662. data: {
  663. datasets: [{
  664. xAxisID: 'xScale0',
  665. yAxisID: 'yScale0',
  666. data: [{
  667. x: 10,
  668. y: 100
  669. }, {
  670. x: -10,
  671. y: 0
  672. }, {
  673. x: 0,
  674. y: 0
  675. }, {
  676. x: 99,
  677. y: 7
  678. }]
  679. }],
  680. },
  681. options: {
  682. scales: {
  683. xAxes: [{
  684. id: 'xScale0',
  685. type: 'linear',
  686. position: 'bottom'
  687. }],
  688. yAxes: [{
  689. id: 'yScale0',
  690. type: 'linear'
  691. }]
  692. }
  693. }
  694. });
  695. var xScale = chart.scales.xScale0;
  696. expect(xScale.paddingTop).toBeCloseToPixel(0);
  697. expect(xScale.paddingBottom).toBeCloseToPixel(0);
  698. expect(xScale.paddingLeft).toBeCloseToPixel(0);
  699. expect(xScale.paddingRight).toBeCloseToPixel(0);
  700. expect(xScale.width).toBeCloseToPixel(468 - 6); // minus lineSpace
  701. expect(xScale.height).toBeCloseToPixel(28);
  702. var yScale = chart.scales.yScale0;
  703. expect(yScale.paddingTop).toBeCloseToPixel(0);
  704. expect(yScale.paddingBottom).toBeCloseToPixel(0);
  705. expect(yScale.paddingLeft).toBeCloseToPixel(0);
  706. expect(yScale.paddingRight).toBeCloseToPixel(0);
  707. expect(yScale.width).toBeCloseToPixel(30 + 6); // plus lineSpace
  708. expect(yScale.height).toBeCloseToPixel(452);
  709. // Extra size when scale label showing
  710. xScale.options.scaleLabel.display = true;
  711. yScale.options.scaleLabel.display = true;
  712. chart.update();
  713. expect(xScale.paddingTop).toBeCloseToPixel(0);
  714. expect(xScale.paddingBottom).toBeCloseToPixel(0);
  715. expect(xScale.paddingLeft).toBeCloseToPixel(0);
  716. expect(xScale.paddingRight).toBeCloseToPixel(0);
  717. expect(xScale.width).toBeCloseToPixel(440);
  718. expect(xScale.height).toBeCloseToPixel(50);
  719. expect(yScale.paddingTop).toBeCloseToPixel(0);
  720. expect(yScale.paddingBottom).toBeCloseToPixel(0);
  721. expect(yScale.paddingLeft).toBeCloseToPixel(0);
  722. expect(yScale.paddingRight).toBeCloseToPixel(0);
  723. expect(yScale.width).toBeCloseToPixel(58);
  724. expect(yScale.height).toBeCloseToPixel(430);
  725. });
  726. it('should fit correctly when display is turned off', function() {
  727. var chart = window.acquireChart({
  728. type: 'line',
  729. data: {
  730. datasets: [{
  731. xAxisID: 'xScale0',
  732. yAxisID: 'yScale0',
  733. data: [{
  734. x: 10,
  735. y: 100
  736. }, {
  737. x: -10,
  738. y: 0
  739. }, {
  740. x: 0,
  741. y: 0
  742. }, {
  743. x: 99,
  744. y: 7
  745. }]
  746. }],
  747. },
  748. options: {
  749. scales: {
  750. xAxes: [{
  751. id: 'xScale0',
  752. type: 'linear',
  753. position: 'bottom'
  754. }],
  755. yAxes: [{
  756. id: 'yScale0',
  757. type: 'linear',
  758. gridLines: {
  759. drawTicks: false,
  760. drawBorder: false
  761. },
  762. scaleLabel: {
  763. display: false,
  764. lineHeight: 1.2
  765. },
  766. ticks: {
  767. display: false,
  768. padding: 0
  769. }
  770. }]
  771. }
  772. }
  773. });
  774. var yScale = chart.scales.yScale0;
  775. expect(yScale.width).toBeCloseToPixel(0);
  776. });
  777. it('max and min value should be valid and finite when charts datasets are hidden', function() {
  778. var barData = {
  779. labels: ['S1', 'S2', 'S3'],
  780. datasets: [{
  781. label: 'Closed',
  782. backgroundColor: '#382765',
  783. data: [2500, 2000, 1500]
  784. }, {
  785. label: 'In Progress',
  786. backgroundColor: '#7BC225',
  787. data: [1000, 2000, 1500]
  788. }, {
  789. label: 'Assigned',
  790. backgroundColor: '#ffC225',
  791. data: [1000, 2000, 1500]
  792. }]
  793. };
  794. var chart = window.acquireChart({
  795. type: 'horizontalBar',
  796. data: barData,
  797. options: {
  798. scales: {
  799. xAxes: [{
  800. stacked: true
  801. }],
  802. yAxes: [{
  803. stacked: true
  804. }]
  805. }
  806. }
  807. });
  808. barData.datasets.forEach(function(data, index) {
  809. var meta = chart.getDatasetMeta(index);
  810. meta.hidden = true;
  811. chart.update();
  812. });
  813. expect(chart.scales['x-axis-0'].min).toEqual(0);
  814. expect(chart.scales['x-axis-0'].max).toEqual(1);
  815. });
  816. it('max and min value should be valid when min is set and all datasets are hidden', function() {
  817. var barData = {
  818. labels: ['S1', 'S2', 'S3'],
  819. datasets: [{
  820. label: 'dataset 1',
  821. backgroundColor: '#382765',
  822. data: [2500, 2000, 1500],
  823. hidden: true,
  824. }]
  825. };
  826. var chart = window.acquireChart({
  827. type: 'horizontalBar',
  828. data: barData,
  829. options: {
  830. scales: {
  831. xAxes: [{
  832. ticks: {
  833. min: 20
  834. }
  835. }]
  836. }
  837. }
  838. });
  839. expect(chart.scales['x-axis-0'].min).toEqual(20);
  840. expect(chart.scales['x-axis-0'].max).toEqual(21);
  841. });
  842. });