Interval.js 9.1 KB

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