tooltipMarkup.js 13 KB

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