modelHelper.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 Model from '../../model/Model.js';
  41. import { each, curry, clone, defaults, isArray, indexOf } from 'zrender/lib/core/util.js'; // Build axisPointerModel, mergin tooltip.axisPointer model for each axis.
  42. // allAxesInfo should be updated when setOption performed.
  43. export function collect(ecModel, api) {
  44. var result = {
  45. /**
  46. * key: makeKey(axis.model)
  47. * value: {
  48. * axis,
  49. * coordSys,
  50. * axisPointerModel,
  51. * triggerTooltip,
  52. * involveSeries,
  53. * snap,
  54. * seriesModels,
  55. * seriesDataCount
  56. * }
  57. */
  58. axesInfo: {},
  59. seriesInvolved: false,
  60. /**
  61. * key: makeKey(coordSys.model)
  62. * value: Object: key makeKey(axis.model), value: axisInfo
  63. */
  64. coordSysAxesInfo: {},
  65. coordSysMap: {}
  66. };
  67. collectAxesInfo(result, ecModel, api); // Check seriesInvolved for performance, in case too many series in some chart.
  68. result.seriesInvolved && collectSeriesInfo(result, ecModel);
  69. return result;
  70. }
  71. function collectAxesInfo(result, ecModel, api) {
  72. var globalTooltipModel = ecModel.getComponent('tooltip');
  73. var globalAxisPointerModel = ecModel.getComponent('axisPointer'); // links can only be set on global.
  74. var linksOption = globalAxisPointerModel.get('link', true) || [];
  75. var linkGroups = []; // Collect axes info.
  76. each(api.getCoordinateSystems(), function (coordSys) {
  77. // Some coordinate system do not support axes, like geo.
  78. if (!coordSys.axisPointerEnabled) {
  79. return;
  80. }
  81. var coordSysKey = makeKey(coordSys.model);
  82. var axesInfoInCoordSys = result.coordSysAxesInfo[coordSysKey] = {};
  83. result.coordSysMap[coordSysKey] = coordSys; // Set tooltip (like 'cross') is a convenient way to show axisPointer
  84. // for user. So we enable setting tooltip on coordSys model.
  85. var coordSysModel = coordSys.model;
  86. var baseTooltipModel = coordSysModel.getModel('tooltip', globalTooltipModel);
  87. each(coordSys.getAxes(), curry(saveTooltipAxisInfo, false, null)); // If axis tooltip used, choose tooltip axis for each coordSys.
  88. // Notice this case: coordSys is `grid` but not `cartesian2D` here.
  89. if (coordSys.getTooltipAxes && globalTooltipModel // If tooltip.showContent is set as false, tooltip will not
  90. // show but axisPointer will show as normal.
  91. && baseTooltipModel.get('show')) {
  92. // Compatible with previous logic. But series.tooltip.trigger: 'axis'
  93. // or series.data[n].tooltip.trigger: 'axis' are not support any more.
  94. var triggerAxis = baseTooltipModel.get('trigger') === 'axis';
  95. var cross = baseTooltipModel.get(['axisPointer', 'type']) === 'cross';
  96. var tooltipAxes = coordSys.getTooltipAxes(baseTooltipModel.get(['axisPointer', 'axis']));
  97. if (triggerAxis || cross) {
  98. each(tooltipAxes.baseAxes, curry(saveTooltipAxisInfo, cross ? 'cross' : true, triggerAxis));
  99. }
  100. if (cross) {
  101. each(tooltipAxes.otherAxes, curry(saveTooltipAxisInfo, 'cross', false));
  102. }
  103. } // fromTooltip: true | false | 'cross'
  104. // triggerTooltip: true | false | null
  105. function saveTooltipAxisInfo(fromTooltip, triggerTooltip, axis) {
  106. var axisPointerModel = axis.model.getModel('axisPointer', globalAxisPointerModel);
  107. var axisPointerShow = axisPointerModel.get('show');
  108. if (!axisPointerShow || axisPointerShow === 'auto' && !fromTooltip && !isHandleTrigger(axisPointerModel)) {
  109. return;
  110. }
  111. if (triggerTooltip == null) {
  112. triggerTooltip = axisPointerModel.get('triggerTooltip');
  113. }
  114. axisPointerModel = fromTooltip ? makeAxisPointerModel(axis, baseTooltipModel, globalAxisPointerModel, ecModel, fromTooltip, triggerTooltip) : axisPointerModel;
  115. var snap = axisPointerModel.get('snap');
  116. var axisKey = makeKey(axis.model);
  117. var involveSeries = triggerTooltip || snap || axis.type === 'category'; // If result.axesInfo[key] exist, override it (tooltip has higher priority).
  118. var axisInfo = result.axesInfo[axisKey] = {
  119. key: axisKey,
  120. axis: axis,
  121. coordSys: coordSys,
  122. axisPointerModel: axisPointerModel,
  123. triggerTooltip: triggerTooltip,
  124. involveSeries: involveSeries,
  125. snap: snap,
  126. useHandle: isHandleTrigger(axisPointerModel),
  127. seriesModels: [],
  128. linkGroup: null
  129. };
  130. axesInfoInCoordSys[axisKey] = axisInfo;
  131. result.seriesInvolved = result.seriesInvolved || involveSeries;
  132. var groupIndex = getLinkGroupIndex(linksOption, axis);
  133. if (groupIndex != null) {
  134. var linkGroup = linkGroups[groupIndex] || (linkGroups[groupIndex] = {
  135. axesInfo: {}
  136. });
  137. linkGroup.axesInfo[axisKey] = axisInfo;
  138. linkGroup.mapper = linksOption[groupIndex].mapper;
  139. axisInfo.linkGroup = linkGroup;
  140. }
  141. }
  142. });
  143. }
  144. function makeAxisPointerModel(axis, baseTooltipModel, globalAxisPointerModel, ecModel, fromTooltip, triggerTooltip) {
  145. var tooltipAxisPointerModel = baseTooltipModel.getModel('axisPointer');
  146. var fields = ['type', 'snap', 'lineStyle', 'shadowStyle', 'label', 'animation', 'animationDurationUpdate', 'animationEasingUpdate', 'z'];
  147. var volatileOption = {};
  148. each(fields, function (field) {
  149. volatileOption[field] = clone(tooltipAxisPointerModel.get(field));
  150. }); // category axis do not auto snap, otherwise some tick that do not
  151. // has value can not be hovered. value/time/log axis default snap if
  152. // triggered from tooltip and trigger tooltip.
  153. volatileOption.snap = axis.type !== 'category' && !!triggerTooltip; // Compatible with previous behavior, tooltip axis does not show label by default.
  154. // Only these properties can be overridden from tooltip to axisPointer.
  155. if (tooltipAxisPointerModel.get('type') === 'cross') {
  156. volatileOption.type = 'line';
  157. }
  158. var labelOption = volatileOption.label || (volatileOption.label = {}); // Follow the convention, do not show label when triggered by tooltip by default.
  159. labelOption.show == null && (labelOption.show = false);
  160. if (fromTooltip === 'cross') {
  161. // When 'cross', both axes show labels.
  162. var tooltipAxisPointerLabelShow = tooltipAxisPointerModel.get(['label', 'show']);
  163. labelOption.show = tooltipAxisPointerLabelShow != null ? tooltipAxisPointerLabelShow : true; // If triggerTooltip, this is a base axis, which should better not use cross style
  164. // (cross style is dashed by default)
  165. if (!triggerTooltip) {
  166. var crossStyle = volatileOption.lineStyle = tooltipAxisPointerModel.get('crossStyle');
  167. crossStyle && defaults(labelOption, crossStyle.textStyle);
  168. }
  169. }
  170. return axis.model.getModel('axisPointer', new Model(volatileOption, globalAxisPointerModel, ecModel));
  171. }
  172. function collectSeriesInfo(result, ecModel) {
  173. // Prepare data for axis trigger
  174. ecModel.eachSeries(function (seriesModel) {
  175. // Notice this case: this coordSys is `cartesian2D` but not `grid`.
  176. var coordSys = seriesModel.coordinateSystem;
  177. var seriesTooltipTrigger = seriesModel.get(['tooltip', 'trigger'], true);
  178. var seriesTooltipShow = seriesModel.get(['tooltip', 'show'], true);
  179. if (!coordSys || seriesTooltipTrigger === 'none' || seriesTooltipTrigger === false || seriesTooltipTrigger === 'item' || seriesTooltipShow === false || seriesModel.get(['axisPointer', 'show'], true) === false) {
  180. return;
  181. }
  182. each(result.coordSysAxesInfo[makeKey(coordSys.model)], function (axisInfo) {
  183. var axis = axisInfo.axis;
  184. if (coordSys.getAxis(axis.dim) === axis) {
  185. axisInfo.seriesModels.push(seriesModel);
  186. axisInfo.seriesDataCount == null && (axisInfo.seriesDataCount = 0);
  187. axisInfo.seriesDataCount += seriesModel.getData().count();
  188. }
  189. });
  190. });
  191. }
  192. /**
  193. * For example:
  194. * {
  195. * axisPointer: {
  196. * links: [{
  197. * xAxisIndex: [2, 4],
  198. * yAxisIndex: 'all'
  199. * }, {
  200. * xAxisId: ['a5', 'a7'],
  201. * xAxisName: 'xxx'
  202. * }]
  203. * }
  204. * }
  205. */
  206. function getLinkGroupIndex(linksOption, axis) {
  207. var axisModel = axis.model;
  208. var dim = axis.dim;
  209. for (var i = 0; i < linksOption.length; i++) {
  210. var linkOption = linksOption[i] || {};
  211. if (checkPropInLink(linkOption[dim + 'AxisId'], axisModel.id) || checkPropInLink(linkOption[dim + 'AxisIndex'], axisModel.componentIndex) || checkPropInLink(linkOption[dim + 'AxisName'], axisModel.name)) {
  212. return i;
  213. }
  214. }
  215. }
  216. function checkPropInLink(linkPropValue, axisPropValue) {
  217. return linkPropValue === 'all' || isArray(linkPropValue) && indexOf(linkPropValue, axisPropValue) >= 0 || linkPropValue === axisPropValue;
  218. }
  219. export function fixValue(axisModel) {
  220. var axisInfo = getAxisInfo(axisModel);
  221. if (!axisInfo) {
  222. return;
  223. }
  224. var axisPointerModel = axisInfo.axisPointerModel;
  225. var scale = axisInfo.axis.scale;
  226. var option = axisPointerModel.option;
  227. var status = axisPointerModel.get('status');
  228. var value = axisPointerModel.get('value'); // Parse init value for category and time axis.
  229. if (value != null) {
  230. value = scale.parse(value);
  231. }
  232. var useHandle = isHandleTrigger(axisPointerModel); // If `handle` used, `axisPointer` will always be displayed, so value
  233. // and status should be initialized.
  234. if (status == null) {
  235. option.status = useHandle ? 'show' : 'hide';
  236. }
  237. var extent = scale.getExtent().slice();
  238. extent[0] > extent[1] && extent.reverse();
  239. if ( // Pick a value on axis when initializing.
  240. value == null // If both `handle` and `dataZoom` are used, value may be out of axis extent,
  241. // where we should re-pick a value to keep `handle` displaying normally.
  242. || value > extent[1]) {
  243. // Make handle displayed on the end of the axis when init, which looks better.
  244. value = extent[1];
  245. }
  246. if (value < extent[0]) {
  247. value = extent[0];
  248. }
  249. option.value = value;
  250. if (useHandle) {
  251. option.status = axisInfo.axis.scale.isBlank() ? 'hide' : 'show';
  252. }
  253. }
  254. export function getAxisInfo(axisModel) {
  255. var coordSysAxesInfo = (axisModel.ecModel.getComponent('axisPointer') || {}).coordSysAxesInfo;
  256. return coordSysAxesInfo && coordSysAxesInfo.axesInfo[makeKey(axisModel)];
  257. }
  258. export function getAxisPointerModel(axisModel) {
  259. var axisInfo = getAxisInfo(axisModel);
  260. return axisInfo && axisInfo.axisPointerModel;
  261. }
  262. function isHandleTrigger(axisPointerModel) {
  263. return !!axisPointerModel.get(['handle', 'show']);
  264. }
  265. /**
  266. * @param {module:echarts/model/Model} model
  267. * @return {string} unique key
  268. */
  269. export function makeKey(model) {
  270. return model.type + '||' + model.id;
  271. }