Interval.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. var numberUtil = require("../util/number");
  20. var formatUtil = require("../util/format");
  21. var Scale = require("./Scale");
  22. var helper = require("./helper");
  23. /*
  24. * Licensed to the Apache Software Foundation (ASF) under one
  25. * or more contributor license agreements. See the NOTICE file
  26. * distributed with this work for additional information
  27. * regarding copyright ownership. The ASF licenses this file
  28. * to you under the Apache License, Version 2.0 (the
  29. * "License"); you may not use this file except in compliance
  30. * with the License. You may obtain a copy of the License at
  31. *
  32. * http://www.apache.org/licenses/LICENSE-2.0
  33. *
  34. * Unless required by applicable law or agreed to in writing,
  35. * software distributed under the License is distributed on an
  36. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  37. * KIND, either express or implied. See the License for the
  38. * specific language governing permissions and limitations
  39. * under the License.
  40. */
  41. /**
  42. * Interval scale
  43. * @module echarts/scale/Interval
  44. */
  45. var roundNumber = numberUtil.round;
  46. /**
  47. * @alias module:echarts/coord/scale/Interval
  48. * @constructor
  49. */
  50. var IntervalScale = Scale.extend({
  51. type: 'interval',
  52. _interval: 0,
  53. _intervalPrecision: 2,
  54. setExtent: function (start, end) {
  55. var thisExtent = this._extent; //start,end may be a Number like '25',so...
  56. if (!isNaN(start)) {
  57. thisExtent[0] = parseFloat(start);
  58. }
  59. if (!isNaN(end)) {
  60. thisExtent[1] = parseFloat(end);
  61. }
  62. },
  63. unionExtent: function (other) {
  64. var extent = this._extent;
  65. other[0] < extent[0] && (extent[0] = other[0]);
  66. other[1] > extent[1] && (extent[1] = other[1]); // unionExtent may called by it's sub classes
  67. IntervalScale.prototype.setExtent.call(this, extent[0], extent[1]);
  68. },
  69. /**
  70. * Get interval
  71. */
  72. getInterval: function () {
  73. return this._interval;
  74. },
  75. /**
  76. * Set interval
  77. */
  78. setInterval: function (interval) {
  79. this._interval = interval; // Dropped auto calculated niceExtent and use user setted extent
  80. // We assume user wan't to set both interval, min, max to get a better result
  81. this._niceExtent = this._extent.slice();
  82. this._intervalPrecision = helper.getIntervalPrecision(interval);
  83. },
  84. /**
  85. * @param {boolean} [expandToNicedExtent=false] If expand the ticks to niced extent.
  86. * @return {Array.<number>}
  87. */
  88. getTicks: function (expandToNicedExtent) {
  89. var interval = this._interval;
  90. var extent = this._extent;
  91. var niceTickExtent = this._niceExtent;
  92. var intervalPrecision = this._intervalPrecision;
  93. var ticks = []; // If interval is 0, return [];
  94. if (!interval) {
  95. return ticks;
  96. } // Consider this case: using dataZoom toolbox, zoom and zoom.
  97. var safeLimit = 10000;
  98. if (extent[0] < niceTickExtent[0]) {
  99. if (expandToNicedExtent) {
  100. ticks.push(roundNumber(niceTickExtent[0] - interval, intervalPrecision));
  101. } else {
  102. ticks.push(extent[0]);
  103. }
  104. }
  105. var tick = niceTickExtent[0];
  106. while (tick <= niceTickExtent[1]) {
  107. ticks.push(tick); // Avoid rounding error
  108. tick = roundNumber(tick + interval, intervalPrecision);
  109. if (tick === ticks[ticks.length - 1]) {
  110. // Consider out of safe float point, e.g.,
  111. // -3711126.9907707 + 2e-10 === -3711126.9907707
  112. break;
  113. }
  114. if (ticks.length > safeLimit) {
  115. return [];
  116. }
  117. } // Consider this case: the last item of ticks is smaller
  118. // than niceTickExtent[1] and niceTickExtent[1] === extent[1].
  119. var lastNiceTick = ticks.length ? ticks[ticks.length - 1] : niceTickExtent[1];
  120. if (extent[1] > lastNiceTick) {
  121. if (expandToNicedExtent) {
  122. ticks.push(roundNumber(lastNiceTick + interval, intervalPrecision));
  123. } else {
  124. ticks.push(extent[1]);
  125. }
  126. }
  127. return ticks;
  128. },
  129. /**
  130. * @param {number} [splitNumber=5]
  131. * @return {Array.<Array.<number>>}
  132. */
  133. getMinorTicks: function (splitNumber) {
  134. var ticks = this.getTicks(true);
  135. var minorTicks = [];
  136. var extent = this.getExtent();
  137. for (var i = 1; i < ticks.length; i++) {
  138. var nextTick = ticks[i];
  139. var prevTick = ticks[i - 1];
  140. var count = 0;
  141. var minorTicksGroup = [];
  142. var interval = nextTick - prevTick;
  143. var minorInterval = interval / splitNumber;
  144. while (count < splitNumber - 1) {
  145. var minorTick = numberUtil.round(prevTick + (count + 1) * minorInterval); // For the first and last interval. The count may be less than splitNumber.
  146. if (minorTick > extent[0] && minorTick < extent[1]) {
  147. minorTicksGroup.push(minorTick);
  148. }
  149. count++;
  150. }
  151. minorTicks.push(minorTicksGroup);
  152. }
  153. return minorTicks;
  154. },
  155. /**
  156. * @param {number} data
  157. * @param {Object} [opt]
  158. * @param {number|string} [opt.precision] If 'auto', use nice presision.
  159. * @param {boolean} [opt.pad] returns 1.50 but not 1.5 if precision is 2.
  160. * @return {string}
  161. */
  162. getLabel: function (data, opt) {
  163. if (data == null) {
  164. return '';
  165. }
  166. var precision = opt && opt.precision;
  167. if (precision == null) {
  168. precision = numberUtil.getPrecisionSafe(data) || 0;
  169. } else if (precision === 'auto') {
  170. // Should be more precise then tick.
  171. precision = this._intervalPrecision;
  172. } // (1) If `precision` is set, 12.005 should be display as '12.00500'.
  173. // (2) Use roundNumber (toFixed) to avoid scientific notation like '3.5e-7'.
  174. data = roundNumber(data, precision, true);
  175. return formatUtil.addCommas(data);
  176. },
  177. /**
  178. * Update interval and extent of intervals for nice ticks
  179. *
  180. * @param {number} [splitNumber = 5] Desired number of ticks
  181. * @param {number} [minInterval]
  182. * @param {number} [maxInterval]
  183. */
  184. niceTicks: function (splitNumber, minInterval, maxInterval) {
  185. splitNumber = splitNumber || 5;
  186. var extent = this._extent;
  187. var span = extent[1] - extent[0];
  188. if (!isFinite(span)) {
  189. return;
  190. } // User may set axis min 0 and data are all negative
  191. // FIXME If it needs to reverse ?
  192. if (span < 0) {
  193. span = -span;
  194. extent.reverse();
  195. }
  196. var result = helper.intervalScaleNiceTicks(extent, splitNumber, minInterval, maxInterval);
  197. this._intervalPrecision = result.intervalPrecision;
  198. this._interval = result.interval;
  199. this._niceExtent = result.niceTickExtent;
  200. },
  201. /**
  202. * Nice extent.
  203. * @param {Object} opt
  204. * @param {number} [opt.splitNumber = 5] Given approx tick number
  205. * @param {boolean} [opt.fixMin=false]
  206. * @param {boolean} [opt.fixMax=false]
  207. * @param {boolean} [opt.minInterval]
  208. * @param {boolean} [opt.maxInterval]
  209. */
  210. niceExtent: function (opt) {
  211. var extent = this._extent; // If extent start and end are same, expand them
  212. if (extent[0] === extent[1]) {
  213. if (extent[0] !== 0) {
  214. // Expand extent
  215. var expandSize = extent[0]; // In the fowllowing case
  216. // Axis has been fixed max 100
  217. // Plus data are all 100 and axis extent are [100, 100].
  218. // Extend to the both side will cause expanded max is larger than fixed max.
  219. // So only expand to the smaller side.
  220. if (!opt.fixMax) {
  221. extent[1] += expandSize / 2;
  222. extent[0] -= expandSize / 2;
  223. } else {
  224. extent[0] -= expandSize / 2;
  225. }
  226. } else {
  227. extent[1] = 1;
  228. }
  229. }
  230. var span = extent[1] - extent[0]; // If there are no data and extent are [Infinity, -Infinity]
  231. if (!isFinite(span)) {
  232. extent[0] = 0;
  233. extent[1] = 1;
  234. }
  235. this.niceTicks(opt.splitNumber, opt.minInterval, opt.maxInterval); // var extent = this._extent;
  236. var interval = this._interval;
  237. if (!opt.fixMin) {
  238. extent[0] = roundNumber(Math.floor(extent[0] / interval) * interval);
  239. }
  240. if (!opt.fixMax) {
  241. extent[1] = roundNumber(Math.ceil(extent[1] / interval) * interval);
  242. }
  243. }
  244. });
  245. /**
  246. * @return {module:echarts/scale/Time}
  247. */
  248. IntervalScale.create = function () {
  249. return new IntervalScale();
  250. };
  251. var _default = IntervalScale;
  252. module.exports = _default;