tooltipMarkup.js 13 KB

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