DataZoomModel.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 { __extends } from "tslib";
  41. import { each, createHashMap, merge, assert } from 'zrender/lib/core/util.js';
  42. import ComponentModel from '../../model/Component.js';
  43. import { getAxisMainType, DATA_ZOOM_AXIS_DIMENSIONS } from './helper.js';
  44. import { MULTIPLE_REFERRING, SINGLE_REFERRING } from '../../util/model.js';
  45. var DataZoomAxisInfo = /** @class */function () {
  46. function DataZoomAxisInfo() {
  47. this.indexList = [];
  48. this.indexMap = [];
  49. }
  50. DataZoomAxisInfo.prototype.add = function (axisCmptIdx) {
  51. // Remove duplication.
  52. if (!this.indexMap[axisCmptIdx]) {
  53. this.indexList.push(axisCmptIdx);
  54. this.indexMap[axisCmptIdx] = true;
  55. }
  56. };
  57. return DataZoomAxisInfo;
  58. }();
  59. var DataZoomModel = /** @class */function (_super) {
  60. __extends(DataZoomModel, _super);
  61. function DataZoomModel() {
  62. var _this = _super !== null && _super.apply(this, arguments) || this;
  63. _this.type = DataZoomModel.type;
  64. _this._autoThrottle = true;
  65. _this._noTarget = true;
  66. /**
  67. * It is `[rangeModeForMin, rangeModeForMax]`.
  68. * The optional values for `rangeMode`:
  69. * + `'value'` mode: the axis extent will always be determined by
  70. * `dataZoom.startValue` and `dataZoom.endValue`, despite
  71. * how data like and how `axis.min` and `axis.max` are.
  72. * + `'percent'` mode: `100` represents 100% of the `[dMin, dMax]`,
  73. * where `dMin` is `axis.min` if `axis.min` specified, otherwise `data.extent[0]`,
  74. * and `dMax` is `axis.max` if `axis.max` specified, otherwise `data.extent[1]`.
  75. * Axis extent will be determined by the result of the percent of `[dMin, dMax]`.
  76. *
  77. * For example, when users are using dynamic data (update data periodically via `setOption`),
  78. * if in `'value`' mode, the window will be kept in a fixed value range despite how
  79. * data are appended, while if in `'percent'` mode, whe window range will be changed alone with
  80. * the appended data (suppose `axis.min` and `axis.max` are not specified).
  81. */
  82. _this._rangePropMode = ['percent', 'percent'];
  83. return _this;
  84. }
  85. DataZoomModel.prototype.init = function (option, parentModel, ecModel) {
  86. var inputRawOption = retrieveRawOption(option);
  87. /**
  88. * Suppose a "main process" start at the point that model prepared (that is,
  89. * model initialized or merged or method called in `action`).
  90. * We should keep the `main process` idempotent, that is, given a set of values
  91. * on `option`, we get the same result.
  92. *
  93. * But sometimes, values on `option` will be updated for providing users
  94. * a "final calculated value" (`dataZoomProcessor` will do that). Those value
  95. * should not be the base/input of the `main process`.
  96. *
  97. * So in that case we should save and keep the input of the `main process`
  98. * separately, called `settledOption`.
  99. *
  100. * For example, consider the case:
  101. * (Step_1) brush zoom the grid by `toolbox.dataZoom`,
  102. * where the original input `option.startValue`, `option.endValue` are earsed by
  103. * calculated value.
  104. * (Step)2) click the legend to hide and show a series,
  105. * where the new range is calculated by the earsed `startValue` and `endValue`,
  106. * which brings incorrect result.
  107. */
  108. this.settledOption = inputRawOption;
  109. this.mergeDefaultAndTheme(option, ecModel);
  110. this._doInit(inputRawOption);
  111. };
  112. DataZoomModel.prototype.mergeOption = function (newOption) {
  113. var inputRawOption = retrieveRawOption(newOption);
  114. // FIX #2591
  115. merge(this.option, newOption, true);
  116. merge(this.settledOption, inputRawOption, true);
  117. this._doInit(inputRawOption);
  118. };
  119. DataZoomModel.prototype._doInit = function (inputRawOption) {
  120. var thisOption = this.option;
  121. this._setDefaultThrottle(inputRawOption);
  122. this._updateRangeUse(inputRawOption);
  123. var settledOption = this.settledOption;
  124. each([['start', 'startValue'], ['end', 'endValue']], function (names, index) {
  125. // start/end has higher priority over startValue/endValue if they
  126. // both set, but we should make chart.setOption({endValue: 1000})
  127. // effective, rather than chart.setOption({endValue: 1000, end: null}).
  128. if (this._rangePropMode[index] === 'value') {
  129. thisOption[names[0]] = settledOption[names[0]] = null;
  130. }
  131. // Otherwise do nothing and use the merge result.
  132. }, this);
  133. this._resetTarget();
  134. };
  135. DataZoomModel.prototype._resetTarget = function () {
  136. var optionOrient = this.get('orient', true);
  137. var targetAxisIndexMap = this._targetAxisInfoMap = createHashMap();
  138. var hasAxisSpecified = this._fillSpecifiedTargetAxis(targetAxisIndexMap);
  139. if (hasAxisSpecified) {
  140. this._orient = optionOrient || this._makeAutoOrientByTargetAxis();
  141. } else {
  142. this._orient = optionOrient || 'horizontal';
  143. this._fillAutoTargetAxisByOrient(targetAxisIndexMap, this._orient);
  144. }
  145. this._noTarget = true;
  146. targetAxisIndexMap.each(function (axisInfo) {
  147. if (axisInfo.indexList.length) {
  148. this._noTarget = false;
  149. }
  150. }, this);
  151. };
  152. DataZoomModel.prototype._fillSpecifiedTargetAxis = function (targetAxisIndexMap) {
  153. var hasAxisSpecified = false;
  154. each(DATA_ZOOM_AXIS_DIMENSIONS, function (axisDim) {
  155. var refering = this.getReferringComponents(getAxisMainType(axisDim), MULTIPLE_REFERRING);
  156. // When user set axisIndex as a empty array, we think that user specify axisIndex
  157. // but do not want use auto mode. Because empty array may be encountered when
  158. // some error occurred.
  159. if (!refering.specified) {
  160. return;
  161. }
  162. hasAxisSpecified = true;
  163. var axisInfo = new DataZoomAxisInfo();
  164. each(refering.models, function (axisModel) {
  165. axisInfo.add(axisModel.componentIndex);
  166. });
  167. targetAxisIndexMap.set(axisDim, axisInfo);
  168. }, this);
  169. return hasAxisSpecified;
  170. };
  171. DataZoomModel.prototype._fillAutoTargetAxisByOrient = function (targetAxisIndexMap, orient) {
  172. var ecModel = this.ecModel;
  173. var needAuto = true;
  174. // Find axis that parallel to dataZoom as default.
  175. if (needAuto) {
  176. var axisDim = orient === 'vertical' ? 'y' : 'x';
  177. var axisModels = ecModel.findComponents({
  178. mainType: axisDim + 'Axis'
  179. });
  180. setParallelAxis(axisModels, axisDim);
  181. }
  182. // Find axis that parallel to dataZoom as default.
  183. if (needAuto) {
  184. var axisModels = ecModel.findComponents({
  185. mainType: 'singleAxis',
  186. filter: function (axisModel) {
  187. return axisModel.get('orient', true) === orient;
  188. }
  189. });
  190. setParallelAxis(axisModels, 'single');
  191. }
  192. function setParallelAxis(axisModels, axisDim) {
  193. // At least use the first parallel axis as the target axis.
  194. var axisModel = axisModels[0];
  195. if (!axisModel) {
  196. return;
  197. }
  198. var axisInfo = new DataZoomAxisInfo();
  199. axisInfo.add(axisModel.componentIndex);
  200. targetAxisIndexMap.set(axisDim, axisInfo);
  201. needAuto = false;
  202. // Find parallel axes in the same grid.
  203. if (axisDim === 'x' || axisDim === 'y') {
  204. var gridModel_1 = axisModel.getReferringComponents('grid', SINGLE_REFERRING).models[0];
  205. gridModel_1 && each(axisModels, function (axModel) {
  206. if (axisModel.componentIndex !== axModel.componentIndex && gridModel_1 === axModel.getReferringComponents('grid', SINGLE_REFERRING).models[0]) {
  207. axisInfo.add(axModel.componentIndex);
  208. }
  209. });
  210. }
  211. }
  212. if (needAuto) {
  213. // If no parallel axis, find the first category axis as default. (Also consider polar).
  214. each(DATA_ZOOM_AXIS_DIMENSIONS, function (axisDim) {
  215. if (!needAuto) {
  216. return;
  217. }
  218. var axisModels = ecModel.findComponents({
  219. mainType: getAxisMainType(axisDim),
  220. filter: function (axisModel) {
  221. return axisModel.get('type', true) === 'category';
  222. }
  223. });
  224. if (axisModels[0]) {
  225. var axisInfo = new DataZoomAxisInfo();
  226. axisInfo.add(axisModels[0].componentIndex);
  227. targetAxisIndexMap.set(axisDim, axisInfo);
  228. needAuto = false;
  229. }
  230. }, this);
  231. }
  232. };
  233. DataZoomModel.prototype._makeAutoOrientByTargetAxis = function () {
  234. var dim;
  235. // Find the first axis
  236. this.eachTargetAxis(function (axisDim) {
  237. !dim && (dim = axisDim);
  238. }, this);
  239. return dim === 'y' ? 'vertical' : 'horizontal';
  240. };
  241. DataZoomModel.prototype._setDefaultThrottle = function (inputRawOption) {
  242. // When first time user set throttle, auto throttle ends.
  243. if (inputRawOption.hasOwnProperty('throttle')) {
  244. this._autoThrottle = false;
  245. }
  246. if (this._autoThrottle) {
  247. var globalOption = this.ecModel.option;
  248. this.option.throttle = globalOption.animation && globalOption.animationDurationUpdate > 0 ? 100 : 20;
  249. }
  250. };
  251. DataZoomModel.prototype._updateRangeUse = function (inputRawOption) {
  252. var rangePropMode = this._rangePropMode;
  253. var rangeModeInOption = this.get('rangeMode');
  254. each([['start', 'startValue'], ['end', 'endValue']], function (names, index) {
  255. var percentSpecified = inputRawOption[names[0]] != null;
  256. var valueSpecified = inputRawOption[names[1]] != null;
  257. if (percentSpecified && !valueSpecified) {
  258. rangePropMode[index] = 'percent';
  259. } else if (!percentSpecified && valueSpecified) {
  260. rangePropMode[index] = 'value';
  261. } else if (rangeModeInOption) {
  262. rangePropMode[index] = rangeModeInOption[index];
  263. } else if (percentSpecified) {
  264. // percentSpecified && valueSpecified
  265. rangePropMode[index] = 'percent';
  266. }
  267. // else remain its original setting.
  268. });
  269. };
  270. DataZoomModel.prototype.noTarget = function () {
  271. return this._noTarget;
  272. };
  273. DataZoomModel.prototype.getFirstTargetAxisModel = function () {
  274. var firstAxisModel;
  275. this.eachTargetAxis(function (axisDim, axisIndex) {
  276. if (firstAxisModel == null) {
  277. firstAxisModel = this.ecModel.getComponent(getAxisMainType(axisDim), axisIndex);
  278. }
  279. }, this);
  280. return firstAxisModel;
  281. };
  282. /**
  283. * @param {Function} callback param: axisModel, dimNames, axisIndex, dataZoomModel, ecModel
  284. */
  285. DataZoomModel.prototype.eachTargetAxis = function (callback, context) {
  286. this._targetAxisInfoMap.each(function (axisInfo, axisDim) {
  287. each(axisInfo.indexList, function (axisIndex) {
  288. callback.call(context, axisDim, axisIndex);
  289. });
  290. });
  291. };
  292. /**
  293. * @return If not found, return null/undefined.
  294. */
  295. DataZoomModel.prototype.getAxisProxy = function (axisDim, axisIndex) {
  296. var axisModel = this.getAxisModel(axisDim, axisIndex);
  297. if (axisModel) {
  298. return axisModel.__dzAxisProxy;
  299. }
  300. };
  301. /**
  302. * @return If not found, return null/undefined.
  303. */
  304. DataZoomModel.prototype.getAxisModel = function (axisDim, axisIndex) {
  305. if (process.env.NODE_ENV !== 'production') {
  306. assert(axisDim && axisIndex != null);
  307. }
  308. var axisInfo = this._targetAxisInfoMap.get(axisDim);
  309. if (axisInfo && axisInfo.indexMap[axisIndex]) {
  310. return this.ecModel.getComponent(getAxisMainType(axisDim), axisIndex);
  311. }
  312. };
  313. /**
  314. * If not specified, set to undefined.
  315. */
  316. DataZoomModel.prototype.setRawRange = function (opt) {
  317. var thisOption = this.option;
  318. var settledOption = this.settledOption;
  319. each([['start', 'startValue'], ['end', 'endValue']], function (names) {
  320. // Consider the pair <start, startValue>:
  321. // If one has value and the other one is `null/undefined`, we both set them
  322. // to `settledOption`. This strategy enables the feature to clear the original
  323. // value in `settledOption` to `null/undefined`.
  324. // But if both of them are `null/undefined`, we do not set them to `settledOption`
  325. // and keep `settledOption` with the original value. This strategy enables users to
  326. // only set <end or endValue> but not set <start or startValue> when calling
  327. // `dispatchAction`.
  328. // The pair <end, endValue> is treated in the same way.
  329. if (opt[names[0]] != null || opt[names[1]] != null) {
  330. thisOption[names[0]] = settledOption[names[0]] = opt[names[0]];
  331. thisOption[names[1]] = settledOption[names[1]] = opt[names[1]];
  332. }
  333. }, this);
  334. this._updateRangeUse(opt);
  335. };
  336. DataZoomModel.prototype.setCalculatedRange = function (opt) {
  337. var option = this.option;
  338. each(['start', 'startValue', 'end', 'endValue'], function (name) {
  339. option[name] = opt[name];
  340. });
  341. };
  342. DataZoomModel.prototype.getPercentRange = function () {
  343. var axisProxy = this.findRepresentativeAxisProxy();
  344. if (axisProxy) {
  345. return axisProxy.getDataPercentWindow();
  346. }
  347. };
  348. /**
  349. * For example, chart.getModel().getComponent('dataZoom').getValueRange('y', 0);
  350. *
  351. * @return [startValue, endValue] value can only be '-' or finite number.
  352. */
  353. DataZoomModel.prototype.getValueRange = function (axisDim, axisIndex) {
  354. if (axisDim == null && axisIndex == null) {
  355. var axisProxy = this.findRepresentativeAxisProxy();
  356. if (axisProxy) {
  357. return axisProxy.getDataValueWindow();
  358. }
  359. } else {
  360. return this.getAxisProxy(axisDim, axisIndex).getDataValueWindow();
  361. }
  362. };
  363. /**
  364. * @param axisModel If axisModel given, find axisProxy
  365. * corresponding to the axisModel
  366. */
  367. DataZoomModel.prototype.findRepresentativeAxisProxy = function (axisModel) {
  368. if (axisModel) {
  369. return axisModel.__dzAxisProxy;
  370. }
  371. // Find the first hosted axisProxy
  372. var firstProxy;
  373. var axisDimList = this._targetAxisInfoMap.keys();
  374. for (var i = 0; i < axisDimList.length; i++) {
  375. var axisDim = axisDimList[i];
  376. var axisInfo = this._targetAxisInfoMap.get(axisDim);
  377. for (var j = 0; j < axisInfo.indexList.length; j++) {
  378. var proxy = this.getAxisProxy(axisDim, axisInfo.indexList[j]);
  379. if (proxy.hostedBy(this)) {
  380. return proxy;
  381. }
  382. if (!firstProxy) {
  383. firstProxy = proxy;
  384. }
  385. }
  386. }
  387. // If no hosted proxy found, still need to return a proxy.
  388. // This case always happens in toolbox dataZoom, where axes are all hosted by
  389. // other dataZooms.
  390. return firstProxy;
  391. };
  392. DataZoomModel.prototype.getRangePropMode = function () {
  393. return this._rangePropMode.slice();
  394. };
  395. DataZoomModel.prototype.getOrient = function () {
  396. if (process.env.NODE_ENV !== 'production') {
  397. // Should not be called before initialized.
  398. assert(this._orient);
  399. }
  400. return this._orient;
  401. };
  402. DataZoomModel.type = 'dataZoom';
  403. DataZoomModel.dependencies = ['xAxis', 'yAxis', 'radiusAxis', 'angleAxis', 'singleAxis', 'series', 'toolbox'];
  404. DataZoomModel.defaultOption = {
  405. // zlevel: 0,
  406. z: 4,
  407. filterMode: 'filter',
  408. start: 0,
  409. end: 100
  410. };
  411. return DataZoomModel;
  412. }(ComponentModel);
  413. /**
  414. * Retrieve those raw params from option, which will be cached separately,
  415. * because they will be overwritten by normalized/calculated values in the main
  416. * process.
  417. */
  418. function retrieveRawOption(option) {
  419. var ret = {};
  420. each(['start', 'end', 'startValue', 'endValue', 'throttle'], function (name) {
  421. option.hasOwnProperty(name) && (ret[name] = option[name]);
  422. });
  423. return ret;
  424. }
  425. export default DataZoomModel;