tooltipMarkup.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 { getTooltipMarker, encodeHTML, makeValueReadable, convertToColorString } from '../../util/format.js';
  41. import { isString, each, hasOwn, isArray, map, assert, extend } from 'zrender/lib/core/util.js';
  42. import { SortOrderComparator } from '../../data/helper/dataValueHelper.js';
  43. import { getRandomIdBase } from '../../util/number.js';
  44. import tokens from '../../visual/tokens.js';
  45. var TOOLTIP_LINE_HEIGHT_CSS = 'line-height:1';
  46. function getTooltipLineHeight(textStyle) {
  47. var lineHeight = textStyle.lineHeight;
  48. if (lineHeight == null) {
  49. return TOOLTIP_LINE_HEIGHT_CSS;
  50. } else {
  51. return "line-height:" + encodeHTML(lineHeight + '') + "px";
  52. }
  53. }
  54. // TODO: more textStyle option
  55. function getTooltipTextStyle(textStyle, renderMode) {
  56. var nameFontColor = textStyle.color || tokens.color.tertiary;
  57. var nameFontSize = textStyle.fontSize || 12;
  58. var nameFontWeight = textStyle.fontWeight || '400';
  59. var valueFontColor = textStyle.color || tokens.color.secondary;
  60. var valueFontSize = textStyle.fontSize || 14;
  61. var valueFontWeight = textStyle.fontWeight || '900';
  62. if (renderMode === 'html') {
  63. // `textStyle` is probably from user input, should be encoded to reduce security risk.
  64. return {
  65. // eslint-disable-next-line max-len
  66. nameStyle: "font-size:" + encodeHTML(nameFontSize + '') + "px;color:" + encodeHTML(nameFontColor) + ";font-weight:" + encodeHTML(nameFontWeight + ''),
  67. // eslint-disable-next-line max-len
  68. valueStyle: "font-size:" + encodeHTML(valueFontSize + '') + "px;color:" + encodeHTML(valueFontColor) + ";font-weight:" + encodeHTML(valueFontWeight + '')
  69. };
  70. } else {
  71. return {
  72. nameStyle: {
  73. fontSize: nameFontSize,
  74. fill: nameFontColor,
  75. fontWeight: nameFontWeight
  76. },
  77. valueStyle: {
  78. fontSize: valueFontSize,
  79. fill: valueFontColor,
  80. fontWeight: valueFontWeight
  81. }
  82. };
  83. }
  84. }
  85. // See `TooltipMarkupLayoutIntent['innerGapLevel']`.
  86. // (value from UI design)
  87. var HTML_GAPS = [0, 10, 20, 30];
  88. var RICH_TEXT_GAPS = ['', '\n', '\n\n', '\n\n\n'];
  89. // eslint-disable-next-line max-len
  90. export function createTooltipMarkup(type, option) {
  91. option.type = type;
  92. return option;
  93. }
  94. function isSectionFragment(frag) {
  95. return frag.type === 'section';
  96. }
  97. function getBuilder(frag) {
  98. return isSectionFragment(frag) ? buildSection : buildNameValue;
  99. }
  100. function getBlockGapLevel(frag) {
  101. if (isSectionFragment(frag)) {
  102. var gapLevel_1 = 0;
  103. var subBlockLen = frag.blocks.length;
  104. var hasInnerGap_1 = subBlockLen > 1 || subBlockLen > 0 && !frag.noHeader;
  105. each(frag.blocks, function (subBlock) {
  106. var subGapLevel = getBlockGapLevel(subBlock);
  107. // If the some of the sub-blocks have some gaps (like 10px) inside, this block
  108. // should use a larger gap (like 20px) to distinguish those sub-blocks.
  109. if (subGapLevel >= gapLevel_1) {
  110. gapLevel_1 = subGapLevel + +(hasInnerGap_1 && (
  111. // 0 always can not be readable gap level.
  112. !subGapLevel
  113. // If no header, always keep the sub gap level. Otherwise
  114. // look weird in case `multipleSeries`.
  115. || isSectionFragment(subBlock) && !subBlock.noHeader));
  116. }
  117. });
  118. return gapLevel_1;
  119. }
  120. return 0;
  121. }
  122. function buildSection(ctx, fragment, topMarginForOuterGap, toolTipTextStyle) {
  123. var noHeader = fragment.noHeader;
  124. var gaps = getGap(getBlockGapLevel(fragment));
  125. var subMarkupTextList = [];
  126. var subBlocks = fragment.blocks || [];
  127. assert(!subBlocks || isArray(subBlocks));
  128. subBlocks = subBlocks || [];
  129. var orderMode = ctx.orderMode;
  130. if (fragment.sortBlocks && orderMode) {
  131. subBlocks = subBlocks.slice();
  132. var orderMap = {
  133. valueAsc: 'asc',
  134. valueDesc: 'desc'
  135. };
  136. if (hasOwn(orderMap, orderMode)) {
  137. var comparator_1 = new SortOrderComparator(orderMap[orderMode], null);
  138. subBlocks.sort(function (a, b) {
  139. return comparator_1.evaluate(a.sortParam, b.sortParam);
  140. });
  141. }
  142. // FIXME 'seriesDesc' necessary?
  143. else if (orderMode === 'seriesDesc') {
  144. subBlocks.reverse();
  145. }
  146. }
  147. each(subBlocks, function (subBlock, idx) {
  148. var valueFormatter = fragment.valueFormatter;
  149. var subMarkupText = getBuilder(subBlock)(
  150. // Inherit valueFormatter
  151. valueFormatter ? extend(extend({}, ctx), {
  152. valueFormatter: valueFormatter
  153. }) : ctx, subBlock, idx > 0 ? gaps.html : 0, toolTipTextStyle);
  154. subMarkupText != null && subMarkupTextList.push(subMarkupText);
  155. });
  156. var subMarkupText = ctx.renderMode === 'richText' ? subMarkupTextList.join(gaps.richText) : wrapBlockHTML(toolTipTextStyle, subMarkupTextList.join(''), noHeader ? topMarginForOuterGap : gaps.html);
  157. if (noHeader) {
  158. return subMarkupText;
  159. }
  160. var displayableHeader = makeValueReadable(fragment.header, 'ordinal', ctx.useUTC);
  161. var nameStyle = getTooltipTextStyle(toolTipTextStyle, ctx.renderMode).nameStyle;
  162. var tooltipLineHeight = getTooltipLineHeight(toolTipTextStyle);
  163. if (ctx.renderMode === 'richText') {
  164. return wrapInlineNameRichText(ctx, displayableHeader, nameStyle) + gaps.richText + subMarkupText;
  165. } else {
  166. return wrapBlockHTML(toolTipTextStyle, "<div style=\"" + nameStyle + ";" + tooltipLineHeight + ";\">" + encodeHTML(displayableHeader) + '</div>' + subMarkupText, topMarginForOuterGap);
  167. }
  168. }
  169. function buildNameValue(ctx, fragment, topMarginForOuterGap, toolTipTextStyle) {
  170. var renderMode = ctx.renderMode;
  171. var noName = fragment.noName;
  172. var noValue = fragment.noValue;
  173. var noMarker = !fragment.markerType;
  174. var name = fragment.name;
  175. var useUTC = ctx.useUTC;
  176. var valueFormatter = fragment.valueFormatter || ctx.valueFormatter || function (value) {
  177. value = isArray(value) ? value : [value];
  178. return map(value, function (val, idx) {
  179. return makeValueReadable(val, isArray(valueTypeOption) ? valueTypeOption[idx] : valueTypeOption, useUTC);
  180. });
  181. };
  182. if (noName && noValue) {
  183. return;
  184. }
  185. var markerStr = noMarker ? '' : ctx.markupStyleCreator.makeTooltipMarker(fragment.markerType, fragment.markerColor || tokens.color.secondary, renderMode);
  186. var readableName = noName ? '' : makeValueReadable(name, 'ordinal', useUTC);
  187. var valueTypeOption = fragment.valueType;
  188. var readableValueList = noValue ? [] : valueFormatter(fragment.value, fragment.dataIndex);
  189. var valueAlignRight = !noMarker || !noName;
  190. // It little weird if only value next to marker but far from marker.
  191. var valueCloseToMarker = !noMarker && noName;
  192. var _a = getTooltipTextStyle(toolTipTextStyle, renderMode),
  193. nameStyle = _a.nameStyle,
  194. valueStyle = _a.valueStyle;
  195. return renderMode === 'richText' ? (noMarker ? '' : markerStr) + (noName ? '' : wrapInlineNameRichText(ctx, readableName, nameStyle))
  196. // Value has commas inside, so use ' ' as delimiter for multiple values.
  197. + (noValue ? '' : wrapInlineValueRichText(ctx, readableValueList, valueAlignRight, valueCloseToMarker, valueStyle)) : wrapBlockHTML(toolTipTextStyle, (noMarker ? '' : markerStr) + (noName ? '' : wrapInlineNameHTML(readableName, !noMarker, nameStyle)) + (noValue ? '' : wrapInlineValueHTML(readableValueList, valueAlignRight, valueCloseToMarker, valueStyle)), topMarginForOuterGap);
  198. }
  199. /**
  200. * @return markupText. null/undefined means no content.
  201. */
  202. export function buildTooltipMarkup(fragment, markupStyleCreator, renderMode, orderMode, useUTC, toolTipTextStyle) {
  203. if (!fragment) {
  204. return;
  205. }
  206. var builder = getBuilder(fragment);
  207. var ctx = {
  208. useUTC: useUTC,
  209. renderMode: renderMode,
  210. orderMode: orderMode,
  211. markupStyleCreator: markupStyleCreator,
  212. valueFormatter: fragment.valueFormatter
  213. };
  214. return builder(ctx, fragment, 0, toolTipTextStyle);
  215. }
  216. function getGap(gapLevel) {
  217. return {
  218. html: HTML_GAPS[gapLevel],
  219. richText: RICH_TEXT_GAPS[gapLevel]
  220. };
  221. }
  222. function wrapBlockHTML(textStyle, encodedContent, topGap) {
  223. var clearfix = '<div style="clear:both"></div>';
  224. var marginCSS = "margin: " + topGap + "px 0 0";
  225. var tooltipLineHeight = getTooltipLineHeight(textStyle);
  226. return "<div style=\"" + marginCSS + ";" + tooltipLineHeight + ";\">" + encodedContent + clearfix + '</div>';
  227. }
  228. function wrapInlineNameHTML(name, leftHasMarker, style) {
  229. var marginCss = leftHasMarker ? 'margin-left:2px' : '';
  230. return "<span style=\"" + style + ";" + marginCss + "\">" + encodeHTML(name) + '</span>';
  231. }
  232. function wrapInlineValueHTML(valueList, alignRight, valueCloseToMarker, style) {
  233. // Do not too close to marker, considering there are multiple values separated by spaces.
  234. var paddingStr = valueCloseToMarker ? '10px' : '20px';
  235. var alignCSS = alignRight ? "float:right;margin-left:" + paddingStr : '';
  236. valueList = isArray(valueList) ? valueList : [valueList];
  237. return "<span style=\"" + alignCSS + ";" + style + "\">"
  238. // Value has commas inside, so use ' ' as delimiter for multiple values.
  239. + map(valueList, function (value) {
  240. return encodeHTML(value);
  241. }).join('&nbsp;&nbsp;') + '</span>';
  242. }
  243. function wrapInlineNameRichText(ctx, name, style) {
  244. return ctx.markupStyleCreator.wrapRichTextStyle(name, style);
  245. }
  246. function wrapInlineValueRichText(ctx, values, alignRight, valueCloseToMarker, style) {
  247. var styles = [style];
  248. var paddingLeft = valueCloseToMarker ? 10 : 20;
  249. alignRight && styles.push({
  250. padding: [0, 0, 0, paddingLeft],
  251. align: 'right'
  252. });
  253. // Value has commas inside, so use ' ' as delimiter for multiple values.
  254. return ctx.markupStyleCreator.wrapRichTextStyle(isArray(values) ? values.join(' ') : values, styles);
  255. }
  256. export function retrieveVisualColorForTooltipMarker(series, dataIndex) {
  257. var style = series.getData().getItemVisual(dataIndex, 'style');
  258. var color = style[series.visualDrawType];
  259. return convertToColorString(color);
  260. }
  261. export function getPaddingFromTooltipModel(model, renderMode) {
  262. var padding = model.get('padding');
  263. return padding != null ? padding
  264. // We give slightly different to look pretty.
  265. : renderMode === 'richText' ? [8, 10] : 10;
  266. }
  267. /**
  268. * The major feature is generate styles for `renderMode: 'richText'`.
  269. * But it also serves `renderMode: 'html'` to provide
  270. * "renderMode-independent" API.
  271. */
  272. var TooltipMarkupStyleCreator = /** @class */function () {
  273. function TooltipMarkupStyleCreator() {
  274. this.richTextStyles = {};
  275. // Notice that "generate a style name" usually happens repeatedly when mouse is moving and
  276. // a tooltip is displayed. So we put the `_nextStyleNameId` as a member of each creator
  277. // rather than static shared by all creators (which will cause it increase to fast).
  278. this._nextStyleNameId = getRandomIdBase();
  279. }
  280. TooltipMarkupStyleCreator.prototype._generateStyleName = function () {
  281. return '__EC_aUTo_' + this._nextStyleNameId++;
  282. };
  283. TooltipMarkupStyleCreator.prototype.makeTooltipMarker = function (markerType, colorStr, renderMode) {
  284. var markerId = renderMode === 'richText' ? this._generateStyleName() : null;
  285. var marker = getTooltipMarker({
  286. color: colorStr,
  287. type: markerType,
  288. renderMode: renderMode,
  289. markerId: markerId
  290. });
  291. if (isString(marker)) {
  292. return marker;
  293. } else {
  294. if (process.env.NODE_ENV !== 'production') {
  295. assert(markerId);
  296. }
  297. this.richTextStyles[markerId] = marker.style;
  298. return marker.content;
  299. }
  300. };
  301. /**
  302. * @usage
  303. * ```ts
  304. * const styledText = markupStyleCreator.wrapRichTextStyle([
  305. * // The styles will be auto merged.
  306. * {
  307. * fontSize: 12,
  308. * color: 'blue'
  309. * },
  310. * {
  311. * padding: 20
  312. * }
  313. * ]);
  314. * ```
  315. */
  316. TooltipMarkupStyleCreator.prototype.wrapRichTextStyle = function (text, styles) {
  317. var finalStl = {};
  318. if (isArray(styles)) {
  319. each(styles, function (stl) {
  320. return extend(finalStl, stl);
  321. });
  322. } else {
  323. extend(finalStl, styles);
  324. }
  325. var styleName = this._generateStyleName();
  326. this.richTextStyles[styleName] = finalStl;
  327. return "{" + styleName + "|" + text + "}";
  328. };
  329. return TooltipMarkupStyleCreator;
  330. }();
  331. export { TooltipMarkupStyleCreator };