DataZoom.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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"; // TODO depends on DataZoom and Brush
  41. import * as zrUtil from 'zrender/lib/core/util.js';
  42. import BrushController from '../../helper/BrushController.js';
  43. import BrushTargetManager from '../../helper/BrushTargetManager.js';
  44. import * as history from '../../dataZoom/history.js';
  45. import sliderMove from '../../helper/sliderMove.js';
  46. import { ToolboxFeature } from '../featureManager.js';
  47. import { makeInternalComponentId, parseFinder } from '../../../util/model.js';
  48. import { registerInternalOptionCreator } from '../../../model/internalComponentCreator.js';
  49. var each = zrUtil.each;
  50. var DATA_ZOOM_ID_BASE = makeInternalComponentId('toolbox-dataZoom_');
  51. var ICON_TYPES = ['zoom', 'back'];
  52. var DataZoomFeature =
  53. /** @class */
  54. function (_super) {
  55. __extends(DataZoomFeature, _super);
  56. function DataZoomFeature() {
  57. return _super !== null && _super.apply(this, arguments) || this;
  58. }
  59. DataZoomFeature.prototype.render = function (featureModel, ecModel, api, payload) {
  60. if (!this._brushController) {
  61. this._brushController = new BrushController(api.getZr());
  62. this._brushController.on('brush', zrUtil.bind(this._onBrush, this)).mount();
  63. }
  64. updateZoomBtnStatus(featureModel, ecModel, this, payload, api);
  65. updateBackBtnStatus(featureModel, ecModel);
  66. };
  67. DataZoomFeature.prototype.onclick = function (ecModel, api, type) {
  68. handlers[type].call(this);
  69. };
  70. DataZoomFeature.prototype.remove = function (ecModel, api) {
  71. this._brushController && this._brushController.unmount();
  72. };
  73. DataZoomFeature.prototype.dispose = function (ecModel, api) {
  74. this._brushController && this._brushController.dispose();
  75. };
  76. DataZoomFeature.prototype._onBrush = function (eventParam) {
  77. var areas = eventParam.areas;
  78. if (!eventParam.isEnd || !areas.length) {
  79. return;
  80. }
  81. var snapshot = {};
  82. var ecModel = this.ecModel;
  83. this._brushController.updateCovers([]); // remove cover
  84. var brushTargetManager = new BrushTargetManager(makeAxisFinder(this.model), ecModel, {
  85. include: ['grid']
  86. });
  87. brushTargetManager.matchOutputRanges(areas, ecModel, function (area, coordRange, coordSys) {
  88. if (coordSys.type !== 'cartesian2d') {
  89. return;
  90. }
  91. var brushType = area.brushType;
  92. if (brushType === 'rect') {
  93. setBatch('x', coordSys, coordRange[0]);
  94. setBatch('y', coordSys, coordRange[1]);
  95. } else {
  96. setBatch({
  97. lineX: 'x',
  98. lineY: 'y'
  99. }[brushType], coordSys, coordRange);
  100. }
  101. });
  102. history.push(ecModel, snapshot);
  103. this._dispatchZoomAction(snapshot);
  104. function setBatch(dimName, coordSys, minMax) {
  105. var axis = coordSys.getAxis(dimName);
  106. var axisModel = axis.model;
  107. var dataZoomModel = findDataZoom(dimName, axisModel, ecModel); // Restrict range.
  108. var minMaxSpan = dataZoomModel.findRepresentativeAxisProxy(axisModel).getMinMaxSpan();
  109. if (minMaxSpan.minValueSpan != null || minMaxSpan.maxValueSpan != null) {
  110. minMax = sliderMove(0, minMax.slice(), axis.scale.getExtent(), 0, minMaxSpan.minValueSpan, minMaxSpan.maxValueSpan);
  111. }
  112. dataZoomModel && (snapshot[dataZoomModel.id] = {
  113. dataZoomId: dataZoomModel.id,
  114. startValue: minMax[0],
  115. endValue: minMax[1]
  116. });
  117. }
  118. function findDataZoom(dimName, axisModel, ecModel) {
  119. var found;
  120. ecModel.eachComponent({
  121. mainType: 'dataZoom',
  122. subType: 'select'
  123. }, function (dzModel) {
  124. var has = dzModel.getAxisModel(dimName, axisModel.componentIndex);
  125. has && (found = dzModel);
  126. });
  127. return found;
  128. }
  129. };
  130. ;
  131. DataZoomFeature.prototype._dispatchZoomAction = function (snapshot) {
  132. var batch = []; // Convert from hash map to array.
  133. each(snapshot, function (batchItem, dataZoomId) {
  134. batch.push(zrUtil.clone(batchItem));
  135. });
  136. batch.length && this.api.dispatchAction({
  137. type: 'dataZoom',
  138. from: this.uid,
  139. batch: batch
  140. });
  141. };
  142. DataZoomFeature.getDefaultOption = function (ecModel) {
  143. var defaultOption = {
  144. show: true,
  145. filterMode: 'filter',
  146. // Icon group
  147. icon: {
  148. zoom: 'M0,13.5h26.9 M13.5,26.9V0 M32.1,13.5H58V58H13.5 V32.1',
  149. back: 'M22,1.4L9.9,13.5l12.3,12.3 M10.3,13.5H54.9v44.6 H10.3v-26'
  150. },
  151. // `zoom`, `back`
  152. title: ecModel.getLocaleModel().get(['toolbox', 'dataZoom', 'title']),
  153. brushStyle: {
  154. borderWidth: 0,
  155. color: 'rgba(210,219,238,0.2)'
  156. }
  157. };
  158. return defaultOption;
  159. };
  160. return DataZoomFeature;
  161. }(ToolboxFeature);
  162. var handlers = {
  163. zoom: function () {
  164. var nextActive = !this._isZoomActive;
  165. this.api.dispatchAction({
  166. type: 'takeGlobalCursor',
  167. key: 'dataZoomSelect',
  168. dataZoomSelectActive: nextActive
  169. });
  170. },
  171. back: function () {
  172. this._dispatchZoomAction(history.pop(this.ecModel));
  173. }
  174. };
  175. function makeAxisFinder(dzFeatureModel) {
  176. var setting = {
  177. xAxisIndex: dzFeatureModel.get('xAxisIndex', true),
  178. yAxisIndex: dzFeatureModel.get('yAxisIndex', true),
  179. xAxisId: dzFeatureModel.get('xAxisId', true),
  180. yAxisId: dzFeatureModel.get('yAxisId', true)
  181. }; // If both `xAxisIndex` `xAxisId` not set, it means 'all'.
  182. // If both `yAxisIndex` `yAxisId` not set, it means 'all'.
  183. // Some old cases set like this below to close yAxis control but leave xAxis control:
  184. // `{ feature: { dataZoom: { yAxisIndex: false } }`.
  185. if (setting.xAxisIndex == null && setting.xAxisId == null) {
  186. setting.xAxisIndex = 'all';
  187. }
  188. if (setting.yAxisIndex == null && setting.yAxisId == null) {
  189. setting.yAxisIndex = 'all';
  190. }
  191. return setting;
  192. }
  193. function updateBackBtnStatus(featureModel, ecModel) {
  194. featureModel.setIconStatus('back', history.count(ecModel) > 1 ? 'emphasis' : 'normal');
  195. }
  196. function updateZoomBtnStatus(featureModel, ecModel, view, payload, api) {
  197. var zoomActive = view._isZoomActive;
  198. if (payload && payload.type === 'takeGlobalCursor') {
  199. zoomActive = payload.key === 'dataZoomSelect' ? payload.dataZoomSelectActive : false;
  200. }
  201. view._isZoomActive = zoomActive;
  202. featureModel.setIconStatus('zoom', zoomActive ? 'emphasis' : 'normal');
  203. var brushTargetManager = new BrushTargetManager(makeAxisFinder(featureModel), ecModel, {
  204. include: ['grid']
  205. });
  206. var panels = brushTargetManager.makePanelOpts(api, function (targetInfo) {
  207. return targetInfo.xAxisDeclared && !targetInfo.yAxisDeclared ? 'lineX' : !targetInfo.xAxisDeclared && targetInfo.yAxisDeclared ? 'lineY' : 'rect';
  208. });
  209. view._brushController.setPanels(panels).enableBrush(zoomActive && panels.length ? {
  210. brushType: 'auto',
  211. brushStyle: featureModel.getModel('brushStyle').getItemStyle()
  212. } : false);
  213. }
  214. registerInternalOptionCreator('dataZoom', function (ecModel) {
  215. var toolboxModel = ecModel.getComponent('toolbox', 0);
  216. var featureDataZoomPath = ['feature', 'dataZoom'];
  217. if (!toolboxModel || toolboxModel.get(featureDataZoomPath) == null) {
  218. return;
  219. }
  220. var dzFeatureModel = toolboxModel.getModel(featureDataZoomPath);
  221. var dzOptions = [];
  222. var finder = makeAxisFinder(dzFeatureModel);
  223. var finderResult = parseFinder(ecModel, finder);
  224. each(finderResult.xAxisModels, function (axisModel) {
  225. return buildInternalOptions(axisModel, 'xAxis', 'xAxisIndex');
  226. });
  227. each(finderResult.yAxisModels, function (axisModel) {
  228. return buildInternalOptions(axisModel, 'yAxis', 'yAxisIndex');
  229. });
  230. function buildInternalOptions(axisModel, axisMainType, axisIndexPropName) {
  231. var axisIndex = axisModel.componentIndex;
  232. var newOpt = {
  233. type: 'select',
  234. $fromToolbox: true,
  235. // Default to be filter
  236. filterMode: dzFeatureModel.get('filterMode', true) || 'filter',
  237. // Id for merge mapping.
  238. id: DATA_ZOOM_ID_BASE + axisMainType + axisIndex
  239. };
  240. newOpt[axisIndexPropName] = axisIndex;
  241. dzOptions.push(newOpt);
  242. }
  243. return dzOptions;
  244. });
  245. export default DataZoomFeature;