AxisProxy.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. valueWindow[idx] = boundValue;
  145. percentWindow[idx] = boundPercent;
  146. });
  147. asc(valueWindow);
  148. asc(percentWindow); // The windows from user calling of `dispatchAction` might be out of the extent,
  149. // or do not obey the `min/maxSpan`, `min/maxValueSpan`. But we dont restrict window
  150. // by `zoomLock` here, because we see `zoomLock` just as a interaction constraint,
  151. // where API is able to initialize/modify the window size even though `zoomLock`
  152. // specified.
  153. var spans = this._minMaxSpan;
  154. hasPropModeValue ? restrictSet(valueWindow, percentWindow, dataExtent, percentExtent, false) : restrictSet(percentWindow, valueWindow, percentExtent, dataExtent, true);
  155. function restrictSet(fromWindow, toWindow, fromExtent, toExtent, toValue) {
  156. var suffix = toValue ? 'Span' : 'ValueSpan';
  157. sliderMove(0, fromWindow, fromExtent, 'all', spans['min' + suffix], spans['max' + suffix]);
  158. for (var i = 0; i < 2; i++) {
  159. toWindow[i] = numberUtil.linearMap(fromWindow[i], fromExtent, toExtent, true);
  160. toValue && (toWindow[i] = scale.parse(toWindow[i]));
  161. }
  162. }
  163. return {
  164. valueWindow: valueWindow,
  165. percentWindow: percentWindow
  166. };
  167. };
  168. /**
  169. * Notice: reset should not be called before series.restoreData() called,
  170. * so it is recommanded to be called in "process stage" but not "model init
  171. * stage".
  172. */
  173. AxisProxy.prototype.reset = function (dataZoomModel) {
  174. if (dataZoomModel !== this._dataZoomModel) {
  175. return;
  176. }
  177. var targetSeries = this.getTargetSeriesModels(); // Culculate data window and data extent, and record them.
  178. this._dataExtent = calculateDataExtent(this, this._dimName, targetSeries); // `calculateDataWindow` uses min/maxSpan.
  179. this._updateMinMaxSpan();
  180. var dataWindow = this.calculateDataWindow(dataZoomModel.settledOption);
  181. this._valueWindow = dataWindow.valueWindow;
  182. this._percentWindow = dataWindow.percentWindow; // Update axis setting then.
  183. this._setAxisModel();
  184. };
  185. AxisProxy.prototype.filterData = function (dataZoomModel, api) {
  186. if (dataZoomModel !== this._dataZoomModel) {
  187. return;
  188. }
  189. var axisDim = this._dimName;
  190. var seriesModels = this.getTargetSeriesModels();
  191. var filterMode = dataZoomModel.get('filterMode');
  192. var valueWindow = this._valueWindow;
  193. if (filterMode === 'none') {
  194. return;
  195. } // FIXME
  196. // Toolbox may has dataZoom injected. And if there are stacked bar chart
  197. // with NaN data, NaN will be filtered and stack will be wrong.
  198. // So we need to force the mode to be set empty.
  199. // In fect, it is not a big deal that do not support filterMode-'filter'
  200. // when using toolbox#dataZoom, utill tooltip#dataZoom support "single axis
  201. // selection" some day, which might need "adapt to data extent on the
  202. // otherAxis", which is disabled by filterMode-'empty'.
  203. // But currently, stack has been fixed to based on value but not index,
  204. // so this is not an issue any more.
  205. // let otherAxisModel = this.getOtherAxisModel();
  206. // if (dataZoomModel.get('$fromToolbox')
  207. // && otherAxisModel
  208. // && otherAxisModel.hasSeriesStacked
  209. // ) {
  210. // filterMode = 'empty';
  211. // }
  212. // TODO
  213. // filterMode 'weakFilter' and 'empty' is not optimized for huge data yet.
  214. each(seriesModels, function (seriesModel) {
  215. var seriesData = seriesModel.getData();
  216. var dataDims = seriesData.mapDimensionsAll(axisDim);
  217. if (!dataDims.length) {
  218. return;
  219. }
  220. if (filterMode === 'weakFilter') {
  221. var store_1 = seriesData.getStore();
  222. var dataDimIndices_1 = zrUtil.map(dataDims, function (dim) {
  223. return seriesData.getDimensionIndex(dim);
  224. }, seriesData);
  225. seriesData.filterSelf(function (dataIndex) {
  226. var leftOut;
  227. var rightOut;
  228. var hasValue;
  229. for (var i = 0; i < dataDims.length; i++) {
  230. var value = store_1.get(dataDimIndices_1[i], dataIndex);
  231. var thisHasValue = !isNaN(value);
  232. var thisLeftOut = value < valueWindow[0];
  233. var thisRightOut = value > valueWindow[1];
  234. if (thisHasValue && !thisLeftOut && !thisRightOut) {
  235. return true;
  236. }
  237. thisHasValue && (hasValue = true);
  238. thisLeftOut && (leftOut = true);
  239. thisRightOut && (rightOut = true);
  240. } // If both left out and right out, do not filter.
  241. return hasValue && leftOut && rightOut;
  242. });
  243. } else {
  244. each(dataDims, function (dim) {
  245. if (filterMode === 'empty') {
  246. seriesModel.setData(seriesData = seriesData.map(dim, function (value) {
  247. return !isInWindow(value) ? NaN : value;
  248. }));
  249. } else {
  250. var range = {};
  251. range[dim] = valueWindow; // console.time('select');
  252. seriesData.selectRange(range); // console.timeEnd('select');
  253. }
  254. });
  255. }
  256. each(dataDims, function (dim) {
  257. seriesData.setApproximateExtent(valueWindow, dim);
  258. });
  259. });
  260. function isInWindow(value) {
  261. return value >= valueWindow[0] && value <= valueWindow[1];
  262. }
  263. };
  264. AxisProxy.prototype._updateMinMaxSpan = function () {
  265. var minMaxSpan = this._minMaxSpan = {};
  266. var dataZoomModel = this._dataZoomModel;
  267. var dataExtent = this._dataExtent;
  268. each(['min', 'max'], function (minMax) {
  269. var percentSpan = dataZoomModel.get(minMax + 'Span');
  270. var valueSpan = dataZoomModel.get(minMax + 'ValueSpan');
  271. valueSpan != null && (valueSpan = this.getAxisModel().axis.scale.parse(valueSpan)); // minValueSpan and maxValueSpan has higher priority than minSpan and maxSpan
  272. if (valueSpan != null) {
  273. percentSpan = numberUtil.linearMap(dataExtent[0] + valueSpan, dataExtent, [0, 100], true);
  274. } else if (percentSpan != null) {
  275. valueSpan = numberUtil.linearMap(percentSpan, [0, 100], dataExtent, true) - dataExtent[0];
  276. }
  277. minMaxSpan[minMax + 'Span'] = percentSpan;
  278. minMaxSpan[minMax + 'ValueSpan'] = valueSpan;
  279. }, this);
  280. };
  281. AxisProxy.prototype._setAxisModel = function () {
  282. var axisModel = this.getAxisModel();
  283. var percentWindow = this._percentWindow;
  284. var valueWindow = this._valueWindow;
  285. if (!percentWindow) {
  286. return;
  287. } // [0, 500]: arbitrary value, guess axis extent.
  288. var precision = numberUtil.getPixelPrecision(valueWindow, [0, 500]);
  289. precision = Math.min(precision, 20); // For value axis, if min/max/scale are not set, we just use the extent obtained
  290. // by series data, which may be a little different from the extent calculated by
  291. // `axisHelper.getScaleExtent`. But the different just affects the experience a
  292. // little when zooming. So it will not be fixed until some users require it strongly.
  293. var rawExtentInfo = axisModel.axis.scale.rawExtentInfo;
  294. if (percentWindow[0] !== 0) {
  295. rawExtentInfo.setDeterminedMinMax('min', +valueWindow[0].toFixed(precision));
  296. }
  297. if (percentWindow[1] !== 100) {
  298. rawExtentInfo.setDeterminedMinMax('max', +valueWindow[1].toFixed(precision));
  299. }
  300. rawExtentInfo.freeze();
  301. };
  302. return AxisProxy;
  303. }();
  304. function calculateDataExtent(axisProxy, axisDim, seriesModels) {
  305. var dataExtent = [Infinity, -Infinity];
  306. each(seriesModels, function (seriesModel) {
  307. unionAxisExtentFromData(dataExtent, seriesModel.getData(), axisDim);
  308. }); // It is important to get "consistent" extent when more then one axes is
  309. // controlled by a `dataZoom`, otherwise those axes will not be synchronized
  310. // when zooming. But it is difficult to know what is "consistent", considering
  311. // axes have different type or even different meanings (For example, two
  312. // time axes are used to compare data of the same date in different years).
  313. // So basically dataZoom just obtains extent by series.data (in category axis
  314. // extent can be obtained from axis.data).
  315. // Nevertheless, user can set min/max/scale on axes to make extent of axes
  316. // consistent.
  317. var axisModel = axisProxy.getAxisModel();
  318. var rawExtentResult = ensureScaleRawExtentInfo(axisModel.axis.scale, axisModel, dataExtent).calculate();
  319. return [rawExtentResult.min, rawExtentResult.max];
  320. }
  321. export default AxisProxy;