AxisProxy.js 15 KB

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