BrushTargetManager.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 { each, indexOf, curry, assert, map, createHashMap } from 'zrender/lib/core/util.js';
  41. import * as graphic from '../../util/graphic.js';
  42. import * as brushHelper from './brushHelper.js';
  43. import { parseFinder as modelUtilParseFinder } from '../../util/model.js';
  44. // FIXME
  45. // how to genarialize to more coordinate systems.
  46. var INCLUDE_FINDER_MAIN_TYPES = ['grid', 'xAxis', 'yAxis', 'geo', 'graph', 'polar', 'radiusAxis', 'angleAxis', 'bmap'];
  47. var BrushTargetManager = /** @class */function () {
  48. /**
  49. * @param finder contains Index/Id/Name of xAxis/yAxis/geo/grid
  50. * Each can be {number|Array.<number>}. like: {xAxisIndex: [3, 4]}
  51. * @param opt.include include coordinate system types.
  52. */
  53. function BrushTargetManager(finder, ecModel, opt) {
  54. var _this = this;
  55. this._targetInfoList = [];
  56. var foundCpts = parseFinder(ecModel, finder);
  57. each(targetInfoBuilders, function (builder, type) {
  58. if (!opt || !opt.include || indexOf(opt.include, type) >= 0) {
  59. builder(foundCpts, _this._targetInfoList);
  60. }
  61. });
  62. }
  63. BrushTargetManager.prototype.setOutputRanges = function (areas, ecModel) {
  64. this.matchOutputRanges(areas, ecModel, function (area, coordRange, coordSys) {
  65. (area.coordRanges || (area.coordRanges = [])).push(coordRange);
  66. // area.coordRange is the first of area.coordRanges
  67. if (!area.coordRange) {
  68. area.coordRange = coordRange;
  69. // In 'category' axis, coord to pixel is not reversible, so we can not
  70. // rebuild range by coordRange accrately, which may bring trouble when
  71. // brushing only one item. So we use __rangeOffset to rebuilding range
  72. // by coordRange. And this it only used in brush component so it is no
  73. // need to be adapted to coordRanges.
  74. var result = coordConvert[area.brushType](0, coordSys, coordRange);
  75. area.__rangeOffset = {
  76. offset: diffProcessor[area.brushType](result.values, area.range, [1, 1]),
  77. xyMinMax: result.xyMinMax
  78. };
  79. }
  80. });
  81. return areas;
  82. };
  83. BrushTargetManager.prototype.matchOutputRanges = function (areas, ecModel, cb) {
  84. each(areas, function (area) {
  85. var targetInfo = this.findTargetInfo(area, ecModel);
  86. if (targetInfo && targetInfo !== true) {
  87. each(targetInfo.coordSyses, function (coordSys) {
  88. var result = coordConvert[area.brushType](1, coordSys, area.range, true);
  89. cb(area, result.values, coordSys, ecModel);
  90. });
  91. }
  92. }, this);
  93. };
  94. /**
  95. * the `areas` is `BrushModel.areas`.
  96. * Called in layout stage.
  97. * convert `area.coordRange` to global range and set panelId to `area.range`.
  98. */
  99. BrushTargetManager.prototype.setInputRanges = function (areas, ecModel) {
  100. each(areas, function (area) {
  101. var targetInfo = this.findTargetInfo(area, ecModel);
  102. if (process.env.NODE_ENV !== 'production') {
  103. assert(!targetInfo || targetInfo === true || area.coordRange, 'coordRange must be specified when coord index specified.');
  104. assert(!targetInfo || targetInfo !== true || area.range, 'range must be specified in global brush.');
  105. }
  106. area.range = area.range || [];
  107. // convert coordRange to global range and set panelId.
  108. if (targetInfo && targetInfo !== true) {
  109. area.panelId = targetInfo.panelId;
  110. // (1) area.range should always be calculate from coordRange but does
  111. // not keep its original value, for the sake of the dataZoom scenario,
  112. // where area.coordRange remains unchanged but area.range may be changed.
  113. // (2) Only support converting one coordRange to pixel range in brush
  114. // component. So do not consider `coordRanges`.
  115. // (3) About __rangeOffset, see comment above.
  116. var result = coordConvert[area.brushType](0, targetInfo.coordSys, area.coordRange);
  117. var rangeOffset = area.__rangeOffset;
  118. area.range = rangeOffset ? diffProcessor[area.brushType](result.values, rangeOffset.offset, getScales(result.xyMinMax, rangeOffset.xyMinMax)) : result.values;
  119. }
  120. }, this);
  121. };
  122. BrushTargetManager.prototype.makePanelOpts = function (api, getDefaultBrushType) {
  123. return map(this._targetInfoList, function (targetInfo) {
  124. var rect = targetInfo.getPanelRect();
  125. return {
  126. panelId: targetInfo.panelId,
  127. defaultBrushType: getDefaultBrushType ? getDefaultBrushType(targetInfo) : null,
  128. clipPath: brushHelper.makeRectPanelClipPath(rect),
  129. isTargetByCursor: brushHelper.makeRectIsTargetByCursor(rect, api, targetInfo.coordSysModel),
  130. getLinearBrushOtherExtent: brushHelper.makeLinearBrushOtherExtent(rect)
  131. };
  132. });
  133. };
  134. BrushTargetManager.prototype.controlSeries = function (area, seriesModel, ecModel) {
  135. // Check whether area is bound in coord, and series do not belong to that coord.
  136. // If do not do this check, some brush (like lineX) will controll all axes.
  137. var targetInfo = this.findTargetInfo(area, ecModel);
  138. return targetInfo === true || targetInfo && indexOf(targetInfo.coordSyses, seriesModel.coordinateSystem) >= 0;
  139. };
  140. /**
  141. * If return Object, a coord found.
  142. * If return true, global found.
  143. * Otherwise nothing found.
  144. */
  145. BrushTargetManager.prototype.findTargetInfo = function (area, ecModel) {
  146. var targetInfoList = this._targetInfoList;
  147. var foundCpts = parseFinder(ecModel, area);
  148. for (var i = 0; i < targetInfoList.length; i++) {
  149. var targetInfo = targetInfoList[i];
  150. var areaPanelId = area.panelId;
  151. if (areaPanelId) {
  152. if (targetInfo.panelId === areaPanelId) {
  153. return targetInfo;
  154. }
  155. } else {
  156. for (var j = 0; j < targetInfoMatchers.length; j++) {
  157. if (targetInfoMatchers[j](foundCpts, targetInfo)) {
  158. return targetInfo;
  159. }
  160. }
  161. }
  162. }
  163. return true;
  164. };
  165. return BrushTargetManager;
  166. }();
  167. function formatMinMax(minMax) {
  168. minMax[0] > minMax[1] && minMax.reverse();
  169. return minMax;
  170. }
  171. function parseFinder(ecModel, finder) {
  172. return modelUtilParseFinder(ecModel, finder, {
  173. includeMainTypes: INCLUDE_FINDER_MAIN_TYPES
  174. });
  175. }
  176. var targetInfoBuilders = {
  177. grid: function (foundCpts, targetInfoList) {
  178. var xAxisModels = foundCpts.xAxisModels;
  179. var yAxisModels = foundCpts.yAxisModels;
  180. var gridModels = foundCpts.gridModels;
  181. // Remove duplicated.
  182. var gridModelMap = createHashMap();
  183. var xAxesHas = {};
  184. var yAxesHas = {};
  185. if (!xAxisModels && !yAxisModels && !gridModels) {
  186. return;
  187. }
  188. each(xAxisModels, function (axisModel) {
  189. var gridModel = axisModel.axis.grid.model;
  190. gridModelMap.set(gridModel.id, gridModel);
  191. xAxesHas[gridModel.id] = true;
  192. });
  193. each(yAxisModels, function (axisModel) {
  194. var gridModel = axisModel.axis.grid.model;
  195. gridModelMap.set(gridModel.id, gridModel);
  196. yAxesHas[gridModel.id] = true;
  197. });
  198. each(gridModels, function (gridModel) {
  199. gridModelMap.set(gridModel.id, gridModel);
  200. xAxesHas[gridModel.id] = true;
  201. yAxesHas[gridModel.id] = true;
  202. });
  203. gridModelMap.each(function (gridModel) {
  204. var grid = gridModel.coordinateSystem;
  205. var cartesians = [];
  206. each(grid.getCartesians(), function (cartesian, index) {
  207. if (indexOf(xAxisModels, cartesian.getAxis('x').model) >= 0 || indexOf(yAxisModels, cartesian.getAxis('y').model) >= 0) {
  208. cartesians.push(cartesian);
  209. }
  210. });
  211. targetInfoList.push({
  212. panelId: 'grid--' + gridModel.id,
  213. gridModel: gridModel,
  214. coordSysModel: gridModel,
  215. // Use the first one as the representitive coordSys.
  216. coordSys: cartesians[0],
  217. coordSyses: cartesians,
  218. getPanelRect: panelRectBuilders.grid,
  219. xAxisDeclared: xAxesHas[gridModel.id],
  220. yAxisDeclared: yAxesHas[gridModel.id]
  221. });
  222. });
  223. },
  224. geo: function (foundCpts, targetInfoList) {
  225. each(foundCpts.geoModels, function (geoModel) {
  226. var coordSys = geoModel.coordinateSystem;
  227. targetInfoList.push({
  228. panelId: 'geo--' + geoModel.id,
  229. geoModel: geoModel,
  230. coordSysModel: geoModel,
  231. coordSys: coordSys,
  232. coordSyses: [coordSys],
  233. getPanelRect: panelRectBuilders.geo
  234. });
  235. });
  236. }
  237. };
  238. var targetInfoMatchers = [
  239. // grid
  240. function (foundCpts, targetInfo) {
  241. var xAxisModel = foundCpts.xAxisModel;
  242. var yAxisModel = foundCpts.yAxisModel;
  243. var gridModel = foundCpts.gridModel;
  244. !gridModel && xAxisModel && (gridModel = xAxisModel.axis.grid.model);
  245. !gridModel && yAxisModel && (gridModel = yAxisModel.axis.grid.model);
  246. return gridModel && gridModel === targetInfo.gridModel;
  247. },
  248. // geo
  249. function (foundCpts, targetInfo) {
  250. var geoModel = foundCpts.geoModel;
  251. return geoModel && geoModel === targetInfo.geoModel;
  252. }];
  253. var panelRectBuilders = {
  254. grid: function () {
  255. // grid is not Transformable.
  256. return this.coordSys.master.getRect().clone();
  257. },
  258. geo: function () {
  259. var coordSys = this.coordSys;
  260. var rect = coordSys.getBoundingRect().clone();
  261. // geo roam and zoom transform
  262. rect.applyTransform(graphic.getTransform(coordSys));
  263. return rect;
  264. }
  265. };
  266. var coordConvert = {
  267. lineX: curry(axisConvert, 0),
  268. lineY: curry(axisConvert, 1),
  269. rect: function (to, coordSys, rangeOrCoordRange, clamp) {
  270. var xminymin = to ? coordSys.pointToData([rangeOrCoordRange[0][0], rangeOrCoordRange[1][0]], clamp) : coordSys.dataToPoint([rangeOrCoordRange[0][0], rangeOrCoordRange[1][0]], clamp);
  271. var xmaxymax = to ? coordSys.pointToData([rangeOrCoordRange[0][1], rangeOrCoordRange[1][1]], clamp) : coordSys.dataToPoint([rangeOrCoordRange[0][1], rangeOrCoordRange[1][1]], clamp);
  272. var values = [formatMinMax([xminymin[0], xmaxymax[0]]), formatMinMax([xminymin[1], xmaxymax[1]])];
  273. return {
  274. values: values,
  275. xyMinMax: values
  276. };
  277. },
  278. polygon: function (to, coordSys, rangeOrCoordRange, clamp) {
  279. var xyMinMax = [[Infinity, -Infinity], [Infinity, -Infinity]];
  280. var values = map(rangeOrCoordRange, function (item) {
  281. var p = to ? coordSys.pointToData(item, clamp) : coordSys.dataToPoint(item, clamp);
  282. xyMinMax[0][0] = Math.min(xyMinMax[0][0], p[0]);
  283. xyMinMax[1][0] = Math.min(xyMinMax[1][0], p[1]);
  284. xyMinMax[0][1] = Math.max(xyMinMax[0][1], p[0]);
  285. xyMinMax[1][1] = Math.max(xyMinMax[1][1], p[1]);
  286. return p;
  287. });
  288. return {
  289. values: values,
  290. xyMinMax: xyMinMax
  291. };
  292. }
  293. };
  294. function axisConvert(axisNameIndex, to, coordSys, rangeOrCoordRange) {
  295. if (process.env.NODE_ENV !== 'production') {
  296. assert(coordSys.type === 'cartesian2d', 'lineX/lineY brush is available only in cartesian2d.');
  297. }
  298. var axis = coordSys.getAxis(['x', 'y'][axisNameIndex]);
  299. var values = formatMinMax(map([0, 1], function (i) {
  300. return to ? axis.coordToData(axis.toLocalCoord(rangeOrCoordRange[i]), true) : axis.toGlobalCoord(axis.dataToCoord(rangeOrCoordRange[i]));
  301. }));
  302. var xyMinMax = [];
  303. xyMinMax[axisNameIndex] = values;
  304. xyMinMax[1 - axisNameIndex] = [NaN, NaN];
  305. return {
  306. values: values,
  307. xyMinMax: xyMinMax
  308. };
  309. }
  310. var diffProcessor = {
  311. lineX: curry(axisDiffProcessor, 0),
  312. lineY: curry(axisDiffProcessor, 1),
  313. rect: function (values, refer, scales) {
  314. return [[values[0][0] - scales[0] * refer[0][0], values[0][1] - scales[0] * refer[0][1]], [values[1][0] - scales[1] * refer[1][0], values[1][1] - scales[1] * refer[1][1]]];
  315. },
  316. polygon: function (values, refer, scales) {
  317. return map(values, function (item, idx) {
  318. return [item[0] - scales[0] * refer[idx][0], item[1] - scales[1] * refer[idx][1]];
  319. });
  320. }
  321. };
  322. function axisDiffProcessor(axisNameIndex, values, refer, scales) {
  323. return [values[0] - scales[axisNameIndex] * refer[0], values[1] - scales[axisNameIndex] * refer[1]];
  324. }
  325. // We have to process scale caused by dataZoom manually,
  326. // although it might be not accurate.
  327. // Return [0~1, 0~1]
  328. function getScales(xyMinMaxCurr, xyMinMaxOrigin) {
  329. var sizeCurr = getSize(xyMinMaxCurr);
  330. var sizeOrigin = getSize(xyMinMaxOrigin);
  331. var scales = [sizeCurr[0] / sizeOrigin[0], sizeCurr[1] / sizeOrigin[1]];
  332. isNaN(scales[0]) && (scales[0] = 1);
  333. isNaN(scales[1]) && (scales[1] = 1);
  334. return scales;
  335. }
  336. function getSize(xyMinMax) {
  337. return xyMinMax ? [xyMinMax[0][1] - xyMinMax[0][0], xyMinMax[1][1] - xyMinMax[1][0]] : [NaN, NaN];
  338. }
  339. export default BrushTargetManager;