Interval.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. var numberUtil = require("../util/number");
  2. var formatUtil = require("../util/format");
  3. var Scale = require("./Scale");
  4. var helper = require("./helper");
  5. /**
  6. * Interval scale
  7. * @module echarts/scale/Interval
  8. */
  9. var roundNumber = numberUtil.round;
  10. /**
  11. * @alias module:echarts/coord/scale/Interval
  12. * @constructor
  13. */
  14. var IntervalScale = Scale.extend({
  15. type: 'interval',
  16. _interval: 0,
  17. _intervalPrecision: 2,
  18. setExtent: function (start, end) {
  19. var thisExtent = this._extent; //start,end may be a Number like '25',so...
  20. if (!isNaN(start)) {
  21. thisExtent[0] = parseFloat(start);
  22. }
  23. if (!isNaN(end)) {
  24. thisExtent[1] = parseFloat(end);
  25. }
  26. },
  27. unionExtent: function (other) {
  28. var extent = this._extent;
  29. other[0] < extent[0] && (extent[0] = other[0]);
  30. other[1] > extent[1] && (extent[1] = other[1]); // unionExtent may called by it's sub classes
  31. IntervalScale.prototype.setExtent.call(this, extent[0], extent[1]);
  32. },
  33. /**
  34. * Get interval
  35. */
  36. getInterval: function () {
  37. return this._interval;
  38. },
  39. /**
  40. * Set interval
  41. */
  42. setInterval: function (interval) {
  43. this._interval = interval; // Dropped auto calculated niceExtent and use user setted extent
  44. // We assume user wan't to set both interval, min, max to get a better result
  45. this._niceExtent = this._extent.slice();
  46. this._intervalPrecision = helper.getIntervalPrecision(interval);
  47. },
  48. /**
  49. * @return {Array.<number>}
  50. */
  51. getTicks: function () {
  52. return helper.intervalScaleGetTicks(this._interval, this._extent, this._niceExtent, this._intervalPrecision);
  53. },
  54. /**
  55. * @return {Array.<string>}
  56. */
  57. getTicksLabels: function () {
  58. var labels = [];
  59. var ticks = this.getTicks();
  60. for (var i = 0; i < ticks.length; i++) {
  61. labels.push(this.getLabel(ticks[i]));
  62. }
  63. return labels;
  64. },
  65. /**
  66. * @param {number} data
  67. * @param {Object} [opt]
  68. * @param {number|string} [opt.precision] If 'auto', use nice presision.
  69. * @param {boolean} [opt.pad] returns 1.50 but not 1.5 if precision is 2.
  70. * @return {string}
  71. */
  72. getLabel: function (data, opt) {
  73. if (data == null) {
  74. return '';
  75. }
  76. var precision = opt && opt.precision;
  77. if (precision == null) {
  78. precision = numberUtil.getPrecisionSafe(data) || 0;
  79. } else if (precision === 'auto') {
  80. // Should be more precise then tick.
  81. precision = this._intervalPrecision;
  82. } // (1) If `precision` is set, 12.005 should be display as '12.00500'.
  83. // (2) Use roundNumber (toFixed) to avoid scientific notation like '3.5e-7'.
  84. data = roundNumber(data, precision, true);
  85. return formatUtil.addCommas(data);
  86. },
  87. /**
  88. * Update interval and extent of intervals for nice ticks
  89. *
  90. * @param {number} [splitNumber = 5] Desired number of ticks
  91. * @param {number} [minInterval]
  92. * @param {number} [maxInterval]
  93. */
  94. niceTicks: function (splitNumber, minInterval, maxInterval) {
  95. splitNumber = splitNumber || 5;
  96. var extent = this._extent;
  97. var span = extent[1] - extent[0];
  98. if (!isFinite(span)) {
  99. return;
  100. } // User may set axis min 0 and data are all negative
  101. // FIXME If it needs to reverse ?
  102. if (span < 0) {
  103. span = -span;
  104. extent.reverse();
  105. }
  106. var result = helper.intervalScaleNiceTicks(extent, splitNumber, minInterval, maxInterval);
  107. this._intervalPrecision = result.intervalPrecision;
  108. this._interval = result.interval;
  109. this._niceExtent = result.niceTickExtent;
  110. },
  111. /**
  112. * Nice extent.
  113. * @param {Object} opt
  114. * @param {number} [opt.splitNumber = 5] Given approx tick number
  115. * @param {boolean} [opt.fixMin=false]
  116. * @param {boolean} [opt.fixMax=false]
  117. * @param {boolean} [opt.minInterval]
  118. * @param {boolean} [opt.maxInterval]
  119. */
  120. niceExtent: function (opt) {
  121. var extent = this._extent; // If extent start and end are same, expand them
  122. if (extent[0] === extent[1]) {
  123. if (extent[0] !== 0) {
  124. // Expand extent
  125. var expandSize = extent[0]; // In the fowllowing case
  126. // Axis has been fixed max 100
  127. // Plus data are all 100 and axis extent are [100, 100].
  128. // Extend to the both side will cause expanded max is larger than fixed max.
  129. // So only expand to the smaller side.
  130. if (!opt.fixMax) {
  131. extent[1] += expandSize / 2;
  132. extent[0] -= expandSize / 2;
  133. } else {
  134. extent[0] -= expandSize / 2;
  135. }
  136. } else {
  137. extent[1] = 1;
  138. }
  139. }
  140. var span = extent[1] - extent[0]; // If there are no data and extent are [Infinity, -Infinity]
  141. if (!isFinite(span)) {
  142. extent[0] = 0;
  143. extent[1] = 1;
  144. }
  145. this.niceTicks(opt.splitNumber, opt.minInterval, opt.maxInterval); // var extent = this._extent;
  146. var interval = this._interval;
  147. if (!opt.fixMin) {
  148. extent[0] = roundNumber(Math.floor(extent[0] / interval) * interval);
  149. }
  150. if (!opt.fixMax) {
  151. extent[1] = roundNumber(Math.ceil(extent[1] / interval) * interval);
  152. }
  153. }
  154. });
  155. /**
  156. * @return {module:echarts/scale/Time}
  157. */
  158. IntervalScale.create = function () {
  159. return new IntervalScale();
  160. };
  161. var _default = IntervalScale;
  162. module.exports = _default;