core.ticks.tests.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. describe('Test tick generators', function() {
  2. // formatters are used as default config values so users want to be able to reference them
  3. it('Should expose formatters api', function() {
  4. expect(typeof Chart.Ticks).toBeDefined();
  5. expect(typeof Chart.Ticks.formatters).toBeDefined();
  6. expect(typeof Chart.Ticks.formatters.values).toBe('function');
  7. expect(typeof Chart.Ticks.formatters.linear).toBe('function');
  8. expect(typeof Chart.Ticks.formatters.logarithmic).toBe('function');
  9. });
  10. it('Should generate linear spaced ticks with correct precision', function() {
  11. var chart = window.acquireChart({
  12. type: 'line',
  13. data: {
  14. datasets: [{
  15. data: []
  16. }],
  17. },
  18. options: {
  19. legend: {
  20. display: false,
  21. },
  22. scales: {
  23. xAxes: [{
  24. type: 'linear',
  25. position: 'bottom',
  26. ticks: {
  27. callback: function(value) {
  28. return value.toString();
  29. }
  30. }
  31. }],
  32. yAxes: [{
  33. type: 'linear',
  34. ticks: {
  35. callback: function(value) {
  36. return value.toString();
  37. }
  38. }
  39. }]
  40. }
  41. }
  42. });
  43. var xAxis = chart.scales['x-axis-0'];
  44. var yAxis = chart.scales['y-axis-0'];
  45. expect(xAxis.ticks).toEqual(['-1', '-0.8', '-0.6', '-0.4', '-0.2', '0', '0.2', '0.4', '0.6', '0.8', '1']);
  46. expect(yAxis.ticks).toEqual(['1', '0.8', '0.6', '0.4', '0.2', '0', '-0.2', '-0.4', '-0.6', '-0.8', '-1']);
  47. });
  48. it('Should generate logarithmic spaced ticks with correct precision', function() {
  49. var chart = window.acquireChart({
  50. type: 'line',
  51. data: {
  52. datasets: [{
  53. data: []
  54. }],
  55. },
  56. options: {
  57. legend: {
  58. display: false,
  59. },
  60. scales: {
  61. xAxes: [{
  62. type: 'logarithmic',
  63. position: 'bottom',
  64. ticks: {
  65. min: 0.1,
  66. max: 1,
  67. callback: function(value) {
  68. return value.toString();
  69. }
  70. }
  71. }],
  72. yAxes: [{
  73. type: 'logarithmic',
  74. ticks: {
  75. min: 0.1,
  76. max: 1,
  77. callback: function(value) {
  78. return value.toString();
  79. }
  80. }
  81. }]
  82. }
  83. }
  84. });
  85. var xAxis = chart.scales['x-axis-0'];
  86. var yAxis = chart.scales['y-axis-0'];
  87. expect(xAxis.ticks).toEqual(['0.1', '0.2', '0.3', '0.4', '0.5', '0.6', '0.7', '0.8', '0.9', '1']);
  88. expect(yAxis.ticks).toEqual(['1', '0.9', '0.8', '0.7', '0.6', '0.5', '0.4', '0.3', '0.2', '0.1']);
  89. });
  90. });