AxisProxy.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import * as numberUtil from '../../util/number.js';
  42. import sliderMove from '../helper/sliderMove.js';
  43. import { unionAxisExtentFromData } from '../../coord/axisHelper.js';
  44. import { ensureScaleRawExtentInfo } from '../../coord/scaleRawExtentInfo.js';
  45. import { getAxisMainType, isCoordSupported } from './helper.js';
  46. import { SINGLE_REFERRING } from '../../util/model.js';
  47. var each = zrUtil.each;
  48. var asc = numberUtil.asc;
  49. /**
  50. * Operate single axis.
  51. * One axis can only operated by one axis operator.
  52. * Different dataZoomModels may be defined to operate the same axis.
  53. * (i.e. 'inside' data zoom and 'slider' data zoom components)
  54. * So dataZoomModels share one axisProxy in that case.
  55. */
  56. var AxisProxy =
  57. /** @class */
  58. function () {
  59. function AxisProxy(dimName, axisIndex, dataZoomModel, ecModel) {
  60. this._dimName = dimName;
  61. this._axisIndex = axisIndex;
  62. this.ecModel = ecModel;
  63. this._dataZoomModel = dataZoomModel; // /**
  64. // * @readOnly
  65. // * @private
  66. // */
  67. // this.hasSeriesStacked;
  68. }
  69. /**
  70. * Whether the axisProxy is hosted by dataZoomModel.
  71. */
  72. AxisProxy.prototype.hostedBy = function (dataZoomModel) {
  73. return this._dataZoomModel === dataZoomModel;
  74. };
  75. /**
  76. * @return Value can only be NaN or finite value.
  77. */
  78. AxisProxy.prototype.getDataValueWindow = function () {
  79. return this._valueWindow.slice();
  80. };
  81. /**
  82. * @return {Array.<number>}
  83. */
  84. AxisProxy.prototype.getDataPercentWindow = function () {
  85. return this._percentWindow.slice();
  86. };
  87. AxisProxy.prototype.getTargetSeriesModels = function () {
  88. var seriesModels = [];
  89. this.ecModel.eachSeries(function (seriesModel) {
  90. if (isCoordSupported(seriesModel)) {
  91. var axisMainType = getAxisMainType(this._dimName);
  92. var axisModel = seriesModel.getReferringComponents(axisMainType, SINGLE_REFERRING).models[0];
  93. if (axisModel && this._axisIndex === axisModel.componentIndex) {
  94. seriesModels.push(seriesModel);
  95. }
  96. }
  97. }, this);
  98. return seriesModels;
  99. };
  100. AxisProxy.prototype.getAxisModel = function () {
  101. return this.ecModel.getComponent(this._dimName + 'Axis', this._axisIndex);
  102. };
  103. AxisProxy.prototype.getMinMaxSpan = function () {
  104. return zrUtil.clone(this._minMaxSpan);
  105. };
  106. /**
  107. * Only calculate by given range and this._dataExtent, do not change anything.
  108. */
  109. AxisProxy.prototype.calculateDataWindow = function (opt) {
  110. var dataExtent = this._dataExtent;
  111. var axisModel = this.getAxisModel();
  112. var scale = axisModel.axis.scale;
  113. var rangePropMode = this._dataZoomModel.getRangePropMode();
  114. var percentExtent = [0, 100];
  115. var percentWindow = [];
  116. var valueWindow = [];
  117. var hasPropModeValue;
  118. each(['start', 'end'], function (prop, idx) {
  119. var boundPercent = opt[prop];
  120. var boundValue = opt[prop + 'Value']; // Notice: dataZoom is based either on `percentProp` ('start', 'end') or
  121. // on `valueProp` ('startValue', 'endValue'). (They are based on the data extent
  122. // but not min/max of axis, which will be calculated by data window then).
  123. // The former one is suitable for cases that a dataZoom component controls multiple
  124. // axes with different unit or extent, and the latter one is suitable for accurate
  125. // zoom by pixel (e.g., in dataZoomSelect).
  126. // we use `getRangePropMode()` to mark which prop is used. `rangePropMode` is updated
  127. // only when setOption or dispatchAction, otherwise it remains its original value.
  128. // (Why not only record `percentProp` and always map to `valueProp`? Because
  129. // the map `valueProp` -> `percentProp` -> `valueProp` probably not the original
  130. // `valueProp`. consider two axes constrolled by one dataZoom. They have different
  131. // data extent. All of values that are overflow the `dataExtent` will be calculated
  132. // to percent '100%').
  133. if (rangePropMode[idx] === 'percent') {
  134. boundPercent == null && (boundPercent = percentExtent[idx]); // Use scale.parse to math round for category or time axis.
  135. boundValue = scale.parse(numberUtil.linearMap(boundPercent, percentExtent, dataExtent));
  136. } else {
  137. hasPropModeValue = true;
  138. boundValue = boundValue == null ? dataExtent[idx] : scale.parse(boundValue); // Calculating `percent` from `value` may be not accurate, because
  139. // This calculation can not be inversed, because all of values that
  140. // are overflow the `dataExtent` will be calculated to percent '100%'
  141. boundPercent = numberUtil.linearMap(boundValue, dataExtent, percentExtent);
  142. } // valueWindow[idx] = round(boundValue);
  143. // percentWindow[idx] = round(boundPercent);
  144. // fallback to extent start/end when parsed value or percent is invalid
  145. valueWindow[idx] = boundValue == null || isNaN(boundValue) ? dataExtent[idx] : boundValue;
  146. percentWindow[idx] = boundPercent == null || isNaN(boundPercent) ? percentExtent[idx] : boundPercent;
  147. });
  148. asc(valueWindow);
  149. asc(percentWindow); // The windows from user calling of `dispatchAction` might be out of the extent,
  150. // or do not obey the `min/maxSpan`, `min/maxValueSpan`. But we don't restrict window
  151. // by `zoomLock` here, because we see `zoomLock` just as a interaction constraint,
  152. // where API is able to initialize/modify the window size even though `zoomLock`
  153. // specified.
  154. var spans = this._minMaxSpan;
  155. hasPropModeValue ? restrictSet(valueWindow, percentWindow, dataExtent, percentExtent, false) : restrictSet(percentWindow, valueWindow, percentExtent, dataExtent, true);
  156. function restrictSet(fromWindow, toWindow, fromExtent, toExtent, toValue) {
  157. var suffix = toValue ? 'Span' : 'ValueSpan';
  158. sliderMove(0, fromWindow, fromExtent, 'all', spans['min' + suffix], spans['max' + suffix]);
  159. for (var i = 0; i < 2; i++) {
  160. toWindow[i] = numberUtil.linearMap(fromWindow[i], fromExtent, toExtent, true);
  161. toValue && (toWindow[i] = scale.parse(toWindow[i]));
  162. }
  163. }
  164. return {
  165. valueWindow: valueWindow,
  166. percentWindow: percentWindow
  167. };
  168. };
  169. /**
  170. * Notice: reset should not be called before series.restoreData() is called,
  171. * so it is recommended to be called in "process stage" but not "model init
  172. * stage".
  173. */
  174. AxisProxy.prototype.reset = function (dataZoomModel) {
  175. if (dataZoomModel !== this._dataZoomModel) {
  176. return;
  177. }
  178. var targetSeries = this.getTargetSeriesModels(); // Culculate data window and data extent, and record them.
  179. this._dataExtent = calculateDataExtent(this, this._dimName, targetSeries); // `calculateDataWindow` uses min/maxSpan.
  180. this._updateMinMaxSpan();
  181. var dataWindow = this.calculateDataWindow(dataZoomModel.settledOption);
  182. this._valueWindow = dataWindow.valueWindow;
  183. this._percentWindow = dataWindow.percentWindow; // Update axis setting then.
  184. this._setAxisModel();
  185. };
  186. AxisProxy.prototype.filterData = function (dataZoomModel, api) {
  187. if (dataZoomModel !== this._dataZoomModel) {
  188. return;
  189. }
  190. var axisDim = this._dimName;
  191. var seriesModels = this.getTargetSeriesModels();
  192. var filterMode = dataZoomModel.get('filterMode');
  193. var valueWindow = this._valueWindow;
  194. if (filterMode === 'none') {
  195. return;
  196. } // FIXME
  197. // Toolbox may has dataZoom injected. And if there are stacked bar chart
  198. // with NaN data, NaN will be filtered and stack will be wrong.
  199. // So we need to force the mode to be set empty.
  200. // In fect, it is not a big deal that do not support filterMode-'filter'
  201. // when using toolbox#dataZoom, utill tooltip#dataZoom support "single axis
  202. // selection" some day, which might need "adapt to data extent on the
  203. // otherAxis", which is disabled by filterMode-'empty'.
  204. // But currently, stack has been fixed to based on value but not index,
  205. // so this is not an issue any more.
  206. // let otherAxisModel = this.getOtherAxisModel();
  207. // if (dataZoomModel.get('$fromToolbox')
  208. // && otherAxisModel
  209. // && otherAxisModel.hasSeriesStacked
  210. // ) {
  211. // filterMode = 'empty';
  212. // }
  213. // TODO
  214. // filterMode 'weakFilter' and 'empty' is not optimized for huge data yet.
  215. each(seriesModels, function (seriesModel) {
  216. var seriesData = seriesModel.getData();
  217. var dataDims = seriesData.mapDimensionsAll(axisDim);
  218. if (!dataDims.length) {
  219. return;
  220. }
  221. if (filterMode === 'weakFilter') {
  222. var store_1 = seriesData.getStore();
  223. var dataDimIndices_1 = zrUtil.map(dataDims, function (dim) {
  224. return seriesData.getDimensionIndex(dim);
  225. }, seriesData);
  226. seriesData.filterSelf(function (dataIndex) {
  227. var leftOut;
  228. var rightOut;
  229. var hasValue;
  230. for (var i = 0; i < dataDims.length; i++) {
  231. var value = store_1.get(dataDimIndices_1[i], dataIndex);
  232. var thisHasValue = !isNaN(value);
  233. var thisLeftOut = value < valueWindow[0];
  234. var thisRightOut = value > valueWindow[1];
  235. if (thisHasValue && !thisLeftOut && !thisRightOut) {
  236. return true;
  237. }
  238. thisHasValue && (hasValue = true);
  239. thisLeftOut && (leftOut = true);
  240. thisRightOut && (rightOut = true);
  241. } // If both left out and right out, do not filter.
  242. return hasValue && leftOut && rightOut;
  243. });
  244. } else {
  245. each(dataDims, function (dim) {
  246. if (filterMode === 'empty') {
  247. seriesModel.setData(seriesData = seriesData.map(dim, function (value) {
  248. return !isInWindow(value) ? NaN : value;
  249. }));
  250. } else {
  251. var range = {};
  252. range[dim] = valueWindow; // console.time('select');
  253. seriesData.selectRange(range); // console.timeEnd('select');
  254. }
  255. });
  256. }
  257. each(dataDims, function (dim) {
  258. seriesData.setApproximateExtent(valueWindow, dim);
  259. });
  260. });
  261. function isInWindow(value) {
  262. return value >= valueWindow[0] && value <= valueWindow[1];
  263. }
  264. };
  265. AxisProxy.prototype._updateMinMaxSpan = function () {
  266. var minMaxSpan = this._minMaxSpan = {};
  267. var dataZoomModel = this._dataZoomModel;
  268. var dataExtent = this._dataExtent;
  269. each(['min', 'max'], function (minMax) {
  270. var percentSpan = dataZoomModel.get(minMax + 'Span');
  271. var valueSpan = dataZoomModel.get(minMax + 'ValueSpan');
  272. valueSpan != null && (valueSpan = this.getAxisModel().axis.scale.parse(valueSpan)); // minValueSpan and maxValueSpan has higher priority than minSpan and maxSpan
  273. if (valueSpan != null) {
  274. percentSpan = numberUtil.linearMap(dataExtent[0] + valueSpan, dataExtent, [0, 100], true);
  275. } else if (percentSpan != null) {
  276. valueSpan = numberUtil.linearMap(percentSpan, [0, 100], dataExtent, true) - dataExtent[0];
  277. }
  278. minMaxSpan[minMax + 'Span'] = percentSpan;
  279. minMaxSpan[minMax + 'ValueSpan'] = valueSpan;
  280. }, this);
  281. };
  282. AxisProxy.prototype._setAxisModel = function () {
  283. var axisModel = this.getAxisModel();
  284. var percentWindow = this._percentWindow;
  285. var valueWindow = this._valueWindow;
  286. if (!percentWindow) {
  287. return;
  288. } // [0, 500]: arbitrary value, guess axis extent.
  289. var precision = numberUtil.getPixelPrecision(valueWindow, [0, 500]);
  290. precision = Math.min(precision, 20); // For value axis, if min/max/scale are not set, we just use the extent obtained
  291. // by series data, which may be a little different from the extent calculated by
  292. // `axisHelper.getScaleExtent`. But the different just affects the experience a
  293. // little when zooming. So it will not be fixed until some users require it strongly.
  294. var rawExtentInfo = axisModel.axis.scale.rawExtentInfo;
  295. if (percentWindow[0] !== 0) {
  296. rawExtentInfo.setDeterminedMinMax('min', +valueWindow[0].toFixed(precision));
  297. }
  298. if (percentWindow[1] !== 100) {
  299. rawExtentInfo.setDeterminedMinMax('max', +valueWindow[1].toFixed(precision));
  300. }
  301. rawExtentInfo.freeze();
  302. };
  303. return AxisProxy;
  304. }();
  305. function calculateDataExtent(axisProxy, axisDim, seriesModels) {
  306. var dataExtent = [Infinity, -Infinity];
  307. each(seriesModels, function (seriesModel) {
  308. unionAxisExtentFromData(dataExtent, seriesModel.getData(), axisDim);
  309. }); // It is important to get "consistent" extent when more then one axes is
  310. // controlled by a `dataZoom`, otherwise those axes will not be synchronized
  311. // when zooming. But it is difficult to know what is "consistent", considering
  312. // axes have different type or even different meanings (For example, two
  313. // time axes are used to compare data of the same date in different years).
  314. // So basically dataZoom just obtains extent by series.data (in category axis
  315. // extent can be obtained from axis.data).
  316. // Nevertheless, user can set min/max/scale on axes to make extent of axes
  317. // consistent.
  318. var axisModel = axisProxy.getAxisModel();
  319. var rawExtentResult = ensureScaleRawExtentInfo(axisModel.axis.scale, axisModel, dataExtent).calculate();
  320. return [rawExtentResult.min, rawExtentResult.max];
  321. }
  322. export default AxisProxy;