Interval.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 = /** @class */function (_super) {
  47. __extends(IntervalScale, _super);
  48. function IntervalScale() {
  49. var _this = _super !== null && _super.apply(this, arguments) || this;
  50. _this.type = 'interval';
  51. // Step is calculated in adjustExtent.
  52. _this._interval = 0;
  53. _this._intervalPrecision = 2;
  54. return _this;
  55. }
  56. IntervalScale.prototype.parse = function (val) {
  57. return val;
  58. };
  59. IntervalScale.prototype.contain = function (val) {
  60. return helper.contain(val, this._extent);
  61. };
  62. IntervalScale.prototype.normalize = function (val) {
  63. return helper.normalize(val, this._extent);
  64. };
  65. IntervalScale.prototype.scale = function (val) {
  66. return helper.scale(val, this._extent);
  67. };
  68. IntervalScale.prototype.setExtent = function (start, end) {
  69. var thisExtent = this._extent;
  70. // 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]);
  82. // unionExtent may called by it's sub classes
  83. this.setExtent(extent[0], extent[1]);
  84. };
  85. IntervalScale.prototype.getInterval = function () {
  86. return this._interval;
  87. };
  88. IntervalScale.prototype.setInterval = function (interval) {
  89. this._interval = interval;
  90. // Dropped auto calculated niceExtent and use user-set extent.
  91. // We assume user wants to set both interval, min, max to get a better result.
  92. this._niceExtent = this._extent.slice();
  93. this._intervalPrecision = helper.getIntervalPrecision(interval);
  94. };
  95. /**
  96. * @param expandToNicedExtent Whether expand the ticks to niced extent.
  97. */
  98. IntervalScale.prototype.getTicks = function (expandToNicedExtent) {
  99. var interval = this._interval;
  100. var extent = this._extent;
  101. var niceTickExtent = this._niceExtent;
  102. var intervalPrecision = this._intervalPrecision;
  103. var ticks = [];
  104. // If interval is 0, return [];
  105. if (!interval) {
  106. return ticks;
  107. }
  108. // Consider this case: using dataZoom toolbox, zoom and zoom.
  109. var safeLimit = 10000;
  110. if (extent[0] < niceTickExtent[0]) {
  111. if (expandToNicedExtent) {
  112. ticks.push({
  113. value: roundNumber(niceTickExtent[0] - interval, intervalPrecision)
  114. });
  115. } else {
  116. ticks.push({
  117. value: extent[0]
  118. });
  119. }
  120. }
  121. var tick = niceTickExtent[0];
  122. while (tick <= niceTickExtent[1]) {
  123. ticks.push({
  124. value: tick
  125. });
  126. // Avoid rounding error
  127. tick = roundNumber(tick + interval, intervalPrecision);
  128. if (tick === ticks[ticks.length - 1].value) {
  129. // Consider out of safe float point, e.g.,
  130. // -3711126.9907707 + 2e-10 === -3711126.9907707
  131. break;
  132. }
  133. if (ticks.length > safeLimit) {
  134. return [];
  135. }
  136. }
  137. // Consider this case: the last item of ticks is smaller
  138. // than niceTickExtent[1] and niceTickExtent[1] === extent[1].
  139. var lastNiceTick = ticks.length ? ticks[ticks.length - 1].value : niceTickExtent[1];
  140. if (extent[1] > lastNiceTick) {
  141. if (expandToNicedExtent) {
  142. ticks.push({
  143. value: roundNumber(lastNiceTick + interval, intervalPrecision)
  144. });
  145. } else {
  146. ticks.push({
  147. value: extent[1]
  148. });
  149. }
  150. }
  151. return ticks;
  152. };
  153. IntervalScale.prototype.getMinorTicks = function (splitNumber) {
  154. var ticks = this.getTicks(true);
  155. var minorTicks = [];
  156. var extent = this.getExtent();
  157. for (var i = 1; i < ticks.length; i++) {
  158. var nextTick = ticks[i];
  159. var prevTick = ticks[i - 1];
  160. var count = 0;
  161. var minorTicksGroup = [];
  162. var interval = nextTick.value - prevTick.value;
  163. var minorInterval = interval / splitNumber;
  164. while (count < splitNumber - 1) {
  165. var minorTick = roundNumber(prevTick.value + (count + 1) * minorInterval);
  166. // For the first and last interval. The count may be less than splitNumber.
  167. if (minorTick > extent[0] && minorTick < extent[1]) {
  168. minorTicksGroup.push(minorTick);
  169. }
  170. count++;
  171. }
  172. minorTicks.push(minorTicksGroup);
  173. }
  174. return minorTicks;
  175. };
  176. /**
  177. * @param opt.precision If 'auto', use nice presision.
  178. * @param opt.pad returns 1.50 but not 1.5 if precision is 2.
  179. */
  180. IntervalScale.prototype.getLabel = function (data, opt) {
  181. if (data == null) {
  182. return '';
  183. }
  184. var precision = opt && opt.precision;
  185. if (precision == null) {
  186. precision = numberUtil.getPrecision(data.value) || 0;
  187. } else if (precision === 'auto') {
  188. // Should be more precise then tick.
  189. precision = this._intervalPrecision;
  190. }
  191. // (1) If `precision` is set, 12.005 should be display as '12.00500'.
  192. // (2) Use roundNumber (toFixed) to avoid scientific notation like '3.5e-7'.
  193. var dataNum = roundNumber(data.value, precision, true);
  194. return formatUtil.addCommas(dataNum);
  195. };
  196. /**
  197. * @param splitNumber By default `5`.
  198. */
  199. IntervalScale.prototype.calcNiceTicks = function (splitNumber, minInterval, maxInterval) {
  200. splitNumber = splitNumber || 5;
  201. var extent = this._extent;
  202. var span = extent[1] - extent[0];
  203. if (!isFinite(span)) {
  204. return;
  205. }
  206. // User may set axis min 0 and data are all negative
  207. // FIXME If it needs to reverse ?
  208. if (span < 0) {
  209. span = -span;
  210. extent.reverse();
  211. }
  212. var result = helper.intervalScaleNiceTicks(extent, splitNumber, minInterval, maxInterval);
  213. this._intervalPrecision = result.intervalPrecision;
  214. this._interval = result.interval;
  215. this._niceExtent = result.niceTickExtent;
  216. };
  217. IntervalScale.prototype.calcNiceExtent = function (opt) {
  218. var extent = this._extent;
  219. // If extent start and end are same, expand them
  220. if (extent[0] === extent[1]) {
  221. if (extent[0] !== 0) {
  222. // Expand extent
  223. // Note that extents can be both negative. See #13154
  224. var expandSize = Math.abs(extent[0]);
  225. // In the fowllowing case
  226. // Axis has been fixed max 100
  227. // Plus data are all 100 and axis extent are [100, 100].
  228. // Extend to the both side will cause expanded max is larger than fixed max.
  229. // So only expand to the smaller side.
  230. if (!opt.fixMax) {
  231. extent[1] += expandSize / 2;
  232. extent[0] -= expandSize / 2;
  233. } else {
  234. extent[0] -= expandSize / 2;
  235. }
  236. } else {
  237. extent[1] = 1;
  238. }
  239. }
  240. var span = extent[1] - extent[0];
  241. // If there are no data and extent are [Infinity, -Infinity]
  242. if (!isFinite(span)) {
  243. extent[0] = 0;
  244. extent[1] = 1;
  245. }
  246. this.calcNiceTicks(opt.splitNumber, opt.minInterval, opt.maxInterval);
  247. // let extent = this._extent;
  248. var interval = this._interval;
  249. if (!opt.fixMin) {
  250. extent[0] = roundNumber(Math.floor(extent[0] / interval) * interval);
  251. }
  252. if (!opt.fixMax) {
  253. extent[1] = roundNumber(Math.ceil(extent[1] / interval) * interval);
  254. }
  255. };
  256. IntervalScale.prototype.setNiceExtent = function (min, max) {
  257. this._niceExtent = [min, max];
  258. };
  259. IntervalScale.type = 'interval';
  260. return IntervalScale;
  261. }(Scale);
  262. Scale.registerClass(IntervalScale);
  263. export default IntervalScale;