cione-comp-lib
Version:
`$ yarn add cione-comp-lib` 或者 `$ npm i cione-comp-lib -S`
144 lines (143 loc) • 5.69 kB
JavaScript
import { isString, normalizeCssArray as normalizeCssArray$1, isArray, each, isObject, isStringSafe, isNumber, trim } from "../../../zrender/lib/core/util.mjs";
import { encodeHTML } from "../../../zrender/lib/core/dom.mjs";
export { encodeHTML } from "../../../zrender/lib/core/dom.mjs";
import { isNumeric, parseDate, numericToNumber } from "./number.mjs";
import { pad, format } from "./time.mjs";
function addCommas(x) {
if (!isNumeric(x)) {
return isString(x) ? x : "-";
}
var parts = (x + "").split(".");
return parts[0].replace(/(\d{1,3})(?=(?:\d{3})+(?!\d))/g, "$1,") + (parts.length > 1 ? "." + parts[1] : "");
}
function toCamelCase(str, upperCaseFirst) {
str = (str || "").toLowerCase().replace(/-(.)/g, function(match, group1) {
return group1.toUpperCase();
});
if (upperCaseFirst && str) {
str = str.charAt(0).toUpperCase() + str.slice(1);
}
return str;
}
var normalizeCssArray = normalizeCssArray$1;
function makeValueReadable(value, valueType, useUTC) {
var USER_READABLE_DEFUALT_TIME_PATTERN = "{yyyy}-{MM}-{dd} {HH}:{mm}:{ss}";
function stringToUserReadable(str) {
return str && trim(str) ? str : "-";
}
function isNumberUserReadable(num) {
return !!(num != null && !isNaN(num) && isFinite(num));
}
var isTypeTime = valueType === "time";
var isValueDate = value instanceof Date;
if (isTypeTime || isValueDate) {
var date = isTypeTime ? parseDate(value) : value;
if (!isNaN(+date)) {
return format(date, USER_READABLE_DEFUALT_TIME_PATTERN, useUTC);
} else if (isValueDate) {
return "-";
}
}
if (valueType === "ordinal") {
return isStringSafe(value) ? stringToUserReadable(value) : isNumber(value) ? isNumberUserReadable(value) ? value + "" : "-" : "-";
}
var numericResult = numericToNumber(value);
return isNumberUserReadable(numericResult) ? addCommas(numericResult) : isStringSafe(value) ? stringToUserReadable(value) : typeof value === "boolean" ? value + "" : "-";
}
var TPL_VAR_ALIAS = ["a", "b", "c", "d", "e", "f", "g"];
var wrapVar = function(varName, seriesIdx) {
return "{" + varName + (seriesIdx == null ? "" : seriesIdx) + "}";
};
function formatTpl(tpl, paramsList, encode) {
if (!isArray(paramsList)) {
paramsList = [paramsList];
}
var seriesLen = paramsList.length;
if (!seriesLen) {
return "";
}
var $vars = paramsList[0].$vars || [];
for (var i = 0; i < $vars.length; i++) {
var alias = TPL_VAR_ALIAS[i];
tpl = tpl.replace(wrapVar(alias), wrapVar(alias, 0));
}
for (var seriesIdx = 0; seriesIdx < seriesLen; seriesIdx++) {
for (var k = 0; k < $vars.length; k++) {
var val = paramsList[seriesIdx][$vars[k]];
tpl = tpl.replace(wrapVar(TPL_VAR_ALIAS[k], seriesIdx), encode ? encodeHTML(val) : val);
}
}
return tpl;
}
function formatTplSimple(tpl, param, encode) {
each(param, function(value, key) {
tpl = tpl.replace("{" + key + "}", encode ? encodeHTML(value) : value);
});
return tpl;
}
function getTooltipMarker(inOpt, extraCssText) {
var opt = isString(inOpt) ? {
color: inOpt,
extraCssText
} : inOpt || {};
var color = opt.color;
var type = opt.type;
extraCssText = opt.extraCssText;
var renderMode = opt.renderMode || "html";
if (!color) {
return "";
}
if (renderMode === "html") {
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:' + 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>';
} else {
var markerId = opt.markerId || "markerX";
return {
renderMode,
content: "{" + markerId + "|} ",
style: type === "subItem" ? {
width: 4,
height: 4,
borderRadius: 2,
backgroundColor: color
} : {
width: 10,
height: 10,
borderRadius: 5,
backgroundColor: color
}
};
}
}
function formatTime(tpl, value, isUTC) {
if (tpl === "week" || tpl === "month" || tpl === "quarter" || tpl === "half-year" || tpl === "year") {
tpl = "MM-dd\nyyyy";
}
var date = parseDate(value);
var getUTC = isUTC ? "getUTC" : "get";
var y = date[getUTC + "FullYear"]();
var M = date[getUTC + "Month"]() + 1;
var d = date[getUTC + "Date"]();
var h = date[getUTC + "Hours"]();
var m = date[getUTC + "Minutes"]();
var s = date[getUTC + "Seconds"]();
var S = date[getUTC + "Milliseconds"]();
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));
return tpl;
}
function capitalFirst(str) {
return str ? str.charAt(0).toUpperCase() + str.substr(1) : str;
}
function convertToColorString(color, defaultColor) {
defaultColor = defaultColor || "transparent";
return isString(color) ? color : isObject(color) ? color.colorStops && (color.colorStops[0] || {}).color || defaultColor : defaultColor;
}
function windowOpen(link, target) {
if (target === "_blank" || target === "blank") {
var blank = window.open();
blank.opener = null;
blank.location.href = link;
} else {
window.open(link, target);
}
}
export { addCommas, capitalFirst, convertToColorString, formatTime, formatTpl, formatTplSimple, getTooltipMarker, makeValueReadable, normalizeCssArray, toCamelCase, windowOpen };