DataZoomModel.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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';
  42. import ComponentModel from '../../model/Component';
  43. import { getAxisMainType, DATA_ZOOM_AXIS_DIMENSIONS } from './helper';
  44. import { MULTIPLE_REFERRING, SINGLE_REFERRING } from '../../util/model';
  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; // if (!env.canvasSupported) {
  124. // thisOption.realtime = false;
  125. // }
  126. this._setDefaultThrottle(inputRawOption);
  127. this._updateRangeUse(inputRawOption);
  128. var settledOption = this.settledOption;
  129. each([['start', 'startValue'], ['end', 'endValue']], function (names, index) {
  130. // start/end has higher priority over startValue/endValue if they
  131. // both set, but we should make chart.setOption({endValue: 1000})
  132. // effective, rather than chart.setOption({endValue: 1000, end: null}).
  133. if (this._rangePropMode[index] === 'value') {
  134. thisOption[names[0]] = settledOption[names[0]] = null;
  135. } // Otherwise do nothing and use the merge result.
  136. }, this);
  137. this._resetTarget();
  138. };
  139. DataZoomModel.prototype._resetTarget = function () {
  140. var optionOrient = this.get('orient', true);
  141. var targetAxisIndexMap = this._targetAxisInfoMap = createHashMap();
  142. var hasAxisSpecified = this._fillSpecifiedTargetAxis(targetAxisIndexMap);
  143. if (hasAxisSpecified) {
  144. this._orient = optionOrient || this._makeAutoOrientByTargetAxis();
  145. } else {
  146. this._orient = optionOrient || 'horizontal';
  147. this._fillAutoTargetAxisByOrient(targetAxisIndexMap, this._orient);
  148. }
  149. this._noTarget = true;
  150. targetAxisIndexMap.each(function (axisInfo) {
  151. if (axisInfo.indexList.length) {
  152. this._noTarget = false;
  153. }
  154. }, this);
  155. };
  156. DataZoomModel.prototype._fillSpecifiedTargetAxis = function (targetAxisIndexMap) {
  157. var hasAxisSpecified = false;
  158. each(DATA_ZOOM_AXIS_DIMENSIONS, function (axisDim) {
  159. var refering = this.getReferringComponents(getAxisMainType(axisDim), MULTIPLE_REFERRING); // When user set axisIndex as a empty array, we think that user specify axisIndex
  160. // but do not want use auto mode. Because empty array may be encountered when
  161. // some error occured.
  162. if (!refering.specified) {
  163. return;
  164. }
  165. hasAxisSpecified = true;
  166. var axisInfo = new DataZoomAxisInfo();
  167. each(refering.models, function (axisModel) {
  168. axisInfo.add(axisModel.componentIndex);
  169. });
  170. targetAxisIndexMap.set(axisDim, axisInfo);
  171. }, this);
  172. return hasAxisSpecified;
  173. };
  174. DataZoomModel.prototype._fillAutoTargetAxisByOrient = function (targetAxisIndexMap, orient) {
  175. var ecModel = this.ecModel;
  176. var needAuto = true; // Find axis that parallel to dataZoom as default.
  177. if (needAuto) {
  178. var axisDim = orient === 'vertical' ? 'y' : 'x';
  179. var axisModels = ecModel.findComponents({
  180. mainType: axisDim + 'Axis'
  181. });
  182. setParallelAxis(axisModels, axisDim);
  183. } // Find axis that parallel to dataZoom as default.
  184. if (needAuto) {
  185. var axisModels = ecModel.findComponents({
  186. mainType: 'singleAxis',
  187. filter: function (axisModel) {
  188. return axisModel.get('orient', true) === orient;
  189. }
  190. });
  191. setParallelAxis(axisModels, 'single');
  192. }
  193. function setParallelAxis(axisModels, axisDim) {
  194. // At least use the first parallel axis as the target axis.
  195. var axisModel = axisModels[0];
  196. if (!axisModel) {
  197. return;
  198. }
  199. var axisInfo = new DataZoomAxisInfo();
  200. axisInfo.add(axisModel.componentIndex);
  201. targetAxisIndexMap.set(axisDim, axisInfo);
  202. needAuto = false; // 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; // Find the first axis
  235. this.eachTargetAxis(function (axisDim) {
  236. !dim && (dim = axisDim);
  237. }, this);
  238. return dim === 'y' ? 'vertical' : 'horizontal';
  239. };
  240. DataZoomModel.prototype._setDefaultThrottle = function (inputRawOption) {
  241. // When first time user set throttle, auto throttle ends.
  242. if (inputRawOption.hasOwnProperty('throttle')) {
  243. this._autoThrottle = false;
  244. }
  245. if (this._autoThrottle) {
  246. var globalOption = this.ecModel.option;
  247. this.option.throttle = globalOption.animation && globalOption.animationDurationUpdate > 0 ? 100 : 20;
  248. }
  249. };
  250. DataZoomModel.prototype._updateRangeUse = function (inputRawOption) {
  251. var rangePropMode = this._rangePropMode;
  252. var rangeModeInOption = this.get('rangeMode');
  253. each([['start', 'startValue'], ['end', 'endValue']], function (names, index) {
  254. var percentSpecified = inputRawOption[names[0]] != null;
  255. var valueSpecified = inputRawOption[names[1]] != null;
  256. if (percentSpecified && !valueSpecified) {
  257. rangePropMode[index] = 'percent';
  258. } else if (!percentSpecified && valueSpecified) {
  259. rangePropMode[index] = 'value';
  260. } else if (rangeModeInOption) {
  261. rangePropMode[index] = rangeModeInOption[index];
  262. } else if (percentSpecified) {
  263. // percentSpecified && valueSpecified
  264. rangePropMode[index] = 'percent';
  265. } // else remain its original setting.
  266. });
  267. };
  268. DataZoomModel.prototype.noTarget = function () {
  269. return this._noTarget;
  270. };
  271. DataZoomModel.prototype.getFirstTargetAxisModel = function () {
  272. var firstAxisModel;
  273. this.eachTargetAxis(function (axisDim, axisIndex) {
  274. if (firstAxisModel == null) {
  275. firstAxisModel = this.ecModel.getComponent(getAxisMainType(axisDim), axisIndex);
  276. }
  277. }, this);
  278. return firstAxisModel;
  279. };
  280. /**
  281. * @param {Function} callback param: axisModel, dimNames, axisIndex, dataZoomModel, ecModel
  282. */
  283. DataZoomModel.prototype.eachTargetAxis = function (callback, context) {
  284. this._targetAxisInfoMap.each(function (axisInfo, axisDim) {
  285. each(axisInfo.indexList, function (axisIndex) {
  286. callback.call(context, axisDim, axisIndex);
  287. });
  288. });
  289. };
  290. /**
  291. * @return If not found, return null/undefined.
  292. */
  293. DataZoomModel.prototype.getAxisProxy = function (axisDim, axisIndex) {
  294. var axisModel = this.getAxisModel(axisDim, axisIndex);
  295. if (axisModel) {
  296. return axisModel.__dzAxisProxy;
  297. }
  298. };
  299. /**
  300. * @return If not found, return null/undefined.
  301. */
  302. DataZoomModel.prototype.getAxisModel = function (axisDim, axisIndex) {
  303. if (process.env.NODE_ENV !== 'production') {
  304. assert(axisDim && axisIndex != null);
  305. }
  306. var axisInfo = this._targetAxisInfoMap.get(axisDim);
  307. if (axisInfo && axisInfo.indexMap[axisIndex]) {
  308. return this.ecModel.getComponent(getAxisMainType(axisDim), axisIndex);
  309. }
  310. };
  311. /**
  312. * If not specified, set to undefined.
  313. */
  314. DataZoomModel.prototype.setRawRange = function (opt) {
  315. var thisOption = this.option;
  316. var settledOption = this.settledOption;
  317. each([['start', 'startValue'], ['end', 'endValue']], function (names) {
  318. // Consider the pair <start, startValue>:
  319. // If one has value and the other one is `null/undefined`, we both set them
  320. // to `settledOption`. This strategy enables the feature to clear the original
  321. // value in `settledOption` to `null/undefined`.
  322. // But if both of them are `null/undefined`, we do not set them to `settledOption`
  323. // and keep `settledOption` with the original value. This strategy enables users to
  324. // only set <end or endValue> but not set <start or startValue> when calling
  325. // `dispatchAction`.
  326. // The pair <end, endValue> is treated in the same way.
  327. if (opt[names[0]] != null || opt[names[1]] != null) {
  328. thisOption[names[0]] = settledOption[names[0]] = opt[names[0]];
  329. thisOption[names[1]] = settledOption[names[1]] = opt[names[1]];
  330. }
  331. }, this);
  332. this._updateRangeUse(opt);
  333. };
  334. DataZoomModel.prototype.setCalculatedRange = function (opt) {
  335. var option = this.option;
  336. each(['start', 'startValue', 'end', 'endValue'], function (name) {
  337. option[name] = opt[name];
  338. });
  339. };
  340. DataZoomModel.prototype.getPercentRange = function () {
  341. var axisProxy = this.findRepresentativeAxisProxy();
  342. if (axisProxy) {
  343. return axisProxy.getDataPercentWindow();
  344. }
  345. };
  346. /**
  347. * For example, chart.getModel().getComponent('dataZoom').getValueRange('y', 0);
  348. *
  349. * @return [startValue, endValue] value can only be '-' or finite number.
  350. */
  351. DataZoomModel.prototype.getValueRange = function (axisDim, axisIndex) {
  352. if (axisDim == null && axisIndex == null) {
  353. var axisProxy = this.findRepresentativeAxisProxy();
  354. if (axisProxy) {
  355. return axisProxy.getDataValueWindow();
  356. }
  357. } else {
  358. return this.getAxisProxy(axisDim, axisIndex).getDataValueWindow();
  359. }
  360. };
  361. /**
  362. * @param axisModel If axisModel given, find axisProxy
  363. * corresponding to the axisModel
  364. */
  365. DataZoomModel.prototype.findRepresentativeAxisProxy = function (axisModel) {
  366. if (axisModel) {
  367. return axisModel.__dzAxisProxy;
  368. } // Find the first hosted axisProxy
  369. var firstProxy;
  370. var axisDimList = this._targetAxisInfoMap.keys();
  371. for (var i = 0; i < axisDimList.length; i++) {
  372. var axisDim = axisDimList[i];
  373. var axisInfo = this._targetAxisInfoMap.get(axisDim);
  374. for (var j = 0; j < axisInfo.indexList.length; j++) {
  375. var proxy = this.getAxisProxy(axisDim, axisInfo.indexList[j]);
  376. if (proxy.hostedBy(this)) {
  377. return proxy;
  378. }
  379. if (!firstProxy) {
  380. firstProxy = proxy;
  381. }
  382. }
  383. } // If no hosted proxy found, still need to return a proxy.
  384. // This case always happens in toolbox dataZoom, where axes are all hosted by
  385. // other dataZooms.
  386. return firstProxy;
  387. };
  388. DataZoomModel.prototype.getRangePropMode = function () {
  389. return this._rangePropMode.slice();
  390. };
  391. DataZoomModel.prototype.getOrient = function () {
  392. if (process.env.NODE_ENV !== 'production') {
  393. // Should not be called before initialized.
  394. assert(this._orient);
  395. }
  396. return this._orient;
  397. };
  398. DataZoomModel.type = 'dataZoom';
  399. DataZoomModel.dependencies = ['xAxis', 'yAxis', 'radiusAxis', 'angleAxis', 'singleAxis', 'series', 'toolbox'];
  400. DataZoomModel.defaultOption = {
  401. zlevel: 0,
  402. z: 4,
  403. filterMode: 'filter',
  404. start: 0,
  405. end: 100
  406. };
  407. return DataZoomModel;
  408. }(ComponentModel);
  409. /**
  410. * Retrieve the those raw params from option, which will be cached separately.
  411. * becasue they will be overwritten by normalized/calculated values in the main
  412. * process.
  413. */
  414. function retrieveRawOption(option) {
  415. var ret = {};
  416. each(['start', 'end', 'startValue', 'endValue', 'throttle'], function (name) {
  417. option.hasOwnProperty(name) && (ret[name] = option[name]);
  418. });
  419. return ret;
  420. }
  421. export default DataZoomModel;