visualEncoding.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. var brushTargetManager = brushModel.brushTargetManager = new BrushTargetManager(brushModel.option, ecModel);
  59. brushTargetManager.setInputRanges(brushModel.areas, ecModel);
  60. });
  61. });
  62. /**
  63. * Register the visual encoding if this modules required.
  64. */
  65. echarts.registerVisual(PRIORITY_BRUSH, function (ecModel, api, payload) {
  66. var brushSelected = [];
  67. var throttleType;
  68. var throttleDelay;
  69. ecModel.eachComponent({
  70. mainType: 'brush'
  71. }, function (brushModel, brushIndex) {
  72. var thisBrushSelected = {
  73. brushId: brushModel.id,
  74. brushIndex: brushIndex,
  75. brushName: brushModel.name,
  76. areas: zrUtil.clone(brushModel.areas),
  77. selected: []
  78. }; // Every brush component exists in event params, convenient
  79. // for user to find by index.
  80. brushSelected.push(thisBrushSelected);
  81. var brushOption = brushModel.option;
  82. var brushLink = brushOption.brushLink;
  83. var linkedSeriesMap = [];
  84. var selectedDataIndexForLink = [];
  85. var rangeInfoBySeries = [];
  86. var hasBrushExists = 0;
  87. if (!brushIndex) {
  88. // Only the first throttle setting works.
  89. throttleType = brushOption.throttleType;
  90. throttleDelay = brushOption.throttleDelay;
  91. } // Add boundingRect and selectors to range.
  92. var areas = zrUtil.map(brushModel.areas, function (area) {
  93. return bindSelector(zrUtil.defaults({
  94. boundingRect: boundingRectBuilders[area.brushType](area)
  95. }, area));
  96. });
  97. var visualMappings = visualSolution.createVisualMappings(brushModel.option, STATE_LIST, function (mappingOption) {
  98. mappingOption.mappingMethod = 'fixed';
  99. });
  100. zrUtil.isArray(brushLink) && zrUtil.each(brushLink, function (seriesIndex) {
  101. linkedSeriesMap[seriesIndex] = 1;
  102. });
  103. function linkOthers(seriesIndex) {
  104. return brushLink === 'all' || linkedSeriesMap[seriesIndex];
  105. } // If no supported brush or no brush on the series,
  106. // all visuals should be in original state.
  107. function brushed(rangeInfoList) {
  108. return !!rangeInfoList.length;
  109. }
  110. /**
  111. * Logic for each series: (If the logic has to be modified one day, do it carefully!)
  112. *
  113. * ( brushed ┬ && ┬hasBrushExist ┬ && linkOthers ) => StepA: ┬record, ┬ StepB: ┬visualByRecord.
  114. * !brushed┘ ├hasBrushExist ┤ └nothing,┘ ├visualByRecord.
  115. * └!hasBrushExist┘ └nothing.
  116. * ( !brushed && ┬hasBrushExist ┬ && linkOthers ) => StepA: nothing, StepB: ┬visualByRecord.
  117. * └!hasBrushExist┘ └nothing.
  118. * ( brushed ┬ && !linkOthers ) => StepA: nothing, StepB: ┬visualByCheck.
  119. * !brushed┘ └nothing.
  120. * ( !brushed && !linkOthers ) => StepA: nothing, StepB: nothing.
  121. */
  122. // Step A
  123. ecModel.eachSeries(function (seriesModel, seriesIndex) {
  124. var rangeInfoList = rangeInfoBySeries[seriesIndex] = [];
  125. seriesModel.subType === 'parallel' ? stepAParallel(seriesModel, seriesIndex, rangeInfoList) : stepAOthers(seriesModel, seriesIndex, rangeInfoList);
  126. });
  127. function stepAParallel(seriesModel, seriesIndex) {
  128. var coordSys = seriesModel.coordinateSystem;
  129. hasBrushExists |= coordSys.hasAxisBrushed();
  130. linkOthers(seriesIndex) && coordSys.eachActiveState(seriesModel.getData(), function (activeState, dataIndex) {
  131. activeState === 'active' && (selectedDataIndexForLink[dataIndex] = 1);
  132. });
  133. }
  134. function stepAOthers(seriesModel, seriesIndex, rangeInfoList) {
  135. var selectorsByBrushType = getSelectorsByBrushType(seriesModel);
  136. if (!selectorsByBrushType || brushModelNotControll(brushModel, seriesIndex)) {
  137. return;
  138. }
  139. zrUtil.each(areas, function (area) {
  140. selectorsByBrushType[area.brushType] && brushModel.brushTargetManager.controlSeries(area, seriesModel, ecModel) && rangeInfoList.push(area);
  141. hasBrushExists |= brushed(rangeInfoList);
  142. });
  143. if (linkOthers(seriesIndex) && brushed(rangeInfoList)) {
  144. var data = seriesModel.getData();
  145. data.each(function (dataIndex) {
  146. if (checkInRange(selectorsByBrushType, rangeInfoList, data, dataIndex)) {
  147. selectedDataIndexForLink[dataIndex] = 1;
  148. }
  149. });
  150. }
  151. } // Step B
  152. ecModel.eachSeries(function (seriesModel, seriesIndex) {
  153. var seriesBrushSelected = {
  154. seriesId: seriesModel.id,
  155. seriesIndex: seriesIndex,
  156. seriesName: seriesModel.name,
  157. dataIndex: []
  158. }; // Every series exists in event params, convenient
  159. // for user to find series by seriesIndex.
  160. thisBrushSelected.selected.push(seriesBrushSelected);
  161. var selectorsByBrushType = getSelectorsByBrushType(seriesModel);
  162. var rangeInfoList = rangeInfoBySeries[seriesIndex];
  163. var data = seriesModel.getData();
  164. var getValueState = linkOthers(seriesIndex) ? function (dataIndex) {
  165. return selectedDataIndexForLink[dataIndex] ? (seriesBrushSelected.dataIndex.push(data.getRawIndex(dataIndex)), 'inBrush') : 'outOfBrush';
  166. } : function (dataIndex) {
  167. return checkInRange(selectorsByBrushType, rangeInfoList, data, dataIndex) ? (seriesBrushSelected.dataIndex.push(data.getRawIndex(dataIndex)), 'inBrush') : 'outOfBrush';
  168. }; // If no supported brush or no brush, all visuals are in original state.
  169. (linkOthers(seriesIndex) ? hasBrushExists : brushed(rangeInfoList)) && visualSolution.applyVisual(STATE_LIST, visualMappings, data, getValueState);
  170. });
  171. });
  172. dispatchAction(api, throttleType, throttleDelay, brushSelected, payload);
  173. });
  174. function dispatchAction(api, throttleType, throttleDelay, brushSelected, payload) {
  175. // This event will not be triggered when `setOpion`, otherwise dead lock may
  176. // triggered when do `setOption` in event listener, which we do not find
  177. // satisfactory way to solve yet. Some considered resolutions:
  178. // (a) Diff with prevoius selected data ant only trigger event when changed.
  179. // But store previous data and diff precisely (i.e., not only by dataIndex, but
  180. // also detect value changes in selected data) might bring complexity or fragility.
  181. // (b) Use spectial param like `silent` to suppress event triggering.
  182. // But such kind of volatile param may be weird in `setOption`.
  183. if (!payload) {
  184. return;
  185. }
  186. var zr = api.getZr();
  187. if (zr[DISPATCH_FLAG]) {
  188. return;
  189. }
  190. if (!zr[DISPATCH_METHOD]) {
  191. zr[DISPATCH_METHOD] = doDispatch;
  192. }
  193. var fn = throttleUtil.createOrUpdate(zr, DISPATCH_METHOD, throttleDelay, throttleType);
  194. fn(api, brushSelected);
  195. }
  196. function doDispatch(api, brushSelected) {
  197. if (!api.isDisposed()) {
  198. var zr = api.getZr();
  199. zr[DISPATCH_FLAG] = true;
  200. api.dispatchAction({
  201. type: 'brushSelect',
  202. batch: brushSelected
  203. });
  204. zr[DISPATCH_FLAG] = false;
  205. }
  206. }
  207. function checkInRange(selectorsByBrushType, rangeInfoList, data, dataIndex) {
  208. for (var i = 0, len = rangeInfoList.length; i < len; i++) {
  209. var area = rangeInfoList[i];
  210. if (selectorsByBrushType[area.brushType](dataIndex, data, area.selectors, area)) {
  211. return true;
  212. }
  213. }
  214. }
  215. function getSelectorsByBrushType(seriesModel) {
  216. var brushSelector = seriesModel.brushSelector;
  217. if (zrUtil.isString(brushSelector)) {
  218. var sels = [];
  219. zrUtil.each(selector, function (selectorsByElementType, brushType) {
  220. sels[brushType] = function (dataIndex, data, selectors, area) {
  221. var itemLayout = data.getItemLayout(dataIndex);
  222. return selectorsByElementType[brushSelector](itemLayout, selectors, area);
  223. };
  224. });
  225. return sels;
  226. } else if (zrUtil.isFunction(brushSelector)) {
  227. var bSelector = {};
  228. zrUtil.each(selector, function (sel, brushType) {
  229. bSelector[brushType] = brushSelector;
  230. });
  231. return bSelector;
  232. }
  233. return brushSelector;
  234. }
  235. function brushModelNotControll(brushModel, seriesIndex) {
  236. var seriesIndices = brushModel.option.seriesIndex;
  237. return seriesIndices != null && seriesIndices !== 'all' && (zrUtil.isArray(seriesIndices) ? zrUtil.indexOf(seriesIndices, seriesIndex) < 0 : seriesIndex !== seriesIndices);
  238. }
  239. function bindSelector(area) {
  240. var selectors = area.selectors = {};
  241. zrUtil.each(selector[area.brushType], function (selFn, elType) {
  242. // Do not use function binding or curry for performance.
  243. selectors[elType] = function (itemLayout) {
  244. return selFn(itemLayout, selectors, area);
  245. };
  246. });
  247. return area;
  248. }
  249. var boundingRectBuilders = {
  250. lineX: zrUtil.noop,
  251. lineY: zrUtil.noop,
  252. rect: function (area) {
  253. return getBoundingRectFromMinMax(area.range);
  254. },
  255. polygon: function (area) {
  256. var minMax;
  257. var range = area.range;
  258. for (var i = 0, len = range.length; i < len; i++) {
  259. minMax = minMax || [[Infinity, -Infinity], [Infinity, -Infinity]];
  260. var rg = range[i];
  261. rg[0] < minMax[0][0] && (minMax[0][0] = rg[0]);
  262. rg[0] > minMax[0][1] && (minMax[0][1] = rg[0]);
  263. rg[1] < minMax[1][0] && (minMax[1][0] = rg[1]);
  264. rg[1] > minMax[1][1] && (minMax[1][1] = rg[1]);
  265. }
  266. return minMax && getBoundingRectFromMinMax(minMax);
  267. }
  268. };
  269. function getBoundingRectFromMinMax(minMax) {
  270. return new BoundingRect(minMax[0][0], minMax[1][0], minMax[0][1] - minMax[0][0], minMax[1][1] - minMax[1][0]);
  271. }