DataZoom.js 9.4 KB

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