axisTrigger.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. var zrUtil = require("zrender/lib/core/util");
  2. var modelUtil = require("../../util/model");
  3. var modelHelper = require("./modelHelper");
  4. var findPointFromSeries = require("./findPointFromSeries");
  5. var each = zrUtil.each;
  6. var curry = zrUtil.curry;
  7. var get = modelUtil.makeGetter();
  8. /**
  9. * Basic logic: check all axis, if they do not demand show/highlight,
  10. * then hide/downplay them.
  11. *
  12. * @param {Object} coordSysAxesInfo
  13. * @param {Object} payload
  14. * @param {string} [payload.currTrigger] 'click' | 'mousemove' | 'leave'
  15. * @param {Array.<number>} [payload.x] x and y, which are mandatory, specify a point to
  16. * trigger axisPointer and tooltip.
  17. * @param {Array.<number>} [payload.y] x and y, which are mandatory, specify a point to
  18. * trigger axisPointer and tooltip.
  19. * @param {Object} [payload.seriesIndex] finder, optional, restrict target axes.
  20. * @param {Object} [payload.dataIndex] finder, restrict target axes.
  21. * @param {Object} [payload.axesInfo] finder, restrict target axes.
  22. * [{
  23. * axisDim: 'x'|'y'|'angle'|...,
  24. * axisIndex: ...,
  25. * value: ...
  26. * }, ...]
  27. * @param {Function} [payload.dispatchAction]
  28. * @param {Object} [payload.tooltipOption]
  29. * @param {Object|Array.<number>|Function} [payload.position] Tooltip position,
  30. * which can be specified in dispatchAction
  31. * @param {module:echarts/model/Global} ecModel
  32. * @param {module:echarts/ExtensionAPI} api
  33. * @return {Object} content of event obj for echarts.connect.
  34. */
  35. function _default(payload, ecModel, api) {
  36. var currTrigger = payload.currTrigger;
  37. var point = [payload.x, payload.y];
  38. var finder = payload;
  39. var dispatchAction = payload.dispatchAction || zrUtil.bind(api.dispatchAction, api);
  40. var coordSysAxesInfo = ecModel.getComponent('axisPointer').coordSysAxesInfo; // Pending
  41. // See #6121. But we are not able to reproduce it yet.
  42. if (!coordSysAxesInfo) {
  43. return;
  44. }
  45. if (illegalPoint(point)) {
  46. // Used in the default behavior of `connection`: use the sample seriesIndex
  47. // and dataIndex. And also used in the tooltipView trigger.
  48. point = findPointFromSeries({
  49. seriesIndex: finder.seriesIndex,
  50. // Do not use dataIndexInside from other ec instance.
  51. // FIXME: auto detect it?
  52. dataIndex: finder.dataIndex
  53. }, ecModel).point;
  54. }
  55. var isIllegalPoint = illegalPoint(point); // Axis and value can be specified when calling dispatchAction({type: 'updateAxisPointer'}).
  56. // Notice: In this case, it is difficult to get the `point` (which is necessary to show
  57. // tooltip, so if point is not given, we just use the point found by sample seriesIndex
  58. // and dataIndex.
  59. var inputAxesInfo = finder.axesInfo;
  60. var axesInfo = coordSysAxesInfo.axesInfo;
  61. var shouldHide = currTrigger === 'leave' || illegalPoint(point);
  62. var outputFinder = {};
  63. var showValueMap = {};
  64. var dataByCoordSys = {
  65. list: [],
  66. map: {}
  67. };
  68. var updaters = {
  69. showPointer: curry(showPointer, showValueMap),
  70. showTooltip: curry(showTooltip, dataByCoordSys)
  71. }; // Process for triggered axes.
  72. each(coordSysAxesInfo.coordSysMap, function (coordSys, coordSysKey) {
  73. // If a point given, it must be contained by the coordinate system.
  74. var coordSysContainsPoint = isIllegalPoint || coordSys.containPoint(point);
  75. each(coordSysAxesInfo.coordSysAxesInfo[coordSysKey], function (axisInfo, key) {
  76. var axis = axisInfo.axis;
  77. var inputAxisInfo = findInputAxisInfo(inputAxesInfo, axisInfo); // If no inputAxesInfo, no axis is restricted.
  78. if (!shouldHide && coordSysContainsPoint && (!inputAxesInfo || inputAxisInfo)) {
  79. var val = inputAxisInfo && inputAxisInfo.value;
  80. if (val == null && !isIllegalPoint) {
  81. val = axis.pointToData(point);
  82. }
  83. val != null && processOnAxis(axisInfo, val, updaters, false, outputFinder);
  84. }
  85. });
  86. }); // Process for linked axes.
  87. var linkTriggers = {};
  88. each(axesInfo, function (tarAxisInfo, tarKey) {
  89. var linkGroup = tarAxisInfo.linkGroup; // If axis has been triggered in the previous stage, it should not be triggered by link.
  90. if (linkGroup && !showValueMap[tarKey]) {
  91. each(linkGroup.axesInfo, function (srcAxisInfo, srcKey) {
  92. var srcValItem = showValueMap[srcKey]; // If srcValItem exist, source axis is triggered, so link to target axis.
  93. if (srcAxisInfo !== tarAxisInfo && srcValItem) {
  94. var val = srcValItem.value;
  95. linkGroup.mapper && (val = tarAxisInfo.axis.scale.parse(linkGroup.mapper(val, makeMapperParam(srcAxisInfo), makeMapperParam(tarAxisInfo))));
  96. linkTriggers[tarAxisInfo.key] = val;
  97. }
  98. });
  99. }
  100. });
  101. each(linkTriggers, function (val, tarKey) {
  102. processOnAxis(axesInfo[tarKey], val, updaters, true, outputFinder);
  103. });
  104. updateModelActually(showValueMap, axesInfo, outputFinder);
  105. dispatchTooltipActually(dataByCoordSys, point, payload, dispatchAction);
  106. dispatchHighDownActually(axesInfo, dispatchAction, api);
  107. return outputFinder;
  108. }
  109. function processOnAxis(axisInfo, newValue, updaters, dontSnap, outputFinder) {
  110. var axis = axisInfo.axis;
  111. if (axis.scale.isBlank() || !axis.containData(newValue)) {
  112. return;
  113. }
  114. if (!axisInfo.involveSeries) {
  115. updaters.showPointer(axisInfo, newValue);
  116. return;
  117. } // Heavy calculation. So put it after axis.containData checking.
  118. var payloadInfo = buildPayloadsBySeries(newValue, axisInfo);
  119. var payloadBatch = payloadInfo.payloadBatch;
  120. var snapToValue = payloadInfo.snapToValue; // Fill content of event obj for echarts.connect.
  121. // By defualt use the first involved series data as a sample to connect.
  122. if (payloadBatch[0] && outputFinder.seriesIndex == null) {
  123. zrUtil.extend(outputFinder, payloadBatch[0]);
  124. } // If no linkSource input, this process is for collecting link
  125. // target, where snap should not be accepted.
  126. if (!dontSnap && axisInfo.snap) {
  127. if (axis.containData(snapToValue) && snapToValue != null) {
  128. newValue = snapToValue;
  129. }
  130. }
  131. updaters.showPointer(axisInfo, newValue, payloadBatch, outputFinder); // Tooltip should always be snapToValue, otherwise there will be
  132. // incorrect "axis value ~ series value" mapping displayed in tooltip.
  133. updaters.showTooltip(axisInfo, payloadInfo, snapToValue);
  134. }
  135. function buildPayloadsBySeries(value, axisInfo) {
  136. var axis = axisInfo.axis;
  137. var dim = axis.dim;
  138. var snapToValue = value;
  139. var payloadBatch = [];
  140. var minDist = Number.MAX_VALUE;
  141. var minDiff = -1;
  142. each(axisInfo.seriesModels, function (series, idx) {
  143. var dataDim = series.coordDimToDataDim(dim);
  144. var seriesNestestValue;
  145. var dataIndices;
  146. if (series.getAxisTooltipData) {
  147. var result = series.getAxisTooltipData(dataDim, value, axis);
  148. dataIndices = result.dataIndices;
  149. seriesNestestValue = result.nestestValue;
  150. } else {
  151. dataIndices = series.getData().indicesOfNearest(dataDim[0], value, // Add a threshold to avoid find the wrong dataIndex
  152. // when data length is not same.
  153. false, axis.type === 'category' ? 0.5 : null);
  154. if (!dataIndices.length) {
  155. return;
  156. }
  157. seriesNestestValue = series.getData().get(dataDim[0], dataIndices[0]);
  158. }
  159. if (seriesNestestValue == null || !isFinite(seriesNestestValue)) {
  160. return;
  161. }
  162. var diff = value - seriesNestestValue;
  163. var dist = Math.abs(diff); // Consider category case
  164. if (dist <= minDist) {
  165. if (dist < minDist || diff >= 0 && minDiff < 0) {
  166. minDist = dist;
  167. minDiff = diff;
  168. snapToValue = seriesNestestValue;
  169. payloadBatch.length = 0;
  170. }
  171. each(dataIndices, function (dataIndex) {
  172. payloadBatch.push({
  173. seriesIndex: series.seriesIndex,
  174. dataIndexInside: dataIndex,
  175. dataIndex: series.getData().getRawIndex(dataIndex)
  176. });
  177. });
  178. }
  179. });
  180. return {
  181. payloadBatch: payloadBatch,
  182. snapToValue: snapToValue
  183. };
  184. }
  185. function showPointer(showValueMap, axisInfo, value, payloadBatch) {
  186. showValueMap[axisInfo.key] = {
  187. value: value,
  188. payloadBatch: payloadBatch
  189. };
  190. }
  191. function showTooltip(dataByCoordSys, axisInfo, payloadInfo, value) {
  192. var payloadBatch = payloadInfo.payloadBatch;
  193. var axis = axisInfo.axis;
  194. var axisModel = axis.model;
  195. var axisPointerModel = axisInfo.axisPointerModel; // If no data, do not create anything in dataByCoordSys,
  196. // whose length will be used to judge whether dispatch action.
  197. if (!axisInfo.triggerTooltip || !payloadBatch.length) {
  198. return;
  199. }
  200. var coordSysModel = axisInfo.coordSys.model;
  201. var coordSysKey = modelHelper.makeKey(coordSysModel);
  202. var coordSysItem = dataByCoordSys.map[coordSysKey];
  203. if (!coordSysItem) {
  204. coordSysItem = dataByCoordSys.map[coordSysKey] = {
  205. coordSysId: coordSysModel.id,
  206. coordSysIndex: coordSysModel.componentIndex,
  207. coordSysType: coordSysModel.type,
  208. coordSysMainType: coordSysModel.mainType,
  209. dataByAxis: []
  210. };
  211. dataByCoordSys.list.push(coordSysItem);
  212. }
  213. coordSysItem.dataByAxis.push({
  214. axisDim: axis.dim,
  215. axisIndex: axisModel.componentIndex,
  216. axisType: axisModel.type,
  217. axisId: axisModel.id,
  218. value: value,
  219. // Caustion: viewHelper.getValueLabel is actually on "view stage", which
  220. // depends that all models have been updated. So it should not be performed
  221. // here. Considering axisPointerModel used here is volatile, which is hard
  222. // to be retrieve in TooltipView, we prepare parameters here.
  223. valueLabelOpt: {
  224. precision: axisPointerModel.get('label.precision'),
  225. formatter: axisPointerModel.get('label.formatter')
  226. },
  227. seriesDataIndices: payloadBatch.slice()
  228. });
  229. }
  230. function updateModelActually(showValueMap, axesInfo, outputFinder) {
  231. var outputAxesInfo = outputFinder.axesInfo = []; // Basic logic: If no 'show' required, 'hide' this axisPointer.
  232. each(axesInfo, function (axisInfo, key) {
  233. var option = axisInfo.axisPointerModel.option;
  234. var valItem = showValueMap[key];
  235. if (valItem) {
  236. !axisInfo.useHandle && (option.status = 'show');
  237. option.value = valItem.value; // For label formatter param and highlight.
  238. option.seriesDataIndices = (valItem.payloadBatch || []).slice();
  239. } // When always show (e.g., handle used), remain
  240. // original value and status.
  241. else {
  242. // If hide, value still need to be set, consider
  243. // click legend to toggle axis blank.
  244. !axisInfo.useHandle && (option.status = 'hide');
  245. } // If status is 'hide', should be no info in payload.
  246. option.status === 'show' && outputAxesInfo.push({
  247. axisDim: axisInfo.axis.dim,
  248. axisIndex: axisInfo.axis.model.componentIndex,
  249. value: option.value
  250. });
  251. });
  252. }
  253. function dispatchTooltipActually(dataByCoordSys, point, payload, dispatchAction) {
  254. // Basic logic: If no showTip required, hideTip will be dispatched.
  255. if (illegalPoint(point) || !dataByCoordSys.list.length) {
  256. dispatchAction({
  257. type: 'hideTip'
  258. });
  259. return;
  260. } // In most case only one axis (or event one series is used). It is
  261. // convinient to fetch payload.seriesIndex and payload.dataIndex
  262. // dirtectly. So put the first seriesIndex and dataIndex of the first
  263. // axis on the payload.
  264. var sampleItem = ((dataByCoordSys.list[0].dataByAxis[0] || {}).seriesDataIndices || [])[0] || {};
  265. dispatchAction({
  266. type: 'showTip',
  267. escapeConnect: true,
  268. x: point[0],
  269. y: point[1],
  270. tooltipOption: payload.tooltipOption,
  271. position: payload.position,
  272. dataIndexInside: sampleItem.dataIndexInside,
  273. dataIndex: sampleItem.dataIndex,
  274. seriesIndex: sampleItem.seriesIndex,
  275. dataByCoordSys: dataByCoordSys.list
  276. });
  277. }
  278. function dispatchHighDownActually(axesInfo, dispatchAction, api) {
  279. // FIXME
  280. // highlight status modification shoule be a stage of main process?
  281. // (Consider confilct (e.g., legend and axisPointer) and setOption)
  282. var zr = api.getZr();
  283. var highDownKey = 'axisPointerLastHighlights';
  284. var lastHighlights = get(zr)[highDownKey] || {};
  285. var newHighlights = get(zr)[highDownKey] = {}; // Update highlight/downplay status according to axisPointer model.
  286. // Build hash map and remove duplicate incidentally.
  287. each(axesInfo, function (axisInfo, key) {
  288. var option = axisInfo.axisPointerModel.option;
  289. option.status === 'show' && each(option.seriesDataIndices, function (batchItem) {
  290. var key = batchItem.seriesIndex + ' | ' + batchItem.dataIndex;
  291. newHighlights[key] = batchItem;
  292. });
  293. }); // Diff.
  294. var toHighlight = [];
  295. var toDownplay = [];
  296. zrUtil.each(lastHighlights, function (batchItem, key) {
  297. !newHighlights[key] && toDownplay.push(batchItem);
  298. });
  299. zrUtil.each(newHighlights, function (batchItem, key) {
  300. !lastHighlights[key] && toHighlight.push(batchItem);
  301. });
  302. toDownplay.length && api.dispatchAction({
  303. type: 'downplay',
  304. escapeConnect: true,
  305. batch: toDownplay
  306. });
  307. toHighlight.length && api.dispatchAction({
  308. type: 'highlight',
  309. escapeConnect: true,
  310. batch: toHighlight
  311. });
  312. }
  313. function findInputAxisInfo(inputAxesInfo, axisInfo) {
  314. for (var i = 0; i < (inputAxesInfo || []).length; i++) {
  315. var inputAxisInfo = inputAxesInfo[i];
  316. if (axisInfo.axis.dim === inputAxisInfo.axisDim && axisInfo.axis.model.componentIndex === inputAxisInfo.axisIndex) {
  317. return inputAxisInfo;
  318. }
  319. }
  320. }
  321. function makeMapperParam(axisInfo) {
  322. var axisModel = axisInfo.axis.model;
  323. var item = {};
  324. var dim = item.axisDim = axisInfo.axis.dim;
  325. item.axisIndex = item[dim + 'AxisIndex'] = axisModel.componentIndex;
  326. item.axisName = item[dim + 'AxisName'] = axisModel.name;
  327. item.axisId = item[dim + 'AxisId'] = axisModel.id;
  328. return item;
  329. }
  330. function illegalPoint(point) {
  331. return !point || point[0] == null || isNaN(point[0]) || point[1] == null || isNaN(point[1]);
  332. }
  333. module.exports = _default;