format.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. }
  90. // In other cases, continue to try to display the value in the following code.
  91. }
  92. if (valueType === 'ordinal') {
  93. return zrUtil.isStringSafe(value) ? stringToUserReadable(value) : zrUtil.isNumber(value) ? isNumberUserReadable(value) ? value + '' : '-' : '-';
  94. }
  95. // By default.
  96. var numericResult = numericToNumber(value);
  97. return isNumberUserReadable(numericResult) ? addCommas(numericResult) : zrUtil.isStringSafe(value) ? stringToUserReadable(value) : typeof value === 'boolean' ? value + '' : '-';
  98. }
  99. var TPL_VAR_ALIAS = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
  100. var wrapVar = function (varName, seriesIdx) {
  101. return '{' + varName + (seriesIdx == null ? '' : seriesIdx) + '}';
  102. };
  103. /**
  104. * Template formatter
  105. * @param {Array.<Object>|Object} paramsList
  106. */
  107. export function formatTpl(tpl, paramsList, encode) {
  108. if (!zrUtil.isArray(paramsList)) {
  109. paramsList = [paramsList];
  110. }
  111. var seriesLen = paramsList.length;
  112. if (!seriesLen) {
  113. return '';
  114. }
  115. var $vars = paramsList[0].$vars || [];
  116. for (var i = 0; i < $vars.length; i++) {
  117. var alias = TPL_VAR_ALIAS[i];
  118. tpl = tpl.replace(wrapVar(alias), wrapVar(alias, 0));
  119. }
  120. for (var seriesIdx = 0; seriesIdx < seriesLen; seriesIdx++) {
  121. for (var k = 0; k < $vars.length; k++) {
  122. var val = paramsList[seriesIdx][$vars[k]];
  123. tpl = tpl.replace(wrapVar(TPL_VAR_ALIAS[k], seriesIdx), encode ? encodeHTML(val) : val);
  124. }
  125. }
  126. return tpl;
  127. }
  128. /**
  129. * simple Template formatter
  130. */
  131. export function formatTplSimple(tpl, param, encode) {
  132. zrUtil.each(param, function (value, key) {
  133. tpl = tpl.replace('{' + key + '}', encode ? encodeHTML(value) : value);
  134. });
  135. return tpl;
  136. }
  137. export function getTooltipMarker(inOpt, extraCssText) {
  138. var opt = zrUtil.isString(inOpt) ? {
  139. color: inOpt,
  140. extraCssText: extraCssText
  141. } : inOpt || {};
  142. var color = opt.color;
  143. var type = opt.type;
  144. extraCssText = opt.extraCssText;
  145. var renderMode = opt.renderMode || 'html';
  146. if (!color) {
  147. return '';
  148. }
  149. if (renderMode === 'html') {
  150. 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:'
  151. // Only support string
  152. + 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>';
  153. } else {
  154. // Should better not to auto generate style name by auto-increment number here.
  155. // Because this util is usually called in tooltip formatter, which is probably
  156. // called repeatedly when mouse move and the auto-increment number increases fast.
  157. // Users can make their own style name by theirselves, make it unique and readable.
  158. var markerId = opt.markerId || 'markerX';
  159. return {
  160. renderMode: renderMode,
  161. content: '{' + markerId + '|} ',
  162. style: type === 'subItem' ? {
  163. width: 4,
  164. height: 4,
  165. borderRadius: 2,
  166. backgroundColor: color
  167. } : {
  168. width: 10,
  169. height: 10,
  170. borderRadius: 5,
  171. backgroundColor: color
  172. }
  173. };
  174. }
  175. }
  176. /**
  177. * @deprecated Use `time/format` instead.
  178. * ISO Date format
  179. * @param {string} tpl
  180. * @param {number} value
  181. * @param {boolean} [isUTC=false] Default in local time.
  182. * see `module:echarts/scale/Time`
  183. * and `module:echarts/util/number#parseDate`.
  184. * @inner
  185. */
  186. export function formatTime(tpl, value, isUTC) {
  187. if (process.env.NODE_ENV !== 'production') {
  188. deprecateReplaceLog('echarts.format.formatTime', 'echarts.time.format');
  189. }
  190. if (tpl === 'week' || tpl === 'month' || tpl === 'quarter' || tpl === 'half-year' || tpl === 'year') {
  191. tpl = 'MM-dd\nyyyy';
  192. }
  193. var date = parseDate(value);
  194. var getUTC = isUTC ? 'getUTC' : 'get';
  195. var y = date[getUTC + 'FullYear']();
  196. var M = date[getUTC + 'Month']() + 1;
  197. var d = date[getUTC + 'Date']();
  198. var h = date[getUTC + 'Hours']();
  199. var m = date[getUTC + 'Minutes']();
  200. var s = date[getUTC + 'Seconds']();
  201. var S = date[getUTC + 'Milliseconds']();
  202. 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));
  203. return tpl;
  204. }
  205. /**
  206. * Capital first
  207. * @param {string} str
  208. * @return {string}
  209. */
  210. export function capitalFirst(str) {
  211. return str ? str.charAt(0).toUpperCase() + str.substr(1) : str;
  212. }
  213. /**
  214. * @return Never be null/undefined.
  215. */
  216. export function convertToColorString(color, defaultColor) {
  217. defaultColor = defaultColor || 'transparent';
  218. return zrUtil.isString(color) ? color : zrUtil.isObject(color) ? color.colorStops && (color.colorStops[0] || {}).color || defaultColor : defaultColor;
  219. }
  220. export { truncateText } from 'zrender/lib/graphic/helper/parseText.js';
  221. /**
  222. * open new tab
  223. * @param link url
  224. * @param target blank or self
  225. */
  226. export function windowOpen(link, target) {
  227. /* global window */
  228. if (target === '_blank' || target === 'blank') {
  229. var blank = window.open();
  230. blank.opener = null;
  231. blank.location.href = link;
  232. } else {
  233. window.open(link, target);
  234. }
  235. }
  236. export { getTextRect } from '../legacy/getTextRect.js';