jquery.flot.logaxis.Test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe("unit tests for the log scale functions", function() {
  4. var placeholder;
  5. beforeEach(function() {
  6. placeholder = setFixtures('<div id="test-container" style="width: 800px;height: 400px">')
  7. .find('#test-container');
  8. });
  9. it('should use linear scale for low dynamic range intervals', function() {
  10. var plot = $.plot(placeholder, [], {
  11. xaxes: [{
  12. min: 10,
  13. max: 11,
  14. }]
  15. }),
  16. axis, ticks;
  17. axis = plot.getAxes().xaxis;
  18. ticks = $.plot.logTicksGenerator(plot, axis, 10);
  19. expect(ticks).toEqual([10, 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7, 10.8, 10.9, 11]);
  20. });
  21. xit('should use mixed scale for medium dynamic range intervals', function() {
  22. var plot = $.plot(placeholder, [], {
  23. xaxes: [{
  24. min: 0.2,
  25. max: 8,
  26. }]
  27. }),
  28. axis, ticks,
  29. outputArray = [0.2, 0.4, 0.6, 1, 2, 3, 5, 8 ];
  30. axis = plot.getAxes().xaxis;
  31. ticks = $.plot.logTicksGenerator(plot,axis, 10);
  32. for(i = 0; i < ticks.length; i++) {
  33. expect(ticks[i]).toBeCloseTo(outputArray[i]);
  34. }
  35. });
  36. it('should use log scales for high dynamic range intervals', function() {
  37. var plot = $.plot(placeholder, [], {
  38. xaxes: [{
  39. min: 0.0001,
  40. max: 10000,
  41. }]
  42. }),
  43. axis, ticks,
  44. axis = plot.getAxes().xaxis;
  45. ticks = $.plot.logTicksGenerator(plot,axis, 10);
  46. expect(ticks).toEqual([0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000]);
  47. });
  48. it('should format numbers according to their natural precision', function() {
  49. var plot = $.plot(placeholder, [], {
  50. xaxes: [{mode: 'log'}]
  51. }),
  52. xaxis = plot.getXAxes()[0],
  53. logFormatter = xaxis.tickFormatter,
  54. testVector = [
  55. [1.7000000000000002, '1.7'],
  56. [17.000000000000002, '17'],
  57. [172, '172'],
  58. [1.000, '1'],
  59. [0.0004, '0.0004'],
  60. [0.00004, '4e-5'],
  61. [3.1623E-21, '3e-21']
  62. ];
  63. testVector.forEach(function (t) {
  64. var inputValue = t[0],
  65. expectedValue = t[1];
  66. expect(logFormatter(inputValue, xaxis)).toBe(expectedValue);
  67. });
  68. });
  69. it('should custom the specified precision of endpoints', function(){
  70. var plot = $.plot(placeholder, [], {
  71. xaxes: [{mode: 'log'}]
  72. }),
  73. xaxis = plot.getXAxes()[0],
  74. logFormatter = xaxis.tickFormatter,
  75. testVector = [
  76. [1.7000000000000002, '1.700', 3],
  77. [1.7000000000000002, '1.70', 2],
  78. [17.000000000000002, '17.000', 3],
  79. [172, '172.0', 1],
  80. [1.000, '1.000', 3],
  81. [1, '1.00', 2],
  82. [0.00004, '4.0e-5', 3],
  83. [4.13567003E-8, '4.1e-8', 9],
  84. [413.567003E-8, '4.136e-6', 9],
  85. [3.1623E-21, '3e-21', 21],
  86. [4.13567003E+8, '4e8', -9],
  87. [413.567003E+8, '4.1e10', -9],
  88. [3.1623E+21, '3.2e21', -20],
  89. [0, '0', -1]
  90. ];
  91. testVector.forEach(function (t) {
  92. var inputValue = t[0],
  93. expectedValue = t[1],
  94. precision = t[2];
  95. expect(logFormatter(inputValue, xaxis, precision)).toBe(expectedValue);
  96. });
  97. });
  98. it('should handle intervals which starts close to 0', function() {
  99. var testVector = [
  100. [0, 50, [0.1, 100]],
  101. [1E-40, 1.01, [1e-35, 10]],
  102. [1E-40, 1E+40, [1e-39, 1e40]],
  103. [Number.MIN_VALUE, 1e-20, [10e-273, 1e-20]]
  104. ];
  105. testVector.forEach(function (t) {
  106. var min = t[0],
  107. max = t[1],
  108. expectedTicks = t[2],
  109. plot = $.plot(placeholder, [], {
  110. xaxes: [{
  111. min: min,
  112. max: max,
  113. }]
  114. }),
  115. axis, ticks;
  116. axis = plot.getAxes().xaxis;
  117. ticks = $.plot.logTicksGenerator(plot, axis);
  118. expect(ticks[0]).toBeCloseTo(expectedTicks[0], 10);
  119. expect(ticks[ticks.length - 1]).toBeCloseTo(expectedTicks[1], 10);
  120. });
  121. });
  122. it('should set axis.min to be less than axis.max', function() {
  123. var plot = $.plot(placeholder, [[0, 1, 2, 3]], {
  124. xaxes: [{
  125. mode: 'log',
  126. offset : { below: -1, above: -1 },
  127. max: 1
  128. }]
  129. }),
  130. axis, ticks,
  131. axis = plot.getAxes().xaxis;
  132. expect(axis.min).toBeLessThan(axis.max);
  133. });
  134. it('should set axis.max to default max if less then axis.min', function() {
  135. var plot = $.plot(placeholder, [[0, 1, 2, 3]], {
  136. xaxes: [{
  137. mode: 'log',
  138. offset : { below: -5, above: -5 },
  139. max: 1
  140. }]
  141. }),
  142. axis, ticks,
  143. axis = plot.getAxes().xaxis;
  144. expect(axis.min).toBe(0.1);
  145. expect(axis.max).toBe(1);
  146. });
  147. it('should set min of axis with no data associated with it to be greater than 0', function() {
  148. var plot = $.plot(placeholder, [[0, 1, 2, 3]], {
  149. xaxes: [
  150. {
  151. mode: 'log',
  152. min: 0,
  153. max: 10
  154. },
  155. {
  156. mode: 'log',
  157. autoScale: 'loose',
  158. min: 0,
  159. max: 10,
  160. show: true
  161. }]
  162. }),
  163. axis,
  164. axis = plot.getXAxes()[1];
  165. expect(axis.min).toBeGreaterThan(0);
  166. });
  167. });
  168. describe("integration tests for log scale functions", function() {
  169. var placeholder;
  170. var compareNumbers = function(a, b) {
  171. return a - b;
  172. }
  173. var queryPlotForYTicks = function() {
  174. var actualTicks = [];
  175. var yAxisDivs = $('.yAxis');
  176. expect(yAxisDivs.length).toBe(1);
  177. var childDivs = yAxisDivs.find('.tickLabel');
  178. childDivs.each(function(i, e) {
  179. actualTicks.push(e.textContent);
  180. });
  181. return actualTicks.sort(compareNumbers);
  182. };
  183. beforeEach(function() {
  184. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  185. .find('#test-container');
  186. });
  187. it('should use linear scale for low dynamic range intervals', function() {
  188. var lineardata1 = [
  189. [0, 1],
  190. [1, 1.1],
  191. [2, 1.2],
  192. [3, 1.3],
  193. [4, 1.4],
  194. [5, 1.5],
  195. [6, 1.6],
  196. [7, 1.7],
  197. [8, 1.8],
  198. [9, 1.9],
  199. [10, 2]
  200. ];
  201. $.plot(placeholder, [lineardata1], {
  202. yaxis: {
  203. mode: 'log',
  204. autoScale: 'exact'
  205. }
  206. });
  207. expect(queryPlotForYTicks()).toEqual(['1', '1.1', '1.2', '1.3', '1.4', '1.5', '1.6', '1.7', '1.8', '1.9', '2']);
  208. });
  209. it('should use log scales for high dynamic range intervals', function() {
  210. var logdata1 = [
  211. [0, 0.0001],
  212. [1, 0.001],
  213. [2, 0.01],
  214. [3, 0.1],
  215. [4, 1],
  216. [5, 10],
  217. [6, 100],
  218. [7, 1000],
  219. [8, 10000]
  220. ];
  221. $.plot(placeholder, [logdata1], {
  222. yaxis: {
  223. mode: 'log',
  224. autoScale: 'exact'
  225. }
  226. });
  227. expect(queryPlotForYTicks()).toEqual(['0.0001', '0.001', '0.01', '0.1', '1', '10', '100', '1000', '10000']);
  228. });
  229. it('should allow a user specified tick formatter', function() {
  230. var logdata1 = [
  231. [0, 0.0001],
  232. [1, 0.001],
  233. [2, 0.01],
  234. [3, 0.1],
  235. [4, 1],
  236. [5, 10],
  237. [6, 100],
  238. [7, 1000],
  239. [8, 10000]
  240. ];
  241. $.plot(placeholder, [logdata1], {
  242. yaxis: {
  243. mode: 'log',
  244. tickFormatter: function () {
  245. return 'log tick';
  246. },
  247. autoScale: 'exact'
  248. }
  249. });
  250. expect(queryPlotForYTicks()).toEqual(['log tick', 'log tick', 'log tick', 'log tick', 'log tick', 'log tick', 'log tick', 'log tick', 'log tick']);
  251. });
  252. it('should set the minimum of the logaxis to minimum datapoint between 0 and 0.1', function() {
  253. var logdata1 = [
  254. [0, 0],
  255. [1, 0.0001],
  256. [2, 0.001],
  257. [3, 0.01],
  258. [4, 0.1],
  259. [5, 1],
  260. [6, 10],
  261. [7, 100]
  262. ];
  263. var plot = $.plot(placeholder, [logdata1], {
  264. xaxis: {
  265. mode: 'log',
  266. autoScale: 'exact'
  267. },
  268. yaxis: {
  269. mode: 'log',
  270. autoScale: 'exact'
  271. }
  272. }),
  273. axes = plot.getAxes();
  274. expect(axes.xaxis.min).toBe(0.1);
  275. expect(axes.yaxis.min).toBe(0.0001);
  276. });
  277. it('should multiply the possible ticks with 5 if the original interval is too little', function() {
  278. var logdata = [
  279. [[0, 1E40], [1, 4E40], [2, 8E40], [3, 1E41], [4, 2E41], [5, 4E41]],
  280. [[0, 1000], [1, 1010], [2, 1E4], [3, 1E5]],
  281. [[0, 1], [1, 3], [2, 10], [3, 30], [4, 40], [5, 50]]
  282. ],
  283. expectedTicks = [
  284. ['1e40', '5e40', '1e41'],
  285. ['1000', '5000', '10000', '50000', '100000'],
  286. ['1', '5', '10', '50']
  287. ],
  288. plot, i;
  289. for (i = 0; i < logdata.length; i++) {
  290. plot = $.plot(placeholder, [logdata[i]], {
  291. yaxis: {
  292. mode: 'log',
  293. autoScale: 'exact'
  294. }
  295. }),
  296. ticks = queryPlotForYTicks();
  297. expect(queryPlotForYTicks()).toEqual(expectedTicks[i]);
  298. }
  299. });
  300. it('should use only power of ten for large intervals', function() {
  301. var logdata = [
  302. [[0, 1], [1, 4E20], [2, 8E30], [3, 4E41]],
  303. [[0, 1000], [1, 1E5], [2, 1E7], [3, 1E10]],
  304. [[0, 1e-12], [1, 1e-5], [2, 10], [3, 30], [4, 40], [5, 500]]
  305. ],
  306. expectedTicks = [
  307. ['100', '1000000', '1e10', '1e14', '10e17', '1e22', '1e26', '1e30', '1e34', '1e38'],
  308. ['1000', '10000', '100000', '1000000', '10000000', '1e8', '10e8', '1e10'],
  309. ['1e-11', '1e-9', '1e-7', '1e-5', '0.001', '0.1', '10']
  310. ],
  311. plot, i;
  312. for (i = 0; i < logdata.length; i++) {
  313. plot = $.plot(placeholder, [logdata[i]], {
  314. yaxis: {
  315. mode: 'log',
  316. autoScale: 'exact'
  317. }
  318. }),
  319. ticks = queryPlotForYTicks();
  320. expect(queryPlotForYTicks()).toEqual(expectedTicks[i]);
  321. }
  322. });
  323. });