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