rt-sim-training-client/src/utils/runPlan.js

329 lines
9.9 KiB
JavaScript
Raw Normal View History

2020-12-15 11:18:15 +08:00
import store from '@/store/index';
2019-08-09 17:25:45 +08:00
import { timeFormat } from '@/utils/date';
2019-07-02 16:29:52 +08:00
/** 创建一个车次数据点*/
export function createMartPoint(opt) {
const rotate = opt.directionCode == '2' ? 45 : (opt.directionCode == '1' ? -45 : 0);
2019-11-12 17:27:30 +08:00
const position = opt.type ? 'insideBottomLeft' : 'insideTopLeft';
return {
coord: opt.coord,
name: opt.name,
label: {
normal: {
rotate: rotate,
formatter: '{b}',
backgroundColor: 'rgb(242,242,242,0.1)',
color: 'black',
position: position
}
}
};
}
/** 创建一个车次数据点*/
export function createMartPointReverse(opt) {
const rotate = opt.directionCode == '1' ? 45 : (opt.directionCode == '2' ? -45 : 0);
const position = opt.type ? 'insideBottomLeft' : 'insideTopLeft';
2019-11-12 17:27:30 +08:00
return {
coord: opt.coord,
name: opt.name,
label: {
normal: {
rotate: rotate,
formatter: '{b}',
backgroundColor: 'rgb(242,242,242,0.1)',
color: 'black',
position: position
}
}
};
2019-07-02 16:29:52 +08:00
}
/** 创建一个车次数据序列*/
export function createSeriesModel(opt, lineStyle) {
2019-11-12 17:27:30 +08:00
if (opt) {
return {
z: opt.z || 5,
zlevel: opt.zlevel || 0,
type: 'line',
name: opt.name,
data: opt.data,
sampling: 'average',
lineStyle: lineStyle || {},
markPoint: {
symbol: 'roundRect',
symbolSize: 1,
data: opt.markPointData
}
};
}
2019-07-02 16:29:52 +08:00
}
/** 创建标记横线*/
2020-11-18 13:15:28 +08:00
export function createMarkLineModels(stations, computedYaxis, opt = {}) {
2019-11-12 17:27:30 +08:00
const markLineModel = {};
if (stations && stations.length) {
markLineModel.type = 'line';
markLineModel.name = 'markline';
markLineModel.markLine = {};
markLineModel.markLine.silent = true;
markLineModel.markLine.data = [];
markLineModel.markLine.lineStyle = { color: '#B0C4DE', width: 0.5 };
2020-11-03 16:18:15 +08:00
markLineModel.markLine.symbol = 'none';
markLineModel.elements = [];
2020-11-18 13:15:28 +08:00
Object.assign(markLineModel, opt);
2019-11-12 17:27:30 +08:00
stations.forEach((elem, index) => {
markLineModel.markLine.data.push(
{
label: {
show: true,
position: 'start',
formatter: elem.name,
color: 'black'
},
lineStyle: {
type: 'solid',
width: 0.5,
opacity: 0.5
},
yAxis: computedYaxis(elem, index)
}
);
});
}
return markLineModel;
2019-07-02 16:29:52 +08:00
}
/** 创建不会重复颜色的内部对象*/
2019-07-29 16:03:14 +08:00
export const hexColor = {
2019-11-12 17:27:30 +08:00
colorIndex: 0,
difValue: 0.25, // 一般为0.25
oddColor: null,
eveColor: null,
oldColor: null,
newColor: null,
colorList: [
'#000000', '#0000FF', '#8A2BE2', '#A52A2A', '#DEB887', '#5F9EA0', '#7FFF00', '#FF7F50', '#6495ED', '#DC143C',
'#00FFFF', '#008B8B', '#B8860B', '#BDB76B', '#8B008B', '#FF8C00', '#9932CC', '#8FBC8F', '#FF1493', '#00BFFF',
'#FF00FF', '#FFD700', '#FF69B4', '#FF4500', '#DB7093', '#4169E1', '#6A5ACD', '#00FF7F', '#EE82EE', '#40E0D0'
],
colors: [
'#B9C671', '#6C9040', '#79C671', '#71A5C6', '#C6A071', '#71C689', '#'
],
randomHsl: function () {
const h = Math.random();
const s = Math.random();
const l = Math.random();
return [h, s, l];
},
hslToRgb: function (h, s, l) {
let r, g, b;
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
if (s === 0) {
r = g = b = l; // achromatic
} else {
const hue2rgb = function hue2rgb(p, q, t) {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1 / 6) return p + (q - p) * 6 * t;
if (t < 1 / 2) return q;
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
return p;
};
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1 / 3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1 / 3);
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
},
rgbToHsl: function (r, g, b) {
// eslint-disable-next-line no-sequences
r /= 255, g /= 255, b /= 255;
const max = Math.max(r, g, b); const min = Math.min(r, g, b);
let h; let s; const l = (max + min) / 2;
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
if (max === min) {
h = s = 0; // achromatic
} else {
const d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch (max) {
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
return [h, s, l];
},
// 固定颜色
colorFixed() {
var color = this.colorList[this.colorIndex++ % this.colorList.length];
return color;
},
// 随机颜色
colorRandom() {
return '#' + (Math.random() * 0xffffff << 0).toString(16);
},
// 生成和前一个不同的随机颜色
colorContrast() {
this.newColor = this.randomHsl(); // 获取随机的hsl,并且给一个默认的hsl
this.newColor[1] = 0.7 + this.newColor[1] * 0.2; // [0.7 - 0.9] 排除过灰颜色
this.newColor[2] = 0.4 + this.newColor[2] * 0.2; // [0.4 - 0.8] 排除过亮过暗色
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
/** 如果oldColor不为空时要根据车次号保证两次生成的颜色差值为difValue*/
this.oldColor = Number(this.colorIndex) % 2 ? this.oddColor : this.eveColor;
if (this.oldColor) {
/** 保证本次的颜色和上次的不一致*/
for (let i = 0; i < this.newColor.length && i < this.oldColor.length; i++) {
if (i === 0 && Math.abs(this.newColor[i].toFixed(2) - this.oldColor[i].toFixed(2)) < this.difValue) {
this.colorRandom();
break;
}
}
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
/** 保存之前的颜色状态*/
if (Number(this.colorIndex) % 2) {
this.oddColor = this.newColor;
} else {
this.eveColor = this.newColor;
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
this.colorIndex += 1;
return `#${this.hslToRgb(...this.newColor).map(e => { return Number(e).toString(16); }).join('')}`;
},
// 渐进颜色
colorProgressiveColor() {
},
toCreate: function () {
return this.colorRandom();
}
2019-07-22 09:33:11 +08:00
};
2019-07-02 16:29:52 +08:00
/** 对list数据进行排序, 相同元素保持原有顺序*/
2019-07-29 16:03:14 +08:00
export function sortListByCallBack(list, callback) {
2019-11-12 17:27:30 +08:00
list.map((elem, index) => { elem[`oldIndex`] = index; });
list.sort((a, b) => {
return callback(a, b) || a.oldIndex - b.oldIndex;
});
return list;
2019-07-02 16:29:52 +08:00
}
/** 将数字转换成asc码*/
2019-07-29 16:03:14 +08:00
export function numToAsc(num) {
2019-11-12 17:27:30 +08:00
const nmA = 'A'.charCodeAt(0);
const nmZ = 'Z'.charCodeAt(0);
const len = nmZ - nmA + 1;
let str = '';
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
while (num >= 0) {
str = String.fromCharCode(num % len + nmA) + str;
num = Math.floor(num / len) - 1;
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
return str;
2019-07-02 16:29:52 +08:00
}
/** 将asc码转换成数字*/
2019-07-29 16:03:14 +08:00
export function ascToNum(asc) {
2019-11-12 17:27:30 +08:00
const base = 'A'.charCodeAt() - 1;
let idx = asc.length - 1;
let num = 0;
let mulFactor = 1;
while (idx >= 0) {
num += (asc[idx].charCodeAt() - base) * mulFactor;
mulFactor *= 26;
idx -= 1;
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
return num;
2019-07-02 16:29:52 +08:00
}
/** 根据索引获取单元格的数据*/
export function getCellValue(Sheet, index) {
2019-11-12 17:27:30 +08:00
let value;
const cell = Sheet[index];
if (cell) {
value = cell.w || cell.v;
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
return value;
2019-07-02 16:29:52 +08:00
}
/** 转换sheet数据为json数据*/
2019-07-29 16:03:14 +08:00
export function convertSheetToList(Sheet, isReverse) {
2019-11-12 17:27:30 +08:00
const dataList = [];
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
if (Sheet) {
const refarea = Sheet['!ref'];
const regular = /([a-zA-Z]+)([0-9]+):([a-zA-Z]+)([0-9]+)/i;
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
if (refarea == null) return []; // "A1:M698"
if (regular.test(refarea)) {
/** 正则转换解析行列数据*/
const CoordList = regular.exec(refarea);
/** 转换数据为二维数组*/
const colBeg = ascToNum(CoordList[1]);
const colEnd = ascToNum(CoordList[3]);
const rowBeg = Number(CoordList[2]);
const rowEnd = Number(CoordList[4]);
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
if (isReverse) {
for (let i = colBeg - 1; i < colEnd; i++) {
dataList.push([]);
for (let j = rowBeg; j <= rowEnd; j++) {
dataList[dataList.length - 1].push(getCellValue(Sheet, numToAsc(i) + j));
}
}
} else {
for (let i = rowBeg; i <= rowEnd; i++) {
dataList.push([]);
for (let j = colBeg - 1; j < colEnd; j++) {
dataList[dataList.length - 1].push(getCellValue(Sheet, numToAsc(j) + i));
}
}
}
}
}
2019-07-02 16:29:52 +08:00
2019-11-12 17:27:30 +08:00
return dataList;
2019-07-02 16:29:52 +08:00
}
2019-08-09 17:25:45 +08:00
/** 通过time将时间格式化*/
export function formatTime(time) {
2019-11-12 17:27:30 +08:00
if (Number.isInteger(time)) {
return timeFormat(time);
} else {
return '';
}
2019-08-09 17:25:45 +08:00
}
/** 通过code将名称格式化*/
export function formatName(code) {
2019-11-12 17:27:30 +08:00
let name = '';
const device = store.getters['map/getDeviceByCode'](code);
if (device) {
name = device.name;
}
return name;
2019-08-09 17:25:45 +08:00
}
/** 将时间格式化前补零*/
export function prefixTime(time) {
2019-11-12 17:27:30 +08:00
let str = `${time}` || '';
if (str) {
const list = str.split(':');
str = list.map(elem => {
return `00000${elem}`.substr(-2);
}).join(':');
}
2019-08-09 17:25:45 +08:00
2019-11-12 17:27:30 +08:00
return str;
2019-08-09 17:25:45 +08:00
}