function getDateTime(date) {
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h=h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
minute = minute < 10 ? ('0' + minute) : minute;
var second=date.getSeconds();
second=second < 10 ? ('0' + second) : second;
return y + '-' + m + '-' + d-
+' '+h+':'+minute+':'+second;
}
function timeFormat(str) {
if (typeof str == 'string') {
return str.slice(0,4)+'-'+str.slice(4,6)+'-'+str.slice(6);
} else if (Array.isArray(str)) {
for (let i=0; i< str.length; i++) {
str[i]=str[i].slice(0,4)+'-'+str[i].slice(4,6)+'-'+str[i].slice(6);
}
return str;
}
}
function getUpDateTime(date, type) {
// 前一月
if (type === 'month') {
date.setMonth(date.getMonth() - 1);
let y=date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
return y + '-' + m;
}
// 前一天
date.setTime(date.getTime()-24*60*60*1000);
let y=date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
return y + '-' + m + '-' + d
}
function inspectStae(state) {
// 开发过程中发现有'null'的情况
return (state == null)?'--':state
}
function getNumber(numValue) {
// 判空
if (!numValue || numValue === 'null') {
return "0";
}
let typeIs = typeof(numValue);
if(numValue){
if(typeIs === 'number'){
if (numValue > 0) {
return this.bigNumberTransform(numValue);
}
return `-${this.bigNumberTransform(Math.abs(numValue))}`;
}else{
return numValue;
}
}else{
return "0";
}
}
// 千分位分隔符
function toThousands(num) {
return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
}
function bigNumberTransform(value,flag) {
// 接口中碰到返回数据为null的情况
if (value === null || value === undefined||value<0) {
return 0;
}
const newValue = ['', '', '']
let fr = 1000
let num = 3
let text1 = ''
let fm = 1
let reg = RegExp(/%/);
if(value.toString().match(reg)) {
return value
}else{
while (value / fr >= 1) {
fr *= 10
num += 1
}
if (num <= 4) { // 千
newValue[0] = value//parseInt(value / 1000) + ''
newValue[1] = ''
} else if (num <= 8) { // 万
text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万'
fm = text1 === '万' ? 10000 : 10000000
if (value % fm === 0) {
newValue[0] = parseInt(value / fm) + ''
} else {
newValue[0] = flag =='tofixed2'? parseFloat(value / fm).toFixed(2) + '':parseFloat(value / fm).toFixed(4) + ''
}
newValue[1] = text1
} else if (num <= 16) { // 亿
text1 = (num - 8) / 3 > 1 ? '千亿' : '亿'
text1 = (num - 8) / 4 > 1 ? '万亿' : text1
text1 = (num - 8) / 7 > 1 ? '千万亿' : text1
fm = 1
if (text1 === '亿') {
fm = 100000000
} else if (text1 === '千亿') {
fm = 100000000000
} else if (text1 === '万亿') {
fm = 1000000000000
} else if (text1 === '千万亿') {
fm = 1000000000000000
}
if (value % fm === 0) {
newValue[0] = parseInt(value / fm) + ''
} else {
newValue[0] = flag =='tofixed2'? parseFloat(value / fm).toFixed(2) + '':parseFloat(value / fm).toFixed(4)
}
newValue[1] = text1
}
if (value < 1000) {
newValue[0] = value + ''
newValue[1] = ''
}
return newValue.join('')
}
}
function numberFormat (value) {
let param = {};
let k = 10000,
sizes = ['', '万', '亿', '万亿'],
i;
if(value < k){
param.value =value
param.unit=''
}else{
i = Math.floor(Math.log(value) / Math.log(k));
param.value = ((value / Math.pow(k, i))).toFixed(2);
param.unit = sizes[i];
}
return param;
}
function initToThousands(num) {
return (num || 0).toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
}
function formatNumber(num) {
num = Number(num);
if (num == 0) {
return num + '';
}
// else
// if (num > 1 && num < 10000) {
// return num + '';
// } else {
// }
return (num / 10000)
}
// 处理金额,三个数字加一个逗号
function toThousands(value) {
var fn = (num) => {
var num = (num || 0).toString(), result = '';
while (num.length > 3) {
result = ',' + num.slice(-3) + result;
num = num.slice(0, num.length - 3);
}
if (num) { result = num + result; }
return result;
};
var valueStr = (value || 0).toString();
var arr = valueStr.split('.');
arr[0] = fn(arr[0]);
return arr.join('.');
}
// 四舍五入保留两位小数, 位数不够用0替补
function keepTwoDecimalFull(num) {
var result = parseFloat(num);
if (isNaN(result)) {
return NaN;
}
result = Math.round(result * 100) / 100;
var s_x = result.toString();
var pos_decimal = s_x.indexOf('.');
if (pos_decimal < 0) {
pos_decimal = s_x.length;
s_x += '.';
}
while (s_x.length <= pos_decimal + 2) {
s_x += '0';
}
return s_x;
}
/**
* 依据控件大小改变动态配置坐标轴展示效果
* @param type 图例类型 bar/line
* @param labelArray x轴数据
* @param valueArrays y轴数据 折线图多个图例会出现嵌套数组
* @param poistion 拖动结束返回的当前控件长宽属性
*/
function reactiveDrag(type,labelArray,valueArrays,poistion){
if (!labelArray || !labelArray.length){
return {
rotate:50,
interval:0
}
}
//控件可调整下限
const controlMinWidth = 156;
const controlMinHeight = 451.25;
const controldftWidth = 451.25;
const controldftHeight = 198;
// debugger
let valueArray = [...getArrayItem(valueArrays)];
//y轴最长长度
let YAxleLenPx = Math.max(...valueArray).toString().length *9;
//x轴长度
let XAxleLenPx = labelArray.reduce((prev, curr) => {
let prevLength = prev.toString().length;
let currLength = curr.toString().length;
return prevLength >= currLength ? prevLength : currLength
})*10;
//控件宽高
const {width,height} = poistion;
//计算图例高度
const legendWidth = width * 0.94 - YAxleLenPx;
let activeConfig = {};
//x轴label 偏移量
activeConfig.rotate = function(){
// debugger
if (labelArray.length >= 15){
return 50
}
//0.625 font-size换算比例
return legendWidth < labelArray.length * (XAxleLenPx) * 0.625 ? 50 : 0;
}();
//最小分割间隔
activeConfig.interval = function () {
//x轴预估长度
const labelsLength = labelArray.length * 0.625 * XAxleLenPx * 10;
if (labelsLength > legendWidth && labelArray.length >= 15 ){
//计算倍数
return Math.round(labelsLength / legendWidth)-1;
}else{
return 0
}
}();
//分割段数
activeConfig.splitNumber = function () {
let actInterval = height >= 400 ? 100 : 50
if (height < controldftHeight){
return 4;
}
return height/actInterval
}();
//返回 interval 和 rotate 和 splitNumber 属性。
return activeConfig;
}
/**
* 拆分嵌套数组
* @param array
* @returns {IterableIterator<*>}
*/
function* getArrayItem(array) {
if (Array.isArray(array)) {
for (let i = 0; i < array.length; i++) {
getArrayItem(array)
}
} else {
yield array
}
}
/**
* 区别于原有的数据格式化函数
* 小于四位数不做单位格式化,只控制小数位数
* 运算规则:根据现有图形最多展示8位数字,小数据点后保留2位。
单位 数据 规则 例
元 元 小于4位数不做操作
万元 万 大于等于4位除以10000 9.21万元
万元 十万 大于等于4位除以10000 19.21万元
万元 百万 大于等于4位除以10000 190.21万元
万元 千万 大于等于4位除以10000 1231.33万元
亿元 亿 大于等于9位除以100000000 1.32亿元
亿元 百亿 大于等于9位除以100000000 19.21亿元
亿元 千亿 大于等于13位除以 1000000000000 190.21亿元
亿元 万亿 大于等于13位除以 1000000000000 1231.33亿元
* @param value 非负数
* @returns {string|number|*}
*/
function unitFormat4Tree(value) {
// 接口中碰到返回数据为null的情况
if (value === null || value === undefined || value < 0) {
return 0;
}
const newValue = ['', '', '']
let fr = 1000
let num = 3
let text1 = ''
let fm = 1
let reg = RegExp(/%/);
if (value.toString().match(reg)) {
return value
} else {
while (value / fr >= 1) {
fr *= 10
num += 1
}
if (num <= 4) { // 千
// newValue[0] = parseInt(value / 1000) + ''
// newValue[1] = '千'
newValue[0] = parseFloat(value).toFixed(2);
newValue[1] = ""
} else if (num <= 8) { // 万
text1 = '万元'
fm = 10000;
if (value % fm === 0) {
newValue[0] = parseInt(value / fm) + ''
} else {
newValue[0] = parseFloat(value / fm).toFixed(2) + ''
}
newValue[1] = text1
} else if (num <= 16) { // 亿
text1 = '亿元'
fm = 100000000
if (value % fm === 0) {
newValue[0] = parseInt(value / fm) + ''
} else {
newValue[0] = parseFloat(value / fm).toFixed(2) + ''
}
newValue[1] = text1
}
return newValue.join('')
}
}
module.exports = {
getDateTime,
inspectStae,
timeFormat,
getUpDateTime,
numberFormat,
initToThousands,
getNumber,
formatNumber,
unitFormat4Tree,
toThousands,
bigNumberTransform,
toThousands,
keepTwoDecimalFull,
reactiveDrag,
}
public.js(src/common/public.js)自用
最新推荐文章于 2024-05-20 15:17:08 发布