Interval.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 { round, mathRound, mathMin, getPrecision } from '../util/number.js';
  42. import { addCommas } from '../util/format.js';
  43. import Scale from './Scale.js';
  44. import { getIntervalPrecision } from './helper.js';
  45. import { getBreaksUnsafe, getScaleBreakHelper, hasBreaks, simplyParseBreakOption } from './break.js';
  46. import { assert, clone } from 'zrender/lib/core/util.js';
  47. import { getMinorTicks } from './minorTicks.js';
  48. import { getScaleExtentForTickUnsafe, initBreakOrLinearMapper } from './scaleMapper.js';
  49. import { warn } from '../util/log.js';
  50. var IntervalScale = /** @class */function (_super) {
  51. __extends(IntervalScale, _super);
  52. function IntervalScale(setting) {
  53. var _this = _super.call(this) || this;
  54. _this.type = 'interval';
  55. _this.parse = IntervalScale.parse;
  56. setting = setting || {};
  57. var breakParsed = simplyParseBreakOption(_this, setting);
  58. var res = initBreakOrLinearMapper(_this, breakParsed, null);
  59. // @ts-ignore
  60. _this.brk = res.brk;
  61. _this._cfg = {
  62. interval: 0,
  63. intervalPrecision: 2,
  64. intervalCount: undefined,
  65. niceExtent: undefined
  66. };
  67. return _this;
  68. }
  69. IntervalScale.parse = function (val) {
  70. // `Scale#parse` (and its overrids) are typically applied at the axis values input
  71. // in echarts option. e.g., `axis.min/max`, `dataZoom.min/max`, etc.
  72. // but `series.data` is not included, which uses `dataValueHelper.ts`#`parseDataValue`.
  73. // `Scale#parse` originally introduced in fb8c813215098b9d2458966229bb95c510883d5e
  74. // at 2016 for dataZoom start/end settings (See `parseAxisModelMinMax`).
  75. //
  76. // Historically `scale/Interval.ts` returns the input value directly. But numeric
  77. // values (such as a number-like string '123') effectively passed through here and
  78. // were involved in calculations, which was error-prone and inconsistent with the
  79. // declared TS return type. Previously such issues are fixed separately in different
  80. // places case by case (such as #2475).
  81. //
  82. // Now, we perform actual parse to ensure its `number` type here. The parsing rule
  83. // follows the series data parsing rule (`dataValueHelper.ts`#`parseDataValue`)
  84. // and maintains compatibility as much as possible (thus a more strict parsing
  85. // `number.ts`#`numericToNumber` is not used here.)
  86. //
  87. // FIXME: `ScaleDataValue` also need to be modified to include numeric string type,
  88. // since it effectively does.
  89. return val == null || val === '' ? NaN
  90. // If string (like '-'), using '+' parse to NaN
  91. // If object, also parse to NaN
  92. : Number(val);
  93. };
  94. IntervalScale.prototype.getConfig = function () {
  95. return clone(this._cfg);
  96. };
  97. IntervalScale.prototype.setConfig = function (cfg) {
  98. var extent = getScaleExtentForTickUnsafe(this);
  99. if (process.env.NODE_ENV !== 'production') {
  100. assert(cfg.interval != null);
  101. if (cfg.intervalCount != null) {
  102. assert(cfg.intervalCount >= -1 && cfg.intervalPrecision != null
  103. // Do not support intervalCount on axis break currently.
  104. && !hasBreaks(this));
  105. }
  106. if (cfg.niceExtent != null) {
  107. assert(isFinite(cfg.niceExtent[0]) && isFinite(cfg.niceExtent[1]));
  108. assert(extent[0] <= cfg.niceExtent[0] && cfg.niceExtent[1] <= extent[1]);
  109. assert(round(cfg.niceExtent[0] - cfg.niceExtent[1], getPrecision(cfg.interval)) <= cfg.interval);
  110. }
  111. }
  112. // Reset all.
  113. this._cfg = cfg = clone(cfg);
  114. if (cfg.niceExtent == null) {
  115. // Dropped the auto calculated niceExtent and use user-set extent.
  116. // We assume users want to set both interval and extent to get a better result.
  117. cfg.niceExtent = extent.slice();
  118. }
  119. if (cfg.intervalPrecision == null) {
  120. cfg.intervalPrecision = getIntervalPrecision(cfg.interval);
  121. }
  122. };
  123. /**
  124. * In ascending order.
  125. */
  126. IntervalScale.prototype.getTicks = function (opt) {
  127. opt = opt || {};
  128. var cfg = this._cfg;
  129. var interval = cfg.interval;
  130. var extent = getScaleExtentForTickUnsafe(this);
  131. var niceExtent = cfg.niceExtent;
  132. var intervalPrecision = cfg.intervalPrecision;
  133. var scaleBreakHelper = getScaleBreakHelper();
  134. var brk = this.brk;
  135. var brkAvailable = scaleBreakHelper && brk;
  136. var ticks = [];
  137. // If interval is 0, return [];
  138. if (!interval) {
  139. return ticks;
  140. }
  141. if (opt.breakTicks === 'only_break' && brkAvailable) {
  142. scaleBreakHelper.addBreaksToTicks(ticks, brk.breaks, extent);
  143. return ticks;
  144. }
  145. if (process.env.NODE_ENV !== 'production') {
  146. assert(niceExtent != null);
  147. }
  148. // [CAVEAT]: If changing this logic, must sync it to `axisAlignTicks.ts`.
  149. // A fail-safe is required since `interval` can be user specified, or for the case
  150. // that using dataZoom toolbox and zoom repeatedly.
  151. var safeLimit = 3000;
  152. if (extent[0] < niceExtent[0]) {
  153. ticks.push({
  154. value: opt.expandToNicedExtent ? round(niceExtent[0] - interval, intervalPrecision) : extent[0]
  155. });
  156. }
  157. var estimateNiceMultiple = function (tickVal, targetTick) {
  158. return mathRound((targetTick - tickVal) / interval);
  159. };
  160. var intervalCount = cfg.intervalCount;
  161. for (var tick = niceExtent[0], niceTickIdx = 0;; niceTickIdx++) {
  162. // Consider case `_extent: [5.2, 5.8], _niceExtent: [6, 5], interval: 1`,
  163. // `_intervalCount` makes sense iff `-1`.
  164. // Consider case `_extent: [5, 5.8], _niceExtent: [5, 5], interval: 1`,
  165. // `_intervalCount` makes sense iff `0`.
  166. if (intervalCount == null) {
  167. if (tick > niceExtent[1] || !isFinite(tick) || !isFinite(niceExtent[1])) {
  168. break;
  169. }
  170. } else {
  171. if (niceTickIdx > intervalCount) {
  172. // nice ticks number should be `intervalCount + 1`
  173. break;
  174. }
  175. // Consider cumulative error, especially caused by rounding, the last nice
  176. // `tick` may be less than or greater than `niceExtent[1]` slightly.
  177. tick = mathMin(tick, niceExtent[1]);
  178. if (niceTickIdx === intervalCount) {
  179. tick = niceExtent[1];
  180. }
  181. }
  182. ticks.push({
  183. value: tick
  184. });
  185. // Avoid rounding error
  186. tick = round(tick + interval, intervalPrecision);
  187. if (brk) {
  188. var moreMultiple = brk.calcNiceTickMultiple(tick, estimateNiceMultiple);
  189. if (moreMultiple >= 0) {
  190. tick = round(tick + moreMultiple * interval, intervalPrecision);
  191. }
  192. }
  193. if (ticks.length > 0 && tick === ticks[ticks.length - 1].value) {
  194. // Consider out of safe float point, e.g.,
  195. // -3711126.9907707 + 2e-10 === -3711126.9907707
  196. break;
  197. }
  198. if (ticks.length > safeLimit) {
  199. if (process.env.NODE_ENV !== 'production') {
  200. warn('Exceed safe limit in IntervalScale["getTicks"].');
  201. }
  202. return [];
  203. }
  204. }
  205. // Consider this case: the last item of ticks is smaller
  206. // than niceExtent[1] and niceExtent[1] === extent[1].
  207. var lastNiceTick = ticks.length ? ticks[ticks.length - 1].value : niceExtent[1];
  208. if (extent[1] > lastNiceTick) {
  209. ticks.push({
  210. value: opt.expandToNicedExtent ? round(lastNiceTick + interval, intervalPrecision) : extent[1]
  211. });
  212. }
  213. if (brkAvailable) {
  214. scaleBreakHelper.pruneTicksByBreak(opt.pruneByBreak, ticks, brk.breaks, function (item) {
  215. return item.value;
  216. }, cfg.interval, extent);
  217. }
  218. if (brkAvailable && opt.breakTicks !== 'none') {
  219. scaleBreakHelper.addBreaksToTicks(ticks, brk.breaks, extent);
  220. }
  221. return ticks;
  222. };
  223. IntervalScale.prototype.getMinorTicks = function (splitNumber) {
  224. return getMinorTicks(this, splitNumber, getBreaksUnsafe(this), this._cfg.interval);
  225. };
  226. IntervalScale.prototype.getLabel = function (tick, opt) {
  227. if (tick == null) {
  228. return '';
  229. }
  230. var precision = opt && opt.precision;
  231. if (precision == null) {
  232. precision = getPrecision(tick.value) || 0;
  233. } else if (precision === 'auto') {
  234. // Should be more precise then tick.
  235. precision = this._cfg.intervalPrecision;
  236. }
  237. // (1) If `precision` is set, 12.005 should be display as '12.00500'.
  238. // (2) Use `round` (toFixed) to avoid scientific notation like '3.5e-7'.
  239. var dataNum = round(tick.value, precision, true);
  240. return addCommas(dataNum);
  241. };
  242. IntervalScale.type = 'interval';
  243. return IntervalScale;
  244. }(Scale);
  245. Scale.registerClass(IntervalScale);
  246. export default IntervalScale;