seriesFormatTooltip.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 { trim, isArray, each, reduce } from 'zrender/lib/core/util.js';
  41. import { retrieveVisualColorForTooltipMarker, createTooltipMarkup } from './tooltipMarkup.js';
  42. import { retrieveRawValue } from '../../data/helper/dataProvider.js';
  43. import { isNameSpecified } from '../../util/model.js';
  44. export function defaultSeriesFormatTooltip(opt) {
  45. var series = opt.series;
  46. var dataIndex = opt.dataIndex;
  47. var multipleSeries = opt.multipleSeries;
  48. var data = series.getData();
  49. var tooltipDims = data.mapDimensionsAll('defaultedTooltip');
  50. var tooltipDimLen = tooltipDims.length;
  51. var value = series.getRawValue(dataIndex);
  52. var isValueArr = isArray(value);
  53. var markerColor = retrieveVisualColorForTooltipMarker(series, dataIndex);
  54. // Complicated rule for pretty tooltip.
  55. var inlineValue;
  56. var inlineValueType;
  57. var subBlocks;
  58. var sortParam;
  59. if (tooltipDimLen > 1 || isValueArr && !tooltipDimLen) {
  60. var formatArrResult = formatTooltipArrayValue(value, series, dataIndex, tooltipDims, markerColor);
  61. inlineValue = formatArrResult.inlineValues;
  62. inlineValueType = formatArrResult.inlineValueTypes;
  63. subBlocks = formatArrResult.blocks;
  64. // Only support tooltip sort by the first inline value. It's enough in most cases.
  65. sortParam = formatArrResult.inlineValues[0];
  66. } else if (tooltipDimLen) {
  67. var dimInfo = data.getDimensionInfo(tooltipDims[0]);
  68. sortParam = inlineValue = retrieveRawValue(data, dataIndex, tooltipDims[0]);
  69. inlineValueType = dimInfo.type;
  70. } else {
  71. sortParam = inlineValue = isValueArr ? value[0] : value;
  72. }
  73. // Do not show generated series name. It might not be readable.
  74. var seriesNameSpecified = isNameSpecified(series);
  75. var seriesName = seriesNameSpecified && series.name || '';
  76. var itemName = data.getName(dataIndex);
  77. var inlineName = multipleSeries ? seriesName : itemName;
  78. return createTooltipMarkup('section', {
  79. header: seriesName,
  80. // When series name is not specified, do not show a header line with only '-'.
  81. // This case always happens in tooltip.trigger: 'item'.
  82. noHeader: multipleSeries || !seriesNameSpecified,
  83. sortParam: sortParam,
  84. blocks: [createTooltipMarkup('nameValue', {
  85. markerType: 'item',
  86. markerColor: markerColor,
  87. // Do not mix display seriesName and itemName in one tooltip,
  88. // which might confuses users.
  89. name: inlineName,
  90. // name dimension might be auto assigned, where the name might
  91. // be not readable. So we check trim here.
  92. noName: !trim(inlineName),
  93. value: inlineValue,
  94. valueType: inlineValueType,
  95. dataIndex: dataIndex
  96. })].concat(subBlocks || [])
  97. });
  98. }
  99. function formatTooltipArrayValue(value, series, dataIndex, tooltipDims, colorStr) {
  100. // check: category-no-encode-has-axis-data in dataset.html
  101. var data = series.getData();
  102. var isValueMultipleLine = reduce(value, function (isValueMultipleLine, val, idx) {
  103. var dimItem = data.getDimensionInfo(idx);
  104. return isValueMultipleLine = isValueMultipleLine || dimItem && dimItem.tooltip !== false && dimItem.displayName != null;
  105. }, false);
  106. var inlineValues = [];
  107. var inlineValueTypes = [];
  108. var blocks = [];
  109. tooltipDims.length ? each(tooltipDims, function (dim) {
  110. setEachItem(retrieveRawValue(data, dataIndex, dim), dim);
  111. })
  112. // By default, all dims is used on tooltip.
  113. : each(value, setEachItem);
  114. function setEachItem(val, dim) {
  115. var dimInfo = data.getDimensionInfo(dim);
  116. // If `dimInfo.tooltip` is not set, show tooltip.
  117. if (!dimInfo || dimInfo.otherDims.tooltip === false) {
  118. return;
  119. }
  120. if (isValueMultipleLine) {
  121. blocks.push(createTooltipMarkup('nameValue', {
  122. markerType: 'subItem',
  123. markerColor: colorStr,
  124. name: dimInfo.displayName,
  125. value: val,
  126. valueType: dimInfo.type
  127. }));
  128. } else {
  129. inlineValues.push(val);
  130. inlineValueTypes.push(dimInfo.type);
  131. }
  132. }
  133. return {
  134. inlineValues: inlineValues,
  135. inlineValueTypes: inlineValueTypes,
  136. blocks: blocks
  137. };
  138. }