scaleRawExtentInfo.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 { assert, isArray, eqNaN, isFunction } from 'zrender/lib/core/util.js';
  41. import { parsePercent } from 'zrender/lib/contain/text.js';
  42. var ScaleRawExtentInfo = /** @class */function () {
  43. function ScaleRawExtentInfo(scale, model,
  44. // Usually: data extent from all series on this axis.
  45. originalExtent) {
  46. this._prepareParams(scale, model, originalExtent);
  47. }
  48. /**
  49. * Parameters depending on outside (like model, user callback)
  50. * are prepared and fixed here.
  51. */
  52. ScaleRawExtentInfo.prototype._prepareParams = function (scale, model,
  53. // Usually: data extent from all series on this axis.
  54. dataExtent) {
  55. if (dataExtent[1] < dataExtent[0]) {
  56. dataExtent = [NaN, NaN];
  57. }
  58. this._dataMin = dataExtent[0];
  59. this._dataMax = dataExtent[1];
  60. var isOrdinal = this._isOrdinal = scale.type === 'ordinal';
  61. this._needCrossZero = scale.type === 'interval' && model.getNeedCrossZero && model.getNeedCrossZero();
  62. var modelMinRaw = this._modelMinRaw = model.get('min', true);
  63. if (isFunction(modelMinRaw)) {
  64. // This callback always provides users the full data extent (before data is filtered).
  65. this._modelMinNum = parseAxisModelMinMax(scale, modelMinRaw({
  66. min: dataExtent[0],
  67. max: dataExtent[1]
  68. }));
  69. } else if (modelMinRaw !== 'dataMin') {
  70. this._modelMinNum = parseAxisModelMinMax(scale, modelMinRaw);
  71. }
  72. var modelMaxRaw = this._modelMaxRaw = model.get('max', true);
  73. if (isFunction(modelMaxRaw)) {
  74. // This callback always provides users the full data extent (before data is filtered).
  75. this._modelMaxNum = parseAxisModelMinMax(scale, modelMaxRaw({
  76. min: dataExtent[0],
  77. max: dataExtent[1]
  78. }));
  79. } else if (modelMaxRaw !== 'dataMax') {
  80. this._modelMaxNum = parseAxisModelMinMax(scale, modelMaxRaw);
  81. }
  82. if (isOrdinal) {
  83. // FIXME: there is a flaw here: if there is no "block" data processor like `dataZoom`,
  84. // and progressive rendering is using, here the category result might just only contain
  85. // the processed chunk rather than the entire result.
  86. this._axisDataLen = model.getCategories().length;
  87. } else {
  88. var boundaryGap = model.get('boundaryGap');
  89. var boundaryGapArr = isArray(boundaryGap) ? boundaryGap : [boundaryGap || 0, boundaryGap || 0];
  90. if (typeof boundaryGapArr[0] === 'boolean' || typeof boundaryGapArr[1] === 'boolean') {
  91. if (process.env.NODE_ENV !== 'production') {
  92. console.warn('Boolean type for boundaryGap is only ' + 'allowed for ordinal axis. Please use string in ' + 'percentage instead, e.g., "20%". Currently, ' + 'boundaryGap is set to be 0.');
  93. }
  94. this._boundaryGapInner = [0, 0];
  95. } else {
  96. this._boundaryGapInner = [parsePercent(boundaryGapArr[0], 1), parsePercent(boundaryGapArr[1], 1)];
  97. }
  98. }
  99. };
  100. /**
  101. * Calculate extent by prepared parameters.
  102. * This method has no external dependency and can be called duplicatedly,
  103. * getting the same result.
  104. * If parameters changed, should call this method to recalcuate.
  105. */
  106. ScaleRawExtentInfo.prototype.calculate = function () {
  107. // Notice: When min/max is not set (that is, when there are null/undefined,
  108. // which is the most common case), these cases should be ensured:
  109. // (1) For 'ordinal', show all axis.data.
  110. // (2) For others:
  111. // + `boundaryGap` is applied (if min/max set, boundaryGap is
  112. // disabled).
  113. // + If `needCrossZero`, min/max should be zero, otherwise, min/max should
  114. // be the result that originalExtent enlarged by boundaryGap.
  115. // (3) If no data, it should be ensured that `scale.setBlank` is set.
  116. var isOrdinal = this._isOrdinal;
  117. var dataMin = this._dataMin;
  118. var dataMax = this._dataMax;
  119. var axisDataLen = this._axisDataLen;
  120. var boundaryGapInner = this._boundaryGapInner;
  121. var span = !isOrdinal ? dataMax - dataMin || Math.abs(dataMin) : null;
  122. // Currently if a `'value'` axis model min is specified as 'dataMin'/'dataMax',
  123. // `boundaryGap` will not be used. It's the different from specifying as `null`/`undefined`.
  124. var min = this._modelMinRaw === 'dataMin' ? dataMin : this._modelMinNum;
  125. var max = this._modelMaxRaw === 'dataMax' ? dataMax : this._modelMaxNum;
  126. // If `_modelMinNum`/`_modelMaxNum` is `null`/`undefined`, should not be fixed.
  127. var minFixed = min != null;
  128. var maxFixed = max != null;
  129. if (min == null) {
  130. min = isOrdinal ? axisDataLen ? 0 : NaN : dataMin - boundaryGapInner[0] * span;
  131. }
  132. if (max == null) {
  133. max = isOrdinal ? axisDataLen ? axisDataLen - 1 : NaN : dataMax + boundaryGapInner[1] * span;
  134. }
  135. (min == null || !isFinite(min)) && (min = NaN);
  136. (max == null || !isFinite(max)) && (max = NaN);
  137. var isBlank = eqNaN(min) || eqNaN(max) || isOrdinal && !axisDataLen;
  138. // If data extent modified, need to recalculated to ensure cross zero.
  139. if (this._needCrossZero) {
  140. // Axis is over zero and min is not set
  141. if (min > 0 && max > 0 && !minFixed) {
  142. min = 0;
  143. // minFixed = true;
  144. }
  145. // Axis is under zero and max is not set
  146. if (min < 0 && max < 0 && !maxFixed) {
  147. max = 0;
  148. // maxFixed = true;
  149. }
  150. // PENDING:
  151. // When `needCrossZero` and all data is positive/negative, should it be ensured
  152. // that the results processed by boundaryGap are positive/negative?
  153. // If so, here `minFixed`/`maxFixed` need to be set.
  154. }
  155. var determinedMin = this._determinedMin;
  156. var determinedMax = this._determinedMax;
  157. if (determinedMin != null) {
  158. min = determinedMin;
  159. minFixed = true;
  160. }
  161. if (determinedMax != null) {
  162. max = determinedMax;
  163. maxFixed = true;
  164. }
  165. // Ensure min/max be finite number or NaN here. (not to be null/undefined)
  166. // `NaN` means min/max axis is blank.
  167. return {
  168. min: min,
  169. max: max,
  170. minFixed: minFixed,
  171. maxFixed: maxFixed,
  172. isBlank: isBlank
  173. };
  174. };
  175. ScaleRawExtentInfo.prototype.modifyDataMinMax = function (minMaxName, val) {
  176. if (process.env.NODE_ENV !== 'production') {
  177. assert(!this.frozen);
  178. }
  179. this[DATA_MIN_MAX_ATTR[minMaxName]] = val;
  180. };
  181. ScaleRawExtentInfo.prototype.setDeterminedMinMax = function (minMaxName, val) {
  182. var attr = DETERMINED_MIN_MAX_ATTR[minMaxName];
  183. if (process.env.NODE_ENV !== 'production') {
  184. assert(!this.frozen
  185. // Earse them usually means logic flaw.
  186. && this[attr] == null);
  187. }
  188. this[attr] = val;
  189. };
  190. ScaleRawExtentInfo.prototype.freeze = function () {
  191. // @ts-ignore
  192. this.frozen = true;
  193. };
  194. return ScaleRawExtentInfo;
  195. }();
  196. export { ScaleRawExtentInfo };
  197. var DETERMINED_MIN_MAX_ATTR = {
  198. min: '_determinedMin',
  199. max: '_determinedMax'
  200. };
  201. var DATA_MIN_MAX_ATTR = {
  202. min: '_dataMin',
  203. max: '_dataMax'
  204. };
  205. /**
  206. * Get scale min max and related info only depends on model settings.
  207. * This method can be called after coordinate system created.
  208. * For example, in data processing stage.
  209. *
  210. * Scale extent info probably be required multiple times during a workflow.
  211. * For example:
  212. * (1) `dataZoom` depends it to get the axis extent in "100%" state.
  213. * (2) `processor/extentCalculator` depends it to make sure whether axis extent is specified.
  214. * (3) `coordSys.update` use it to finally decide the scale extent.
  215. * But the callback of `min`/`max` should not be called multiple times.
  216. * The code below should not be implemented repeatedly either.
  217. * So we cache the result in the scale instance, which will be recreated at the beginning
  218. * of the workflow (because `scale` instance will be recreated each round of the workflow).
  219. */
  220. export function ensureScaleRawExtentInfo(scale, model,
  221. // Usually: data extent from all series on this axis.
  222. originalExtent) {
  223. // Do not permit to recreate.
  224. var rawExtentInfo = scale.rawExtentInfo;
  225. if (rawExtentInfo) {
  226. return rawExtentInfo;
  227. }
  228. rawExtentInfo = new ScaleRawExtentInfo(scale, model, originalExtent);
  229. // @ts-ignore
  230. scale.rawExtentInfo = rawExtentInfo;
  231. return rawExtentInfo;
  232. }
  233. export function parseAxisModelMinMax(scale, minMax) {
  234. return minMax == null ? null : eqNaN(minMax) ? NaN : scale.parse(minMax);
  235. }