scaleRawExtentInfo.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 =
  43. /** @class */
  44. function () {
  45. function ScaleRawExtentInfo(scale, model, // Usually: data extent from all series on this axis.
  46. originalExtent) {
  47. this._prepareParams(scale, model, originalExtent);
  48. }
  49. /**
  50. * Parameters depending on outside (like model, user callback)
  51. * are prepared and fixed here.
  52. */
  53. ScaleRawExtentInfo.prototype._prepareParams = function (scale, model, // 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; // Currently if a `'value'` axis model min is specified as 'dataMin'/'dataMax',
  122. // `boundaryGap` will not be used. It's the different from specifying as `null`/`undefined`.
  123. var min = this._modelMinRaw === 'dataMin' ? dataMin : this._modelMinNum;
  124. var max = this._modelMaxRaw === 'dataMax' ? dataMax : this._modelMaxNum; // If `_modelMinNum`/`_modelMaxNum` is `null`/`undefined`, should not be fixed.
  125. var minFixed = min != null;
  126. var maxFixed = max != null;
  127. if (min == null) {
  128. min = isOrdinal ? axisDataLen ? 0 : NaN : dataMin - boundaryGapInner[0] * span;
  129. }
  130. if (max == null) {
  131. max = isOrdinal ? axisDataLen ? axisDataLen - 1 : NaN : dataMax + boundaryGapInner[1] * span;
  132. }
  133. (min == null || !isFinite(min)) && (min = NaN);
  134. (max == null || !isFinite(max)) && (max = NaN);
  135. var isBlank = eqNaN(min) || eqNaN(max) || isOrdinal && !axisDataLen; // If data extent modified, need to recalculated to ensure cross zero.
  136. if (this._needCrossZero) {
  137. // Axis is over zero and min is not set
  138. if (min > 0 && max > 0 && !minFixed) {
  139. min = 0; // minFixed = true;
  140. } // Axis is under zero and max is not set
  141. if (min < 0 && max < 0 && !maxFixed) {
  142. max = 0; // maxFixed = true;
  143. } // PENDING:
  144. // When `needCrossZero` and all data is positive/negative, should it be ensured
  145. // that the results processed by boundaryGap are positive/negative?
  146. // If so, here `minFixed`/`maxFixed` need to be set.
  147. }
  148. var determinedMin = this._determinedMin;
  149. var determinedMax = this._determinedMax;
  150. if (determinedMin != null) {
  151. min = determinedMin;
  152. minFixed = true;
  153. }
  154. if (determinedMax != null) {
  155. max = determinedMax;
  156. maxFixed = true;
  157. } // Ensure min/max be finite number or NaN here. (not to be null/undefined)
  158. // `NaN` means min/max axis is blank.
  159. return {
  160. min: min,
  161. max: max,
  162. minFixed: minFixed,
  163. maxFixed: maxFixed,
  164. isBlank: isBlank
  165. };
  166. };
  167. ScaleRawExtentInfo.prototype.modifyDataMinMax = function (minMaxName, val) {
  168. if (process.env.NODE_ENV !== 'production') {
  169. assert(!this.frozen);
  170. }
  171. this[DATA_MIN_MAX_ATTR[minMaxName]] = val;
  172. };
  173. ScaleRawExtentInfo.prototype.setDeterminedMinMax = function (minMaxName, val) {
  174. var attr = DETERMINED_MIN_MAX_ATTR[minMaxName];
  175. if (process.env.NODE_ENV !== 'production') {
  176. assert(!this.frozen // Earse them usually means logic flaw.
  177. && this[attr] == null);
  178. }
  179. this[attr] = val;
  180. };
  181. ScaleRawExtentInfo.prototype.freeze = function () {
  182. // @ts-ignore
  183. this.frozen = true;
  184. };
  185. return ScaleRawExtentInfo;
  186. }();
  187. export { ScaleRawExtentInfo };
  188. var DETERMINED_MIN_MAX_ATTR = {
  189. min: '_determinedMin',
  190. max: '_determinedMax'
  191. };
  192. var DATA_MIN_MAX_ATTR = {
  193. min: '_dataMin',
  194. max: '_dataMax'
  195. };
  196. /**
  197. * Get scale min max and related info only depends on model settings.
  198. * This method can be called after coordinate system created.
  199. * For example, in data processing stage.
  200. *
  201. * Scale extent info probably be required multiple times during a workflow.
  202. * For example:
  203. * (1) `dataZoom` depends it to get the axis extent in "100%" state.
  204. * (2) `processor/extentCalculator` depends it to make sure whether axis extent is specified.
  205. * (3) `coordSys.update` use it to finally decide the scale extent.
  206. * But the callback of `min`/`max` should not be called multiple times.
  207. * The code below should not be implemented repeatedly either.
  208. * So we cache the result in the scale instance, which will be recreated at the beginning
  209. * of the workflow (because `scale` instance will be recreated each round of the workflow).
  210. */
  211. export function ensureScaleRawExtentInfo(scale, model, // Usually: data extent from all series on this axis.
  212. originalExtent) {
  213. // Do not permit to recreate.
  214. var rawExtentInfo = scale.rawExtentInfo;
  215. if (rawExtentInfo) {
  216. return rawExtentInfo;
  217. }
  218. rawExtentInfo = new ScaleRawExtentInfo(scale, model, originalExtent); // @ts-ignore
  219. scale.rawExtentInfo = rawExtentInfo;
  220. return rawExtentInfo;
  221. }
  222. export function parseAxisModelMinMax(scale, minMax) {
  223. return minMax == null ? null : eqNaN(minMax) ? NaN : scale.parse(minMax);
  224. }