BrushTargetManager.js 14 KB

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