format.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 * as zrUtil from 'zrender/lib/core/util.js';
  41. import { encodeHTML } from 'zrender/lib/core/dom.js';
  42. import { parseDate, isNumeric, numericToNumber } from './number.js';
  43. import { format as timeFormat, pad } from './time.js';
  44. import { deprecateReplaceLog } from './log.js';
  45. /**
  46. * Add a comma each three digit.
  47. */
  48. export function addCommas(x) {
  49. if (!isNumeric(x)) {
  50. return zrUtil.isString(x) ? x : '-';
  51. }
  52. var parts = (x + '').split('.');
  53. return parts[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g, '$1,') + (parts.length > 1 ? '.' + parts[1] : '');
  54. }
  55. export function toCamelCase(str, upperCaseFirst) {
  56. str = (str || '').toLowerCase().replace(/-(.)/g, function (match, group1) {
  57. return group1.toUpperCase();
  58. });
  59. if (upperCaseFirst && str) {
  60. str = str.charAt(0).toUpperCase() + str.slice(1);
  61. }
  62. return str;
  63. }
  64. export var normalizeCssArray = zrUtil.normalizeCssArray;
  65. export { encodeHTML };
  66. /**
  67. * Make value user readable for tooltip and label.
  68. * "User readable":
  69. * Try to not print programmer-specific text like NaN, Infinity, null, undefined.
  70. * Avoid to display an empty string, which users can not recognize there is
  71. * a value and it might look like a bug.
  72. */
  73. export function makeValueReadable(value, valueType, useUTC) {
  74. var USER_READABLE_DEFUALT_TIME_PATTERN = '{yyyy}-{MM}-{dd} {HH}:{mm}:{ss}';
  75. function stringToUserReadable(str) {
  76. return str && zrUtil.trim(str) ? str : '-';
  77. }
  78. function isNumberUserReadable(num) {
  79. return !!(num != null && !isNaN(num) && isFinite(num));
  80. }
  81. var isTypeTime = valueType === 'time';
  82. var isValueDate = value instanceof Date;
  83. if (isTypeTime || isValueDate) {
  84. var date = isTypeTime ? parseDate(value) : value;
  85. if (!isNaN(+date)) {
  86. return timeFormat(date, USER_READABLE_DEFUALT_TIME_PATTERN, useUTC);
  87. } else if (isValueDate) {
  88. return '-';
  89. } // In other cases, continue to try to display the value in the following code.
  90. }
  91. if (valueType === 'ordinal') {
  92. return zrUtil.isStringSafe(value) ? stringToUserReadable(value) : zrUtil.isNumber(value) ? isNumberUserReadable(value) ? value + '' : '-' : '-';
  93. } // By default.
  94. var numericResult = numericToNumber(value);
  95. return isNumberUserReadable(numericResult) ? addCommas(numericResult) : zrUtil.isStringSafe(value) ? stringToUserReadable(value) : typeof value === 'boolean' ? value + '' : '-';
  96. }
  97. var TPL_VAR_ALIAS = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
  98. var wrapVar = function (varName, seriesIdx) {
  99. return '{' + varName + (seriesIdx == null ? '' : seriesIdx) + '}';
  100. };
  101. /**
  102. * Template formatter
  103. * @param {Array.<Object>|Object} paramsList
  104. */
  105. export function formatTpl(tpl, paramsList, encode) {
  106. if (!zrUtil.isArray(paramsList)) {
  107. paramsList = [paramsList];
  108. }
  109. var seriesLen = paramsList.length;
  110. if (!seriesLen) {
  111. return '';
  112. }
  113. var $vars = paramsList[0].$vars || [];
  114. for (var i = 0; i < $vars.length; i++) {
  115. var alias = TPL_VAR_ALIAS[i];
  116. tpl = tpl.replace(wrapVar(alias), wrapVar(alias, 0));
  117. }
  118. for (var seriesIdx = 0; seriesIdx < seriesLen; seriesIdx++) {
  119. for (var k = 0; k < $vars.length; k++) {
  120. var val = paramsList[seriesIdx][$vars[k]];
  121. tpl = tpl.replace(wrapVar(TPL_VAR_ALIAS[k], seriesIdx), encode ? encodeHTML(val) : val);
  122. }
  123. }
  124. return tpl;
  125. }
  126. /**
  127. * simple Template formatter
  128. */
  129. export function formatTplSimple(tpl, param, encode) {
  130. zrUtil.each(param, function (value, key) {
  131. tpl = tpl.replace('{' + key + '}', encode ? encodeHTML(value) : value);
  132. });
  133. return tpl;
  134. }
  135. export function getTooltipMarker(inOpt, extraCssText) {
  136. var opt = zrUtil.isString(inOpt) ? {
  137. color: inOpt,
  138. extraCssText: extraCssText
  139. } : inOpt || {};
  140. var color = opt.color;
  141. var type = opt.type;
  142. extraCssText = opt.extraCssText;
  143. var renderMode = opt.renderMode || 'html';
  144. if (!color) {
  145. return '';
  146. }
  147. if (renderMode === 'html') {
  148. return type === 'subItem' ? '<span style="display:inline-block;vertical-align:middle;margin-right:8px;margin-left:3px;' + 'border-radius:4px;width:4px;height:4px;background-color:' // Only support string
  149. + encodeHTML(color) + ';' + (extraCssText || '') + '"></span>' : '<span style="display:inline-block;margin-right:4px;' + 'border-radius:10px;width:10px;height:10px;background-color:' + encodeHTML(color) + ';' + (extraCssText || '') + '"></span>';
  150. } else {
  151. // Should better not to auto generate style name by auto-increment number here.
  152. // Because this util is usually called in tooltip formatter, which is probably
  153. // called repeatedly when mouse move and the auto-increment number increases fast.
  154. // Users can make their own style name by theirselves, make it unique and readable.
  155. var markerId = opt.markerId || 'markerX';
  156. return {
  157. renderMode: renderMode,
  158. content: '{' + markerId + '|} ',
  159. style: type === 'subItem' ? {
  160. width: 4,
  161. height: 4,
  162. borderRadius: 2,
  163. backgroundColor: color
  164. } : {
  165. width: 10,
  166. height: 10,
  167. borderRadius: 5,
  168. backgroundColor: color
  169. }
  170. };
  171. }
  172. }
  173. /**
  174. * @deprecated Use `time/format` instead.
  175. * ISO Date format
  176. * @param {string} tpl
  177. * @param {number} value
  178. * @param {boolean} [isUTC=false] Default in local time.
  179. * see `module:echarts/scale/Time`
  180. * and `module:echarts/util/number#parseDate`.
  181. * @inner
  182. */
  183. export function formatTime(tpl, value, isUTC) {
  184. if (process.env.NODE_ENV !== 'production') {
  185. deprecateReplaceLog('echarts.format.formatTime', 'echarts.time.format');
  186. }
  187. if (tpl === 'week' || tpl === 'month' || tpl === 'quarter' || tpl === 'half-year' || tpl === 'year') {
  188. tpl = 'MM-dd\nyyyy';
  189. }
  190. var date = parseDate(value);
  191. var getUTC = isUTC ? 'getUTC' : 'get';
  192. var y = date[getUTC + 'FullYear']();
  193. var M = date[getUTC + 'Month']() + 1;
  194. var d = date[getUTC + 'Date']();
  195. var h = date[getUTC + 'Hours']();
  196. var m = date[getUTC + 'Minutes']();
  197. var s = date[getUTC + 'Seconds']();
  198. var S = date[getUTC + 'Milliseconds']();
  199. tpl = tpl.replace('MM', pad(M, 2)).replace('M', M).replace('yyyy', y).replace('yy', pad(y % 100 + '', 2)).replace('dd', pad(d, 2)).replace('d', d).replace('hh', pad(h, 2)).replace('h', h).replace('mm', pad(m, 2)).replace('m', m).replace('ss', pad(s, 2)).replace('s', s).replace('SSS', pad(S, 3));
  200. return tpl;
  201. }
  202. /**
  203. * Capital first
  204. * @param {string} str
  205. * @return {string}
  206. */
  207. export function capitalFirst(str) {
  208. return str ? str.charAt(0).toUpperCase() + str.substr(1) : str;
  209. }
  210. /**
  211. * @return Never be null/undefined.
  212. */
  213. export function convertToColorString(color, defaultColor) {
  214. defaultColor = defaultColor || 'transparent';
  215. return zrUtil.isString(color) ? color : zrUtil.isObject(color) ? color.colorStops && (color.colorStops[0] || {}).color || defaultColor : defaultColor;
  216. }
  217. export { truncateText } from 'zrender/lib/graphic/helper/parseText.js';
  218. /**
  219. * open new tab
  220. * @param link url
  221. * @param target blank or self
  222. */
  223. export function windowOpen(link, target) {
  224. /* global window */
  225. if (target === '_blank' || target === 'blank') {
  226. var blank = window.open();
  227. blank.opener = null;
  228. blank.location.href = link;
  229. } else {
  230. window.open(link, target);
  231. }
  232. }
  233. export { getTextRect } from '../legacy/getTextRect.js';