visualEncoding.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. var echarts = require("../../echarts");
  20. var zrUtil = require("zrender/lib/core/util");
  21. var BoundingRect = require("zrender/lib/core/BoundingRect");
  22. var visualSolution = require("../../visual/visualSolution");
  23. var selector = require("./selector");
  24. var throttleUtil = require("../../util/throttle");
  25. var BrushTargetManager = require("../helper/BrushTargetManager");
  26. /*
  27. * Licensed to the Apache Software Foundation (ASF) under one
  28. * or more contributor license agreements. See the NOTICE file
  29. * distributed with this work for additional information
  30. * regarding copyright ownership. The ASF licenses this file
  31. * to you under the Apache License, Version 2.0 (the
  32. * "License"); you may not use this file except in compliance
  33. * with the License. You may obtain a copy of the License at
  34. *
  35. * http://www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing,
  38. * software distributed under the License is distributed on an
  39. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  40. * KIND, either express or implied. See the License for the
  41. * specific language governing permissions and limitations
  42. * under the License.
  43. */
  44. var STATE_LIST = ['inBrush', 'outOfBrush'];
  45. var DISPATCH_METHOD = '__ecBrushSelect';
  46. var DISPATCH_FLAG = '__ecInBrushSelectEvent';
  47. var PRIORITY_BRUSH = echarts.PRIORITY.VISUAL.BRUSH;
  48. /**
  49. * Layout for visual, the priority higher than other layout, and before brush visual.
  50. */
  51. echarts.registerLayout(PRIORITY_BRUSH, function (ecModel, api, payload) {
  52. ecModel.eachComponent({
  53. mainType: 'brush'
  54. }, function (brushModel) {
  55. payload && payload.type === 'takeGlobalCursor' && brushModel.setBrushOption(payload.key === 'brush' ? payload.brushOption : {
  56. brushType: false
  57. });
  58. });
  59. layoutCovers(ecModel);
  60. });
  61. function layoutCovers(ecModel) {
  62. ecModel.eachComponent({
  63. mainType: 'brush'
  64. }, function (brushModel) {
  65. var brushTargetManager = brushModel.brushTargetManager = new BrushTargetManager(brushModel.option, ecModel);
  66. brushTargetManager.setInputRanges(brushModel.areas, ecModel);
  67. });
  68. }
  69. /**
  70. * Register the visual encoding if this modules required.
  71. */
  72. echarts.registerVisual(PRIORITY_BRUSH, function (ecModel, api, payload) {
  73. var brushSelected = [];
  74. var throttleType;
  75. var throttleDelay;
  76. ecModel.eachComponent({
  77. mainType: 'brush'
  78. }, function (brushModel, brushIndex) {
  79. var thisBrushSelected = {
  80. brushId: brushModel.id,
  81. brushIndex: brushIndex,
  82. brushName: brushModel.name,
  83. areas: zrUtil.clone(brushModel.areas),
  84. selected: []
  85. }; // Every brush component exists in event params, convenient
  86. // for user to find by index.
  87. brushSelected.push(thisBrushSelected);
  88. var brushOption = brushModel.option;
  89. var brushLink = brushOption.brushLink;
  90. var linkedSeriesMap = [];
  91. var selectedDataIndexForLink = [];
  92. var rangeInfoBySeries = [];
  93. var hasBrushExists = 0;
  94. if (!brushIndex) {
  95. // Only the first throttle setting works.
  96. throttleType = brushOption.throttleType;
  97. throttleDelay = brushOption.throttleDelay;
  98. } // Add boundingRect and selectors to range.
  99. var areas = zrUtil.map(brushModel.areas, function (area) {
  100. return bindSelector(zrUtil.defaults({
  101. boundingRect: boundingRectBuilders[area.brushType](area)
  102. }, area));
  103. });
  104. var visualMappings = visualSolution.createVisualMappings(brushModel.option, STATE_LIST, function (mappingOption) {
  105. mappingOption.mappingMethod = 'fixed';
  106. });
  107. zrUtil.isArray(brushLink) && zrUtil.each(brushLink, function (seriesIndex) {
  108. linkedSeriesMap[seriesIndex] = 1;
  109. });
  110. function linkOthers(seriesIndex) {
  111. return brushLink === 'all' || linkedSeriesMap[seriesIndex];
  112. } // If no supported brush or no brush on the series,
  113. // all visuals should be in original state.
  114. function brushed(rangeInfoList) {
  115. return !!rangeInfoList.length;
  116. }
  117. /**
  118. * Logic for each series: (If the logic has to be modified one day, do it carefully!)
  119. *
  120. * ( brushed ┬ && ┬hasBrushExist ┬ && linkOthers ) => StepA: ┬record, ┬ StepB: ┬visualByRecord.
  121. * !brushed┘ ├hasBrushExist ┤ └nothing,┘ ├visualByRecord.
  122. * └!hasBrushExist┘ └nothing.
  123. * ( !brushed && ┬hasBrushExist ┬ && linkOthers ) => StepA: nothing, StepB: ┬visualByRecord.
  124. * └!hasBrushExist┘ └nothing.
  125. * ( brushed ┬ && !linkOthers ) => StepA: nothing, StepB: ┬visualByCheck.
  126. * !brushed┘ └nothing.
  127. * ( !brushed && !linkOthers ) => StepA: nothing, StepB: nothing.
  128. */
  129. // Step A
  130. ecModel.eachSeries(function (seriesModel, seriesIndex) {
  131. var rangeInfoList = rangeInfoBySeries[seriesIndex] = [];
  132. seriesModel.subType === 'parallel' ? stepAParallel(seriesModel, seriesIndex, rangeInfoList) : stepAOthers(seriesModel, seriesIndex, rangeInfoList);
  133. });
  134. function stepAParallel(seriesModel, seriesIndex) {
  135. var coordSys = seriesModel.coordinateSystem;
  136. hasBrushExists |= coordSys.hasAxisBrushed();
  137. linkOthers(seriesIndex) && coordSys.eachActiveState(seriesModel.getData(), function (activeState, dataIndex) {
  138. activeState === 'active' && (selectedDataIndexForLink[dataIndex] = 1);
  139. });
  140. }
  141. function stepAOthers(seriesModel, seriesIndex, rangeInfoList) {
  142. var selectorsByBrushType = getSelectorsByBrushType(seriesModel);
  143. if (!selectorsByBrushType || brushModelNotControll(brushModel, seriesIndex)) {
  144. return;
  145. }
  146. zrUtil.each(areas, function (area) {
  147. selectorsByBrushType[area.brushType] && brushModel.brushTargetManager.controlSeries(area, seriesModel, ecModel) && rangeInfoList.push(area);
  148. hasBrushExists |= brushed(rangeInfoList);
  149. });
  150. if (linkOthers(seriesIndex) && brushed(rangeInfoList)) {
  151. var data = seriesModel.getData();
  152. data.each(function (dataIndex) {
  153. if (checkInRange(selectorsByBrushType, rangeInfoList, data, dataIndex)) {
  154. selectedDataIndexForLink[dataIndex] = 1;
  155. }
  156. });
  157. }
  158. } // Step B
  159. ecModel.eachSeries(function (seriesModel, seriesIndex) {
  160. var seriesBrushSelected = {
  161. seriesId: seriesModel.id,
  162. seriesIndex: seriesIndex,
  163. seriesName: seriesModel.name,
  164. dataIndex: []
  165. }; // Every series exists in event params, convenient
  166. // for user to find series by seriesIndex.
  167. thisBrushSelected.selected.push(seriesBrushSelected);
  168. var selectorsByBrushType = getSelectorsByBrushType(seriesModel);
  169. var rangeInfoList = rangeInfoBySeries[seriesIndex];
  170. var data = seriesModel.getData();
  171. var getValueState = linkOthers(seriesIndex) ? function (dataIndex) {
  172. return selectedDataIndexForLink[dataIndex] ? (seriesBrushSelected.dataIndex.push(data.getRawIndex(dataIndex)), 'inBrush') : 'outOfBrush';
  173. } : function (dataIndex) {
  174. return checkInRange(selectorsByBrushType, rangeInfoList, data, dataIndex) ? (seriesBrushSelected.dataIndex.push(data.getRawIndex(dataIndex)), 'inBrush') : 'outOfBrush';
  175. }; // If no supported brush or no brush, all visuals are in original state.
  176. (linkOthers(seriesIndex) ? hasBrushExists : brushed(rangeInfoList)) && visualSolution.applyVisual(STATE_LIST, visualMappings, data, getValueState);
  177. });
  178. });
  179. dispatchAction(api, throttleType, throttleDelay, brushSelected, payload);
  180. });
  181. function dispatchAction(api, throttleType, throttleDelay, brushSelected, payload) {
  182. // This event will not be triggered when `setOpion`, otherwise dead lock may
  183. // triggered when do `setOption` in event listener, which we do not find
  184. // satisfactory way to solve yet. Some considered resolutions:
  185. // (a) Diff with prevoius selected data ant only trigger event when changed.
  186. // But store previous data and diff precisely (i.e., not only by dataIndex, but
  187. // also detect value changes in selected data) might bring complexity or fragility.
  188. // (b) Use spectial param like `silent` to suppress event triggering.
  189. // But such kind of volatile param may be weird in `setOption`.
  190. if (!payload) {
  191. return;
  192. }
  193. var zr = api.getZr();
  194. if (zr[DISPATCH_FLAG]) {
  195. return;
  196. }
  197. if (!zr[DISPATCH_METHOD]) {
  198. zr[DISPATCH_METHOD] = doDispatch;
  199. }
  200. var fn = throttleUtil.createOrUpdate(zr, DISPATCH_METHOD, throttleDelay, throttleType);
  201. fn(api, brushSelected);
  202. }
  203. function doDispatch(api, brushSelected) {
  204. if (!api.isDisposed()) {
  205. var zr = api.getZr();
  206. zr[DISPATCH_FLAG] = true;
  207. api.dispatchAction({
  208. type: 'brushSelect',
  209. batch: brushSelected
  210. });
  211. zr[DISPATCH_FLAG] = false;
  212. }
  213. }
  214. function checkInRange(selectorsByBrushType, rangeInfoList, data, dataIndex) {
  215. for (var i = 0, len = rangeInfoList.length; i < len; i++) {
  216. var area = rangeInfoList[i];
  217. if (selectorsByBrushType[area.brushType](dataIndex, data, area.selectors, area)) {
  218. return true;
  219. }
  220. }
  221. }
  222. function getSelectorsByBrushType(seriesModel) {
  223. var brushSelector = seriesModel.brushSelector;
  224. if (zrUtil.isString(brushSelector)) {
  225. var sels = [];
  226. zrUtil.each(selector, function (selectorsByElementType, brushType) {
  227. sels[brushType] = function (dataIndex, data, selectors, area) {
  228. var itemLayout = data.getItemLayout(dataIndex);
  229. return selectorsByElementType[brushSelector](itemLayout, selectors, area);
  230. };
  231. });
  232. return sels;
  233. } else if (zrUtil.isFunction(brushSelector)) {
  234. var bSelector = {};
  235. zrUtil.each(selector, function (sel, brushType) {
  236. bSelector[brushType] = brushSelector;
  237. });
  238. return bSelector;
  239. }
  240. return brushSelector;
  241. }
  242. function brushModelNotControll(brushModel, seriesIndex) {
  243. var seriesIndices = brushModel.option.seriesIndex;
  244. return seriesIndices != null && seriesIndices !== 'all' && (zrUtil.isArray(seriesIndices) ? zrUtil.indexOf(seriesIndices, seriesIndex) < 0 : seriesIndex !== seriesIndices);
  245. }
  246. function bindSelector(area) {
  247. var selectors = area.selectors = {};
  248. zrUtil.each(selector[area.brushType], function (selFn, elType) {
  249. // Do not use function binding or curry for performance.
  250. selectors[elType] = function (itemLayout) {
  251. return selFn(itemLayout, selectors, area);
  252. };
  253. });
  254. return area;
  255. }
  256. var boundingRectBuilders = {
  257. lineX: zrUtil.noop,
  258. lineY: zrUtil.noop,
  259. rect: function (area) {
  260. return getBoundingRectFromMinMax(area.range);
  261. },
  262. polygon: function (area) {
  263. var minMax;
  264. var range = area.range;
  265. for (var i = 0, len = range.length; i < len; i++) {
  266. minMax = minMax || [[Infinity, -Infinity], [Infinity, -Infinity]];
  267. var rg = range[i];
  268. rg[0] < minMax[0][0] && (minMax[0][0] = rg[0]);
  269. rg[0] > minMax[0][1] && (minMax[0][1] = rg[0]);
  270. rg[1] < minMax[1][0] && (minMax[1][0] = rg[1]);
  271. rg[1] > minMax[1][1] && (minMax[1][1] = rg[1]);
  272. }
  273. return minMax && getBoundingRectFromMinMax(minMax);
  274. }
  275. };
  276. function getBoundingRectFromMinMax(minMax) {
  277. return new BoundingRect(minMax[0][0], minMax[1][0], minMax[0][1] - minMax[0][0], minMax[1][1] - minMax[1][0]);
  278. }
  279. exports.layoutCovers = layoutCovers;