DataZoomModel.js 17 KB

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