jquery.flot-precision.Test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* eslint-disable */
  2. describe("unit tests for the precision of axis", function() {
  3. var plot;
  4. var placeholder;
  5. var sampledata = [[0, 1], [1, 1.1], [2, 1.2]];
  6. beforeEach(function () {
  7. var fixture = setFixtures('<div id="demo-container" style="width: 800px;height: 600px">').find('#demo-container').get(0);
  8. placeholder = $('<div id="placeholder" style="width: 100%;height: 100%">');
  9. placeholder.appendTo(fixture);
  10. });
  11. afterEach(function () {
  12. if (plot) {
  13. plot.shutdown();
  14. }
  15. $('#placeholder').empty();
  16. });
  17. it('should use the precision given by tickDecimals when specified', function() {
  18. plot = $.plot("#placeholder", [sampledata], {});
  19. var testVector = [
  20. [1, 10, 10, 3, 1],
  21. [1, 1.01, 10, 2, 2],
  22. [0.99963, 0.99964, null, 3, 3],
  23. [1, 1.1, 5, 1, 1],
  24. [1, 1.00000000000001, 10, 5, 5]
  25. ];
  26. testVector.forEach(function (t) {
  27. var min = t[0],
  28. max = t[1],
  29. ticks = t[2],
  30. tickDecimals = t[3],
  31. expectedValue = t[4];
  32. var precision = plot.computeValuePrecision(min, max, "x", ticks, tickDecimals);
  33. expect(precision).toEqual(expectedValue);
  34. });
  35. });
  36. it('should use the maximum precision when tickDecimals not specified', function() {
  37. plot = $.plot("#placeholder", [sampledata], {});
  38. var testVector = [
  39. [1, 10, 10, 1],
  40. [1, 1.01, 10, 3],
  41. [1, 1.1, 5, 2],
  42. [0.99963, 0.99964, null, 6],
  43. [1, 1.00000000000001, 10, 16],
  44. [-200000, 200000, undefined, -4]
  45. ];
  46. testVector.forEach(function (t) {
  47. var min = t[0],
  48. max = t[1],
  49. ticks = t[2],
  50. expectedValue = t[3];
  51. var precision = plot.computeValuePrecision(min, max, "x", ticks);
  52. expect(precision).toEqual(expectedValue);
  53. });
  54. });
  55. it('should increase precision for endpoints', function() {
  56. var testVector = [
  57. [1, 10, 10, 'linear', '1.00', '10.00'],
  58. [1, 100, 11, 'linear', '1.0', '100.0'],
  59. [-1, 1, 20, 'linear', '-1.000', '1.000'],
  60. [1, 1.01, 10, 'linear', '1.00000', '1.01000'],
  61. [99, 99.02, 10, 'linear', '99.00000', '99.02000'],
  62. [0.99963, 0.99964, null, 'linear', '0.99963000', '0.99964000'],
  63. [1, 1.00000000001, 100, 'linear', '1.00000000000000', '1.00000000001000'],
  64. [-2000000, 2000000, null, 'linear', '-2000000', '2000000'],
  65. [-2.18167311226e+21, 2.196693453e+21, null, 'linear', '-2.182e21', '2.197e21'],
  66. [1, 10, 10, 'log', '1.000', '10.00'],
  67. [1, 10000000, 10, 'log', '1.00', '10000000'],
  68. [0.1, 100, 11, 'log', '0.1000', '100.0'],
  69. [0.99963, 0.99964, null, 'log', '0.99963000', '0.99964000'],
  70. [1, 2000000, null, 'log', '1.00', '2000000'],
  71. [1.14567e-43, 1, null, 'log', '1.1e-43', '1.0'],
  72. [0, 1, null, 'log', '0.1000', '1.000']
  73. ];
  74. testVector.forEach(function (t) {
  75. plot = $.plot("#placeholder", [sampledata], {
  76. xaxes: [{
  77. min: t[0],
  78. max: t[1],
  79. ticks: t[2],
  80. showTickLabels : 'endpoints',
  81. autoScale: "none",
  82. mode: t[3]
  83. }]
  84. });
  85. var minExpectedValue = t[4],
  86. maxExpectedValue = t[5],
  87. xaxis = plot.getAxes().xaxis;
  88. expect(xaxis.ticks[0].label).toEqual(minExpectedValue);
  89. expect(xaxis.ticks[xaxis.ticks.length-1].label).toEqual(maxExpectedValue);
  90. });
  91. });
  92. describe('tickFormatter', function(){
  93. it('should ignore the computed precision of axis if negative', function(){
  94. var testVector = [[-12356285.9999, -10, '-12356286'],
  95. [12356285.9999, -10, '12356286'],
  96. [3.215, -1, '3'],
  97. [3.215, -2, '3'],
  98. [3.215, -3, '3'],
  99. [3.215, -50, '3']];
  100. testVector.forEach(function (t) {
  101. plot = $.plot("#placeholder", [], {
  102. xaxes: [{
  103. showTickLabels : 'all',
  104. }]
  105. });
  106. var axis = plot.getXAxes()[0];
  107. expect($.plot.defaultTickFormatter(t[0], axis, t[1])).toEqual(t[2]);
  108. });
  109. });
  110. it('should show small number of decimals for numbers with e representation', function(){
  111. var testVector = [[0.12e-8, 1.2e-7, '1.2e-9', '1.200e-7'],
  112. [1.2e+21, 12e+21, '1.20e21', '1.200e22'],
  113. [1e-18, 2e-18, '1.000e-18', '2.000e-18'],
  114. [1.000000001e-18, 1.99999999e-18, '1.000e-18', '2.000e-18'],
  115. [0.000000000000001, 0.0000000000002, '1.0e-15', '2.000e-13']];
  116. testVector.forEach(function (t) {
  117. plot = $.plot("#placeholder", [sampledata], {
  118. xaxes: [{
  119. showTickLabels : 'endpoints',
  120. min : t[0],
  121. max : t[1],
  122. autoScale : 'none'
  123. }]
  124. });
  125. var axis = plot.getXAxes()[0];
  126. expect(axis.ticks[0].label).toEqual(t[2]);
  127. expect(axis.ticks[axis.ticks.length - 1].label).toEqual(t[3]);
  128. });
  129. });
  130. });
  131. });