jquery.flot.time.Test.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* global describe, it, beforeEach, afterEach, expect, setFixtures */
  2. /* jshint browser: true*/
  3. describe('A Flot chart with absolute time axes', function () {
  4. 'use strict';
  5. var plot;
  6. var placeholder;
  7. beforeEach(function () {
  8. var fixture = setFixtures('<div id="demo-container" style="width: 800px;height: 600px">').find('#demo-container').get(0);
  9. placeholder = $('<div id="placeholder" style="width: 100%;height: 100%">');
  10. placeholder.appendTo(fixture);
  11. });
  12. afterEach(function () {
  13. if (plot) {
  14. plot.shutdown();
  15. }
  16. });
  17. var firstAndLast = function (arr) {
  18. return [arr[0], arr[arr.length - 1]];
  19. };
  20. var createPlotWithAbsoluteTimeAxis = function (placeholder, data, formatString, base, minTickSize) {
  21. return $.plot(placeholder, data, {
  22. xaxis: {
  23. mode: 'time',
  24. timeformat: formatString,
  25. timeBase: base,
  26. minTickSize: minTickSize,
  27. showTickLabels: 'all'
  28. },
  29. yaxis: {}
  30. });
  31. };
  32. var validateTickLabelFormat = function (ticks, regexFormat) {
  33. var isFormatCorrect = true;
  34. ticks.forEach(function (t) {
  35. if (!t.label.match(regexFormat)) {
  36. isFormatCorrect = false;
  37. }
  38. });
  39. return isFormatCorrect;
  40. };
  41. it('shows time ticks', function () {
  42. plot = createPlotWithAbsoluteTimeAxis(placeholder, [[[0, 1], [1000, 2]]], '%Y/%m/%d %M:%S', 'milliseconds');
  43. expect(firstAndLast(plot.getAxes().xaxis.ticks)).toEqual([
  44. {v: 0, label: '1970/01/01 00:00'},
  45. {v: 1000, label: '1970/01/01 00:01'}
  46. ]);
  47. });
  48. describe('date generator', function () {
  49. it('clamps values greater than Date() range to the limit of Date()', function () {
  50. var dateGenerator = $.plot.dateGenerator;
  51. expect(dateGenerator(8640000000000001, {}).date).toEqual(new Date(8640000000000000));
  52. expect(dateGenerator(8640000000000002, {}).date).toEqual(new Date(8640000000000000));
  53. expect(dateGenerator(-8640000000000001, {}).date).toEqual(new Date(-8640000000000000));
  54. expect(dateGenerator(-9640000000000000, {}).date).toEqual(new Date(-8640000000000000));
  55. });
  56. });
  57. describe('timebase in microseconds', function () {
  58. it('supports timestamps given in microseconds', function () {
  59. plot = createPlotWithAbsoluteTimeAxis(placeholder, [[[0, 1], [1000000, 2]]], '%Y/%m/%d %M:%S', 'microseconds');
  60. expect(firstAndLast(plot.getAxes().xaxis.ticks)).toEqual([
  61. {v: 0, label: '1970/01/01 00:00'},
  62. {v: 1000000, label: '1970/01/01 00:01'}
  63. ]);
  64. });
  65. });
  66. describe('sub-second ticks', function () {
  67. it('formats ticks to microsecond accuracy', function () {
  68. plot = createPlotWithAbsoluteTimeAxis(placeholder, [[[0, 1], [1000125, 2]]], '%H:%M:%S.%s', 'microseconds');
  69. expect(validateTickLabelFormat(plot.getAxes().xaxis.ticks, /00:00:0\d\.\d{6}/)).toEqual(true);
  70. expect(firstAndLast(plot.getAxes().xaxis.ticks)).toEqual([
  71. {v: 0, label: '00:00:00.000000'},
  72. {v: 1000125, label: '00:00:01.000125'}
  73. ]);
  74. });
  75. it('formats ticks to millisecond (3 decimals) accuracy', function () {
  76. plot = createPlotWithAbsoluteTimeAxis(placeholder, [[[0, 1], [1002000, 2]]], '%H:%M:%S.%3s', 'microseconds');
  77. expect(validateTickLabelFormat(plot.getAxes().xaxis.ticks, /00:00:0\d\.\d{3}/)).toEqual(true);
  78. expect(firstAndLast(plot.getAxes().xaxis.ticks)).toEqual([
  79. {v: 0, label: '00:00:00.000'},
  80. {v: 1002000, label: '00:00:01.002'}
  81. ]);
  82. });
  83. it('creates ticks automatically to microsecond accuracy', function () {
  84. plot = createPlotWithAbsoluteTimeAxis(placeholder, [[[0, 1], [10, 2]]], null, 'microseconds');
  85. expect(validateTickLabelFormat(plot.getAxes().xaxis.ticks, /00\.\d{6}/)).toEqual(true);
  86. expect(firstAndLast(plot.getAxes().xaxis.ticks)).toEqual([
  87. {v: 0, label: '00.000000'},
  88. {v: 10, label: '00.000010'}
  89. ]);
  90. });
  91. it('creates quarter-second ticks', function () {
  92. plot = createPlotWithAbsoluteTimeAxis(placeholder, [[[0, 1], [1, 2]]], null, 'seconds', [250, 'millisecond']);
  93. expect(validateTickLabelFormat(plot.getAxes().xaxis.ticks, /0\d\.(00|25|50|75)/)).toEqual(true);
  94. expect(firstAndLast(plot.getAxes().xaxis.ticks)).toEqual([
  95. {v: 0, label: '00.00'},
  96. {v: 1, label: '01.00'}
  97. ])
  98. });
  99. it('creates quarter-millisecond ticks', function () {
  100. plot = createPlotWithAbsoluteTimeAxis(placeholder, [[[0, 1], [1, 2]]], null, 'milliseconds', [250, 'microsecond']);
  101. expect(validateTickLabelFormat(plot.getAxes().xaxis.ticks, /00\.00\d(00|25|50|75)/)).toEqual(true);
  102. expect(firstAndLast(plot.getAxes().xaxis.ticks)).toEqual([
  103. {v: 0, label: '00.00000'},
  104. {v: 1, label: '00.00100'}
  105. ])
  106. });
  107. });
  108. });