seriesFormatTooltip.js 5.5 KB

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