rt-sim-training-client/src/jmapNew/theme/parser/EqualRatioParser.js

307 lines
13 KiB
JavaScript
Raw Normal View History

2021-03-17 13:35:15 +08:00
import { createSeriesModel, createMarkLineModels, createMartPoint, createMartPointReverse, hexColor } from './util';
2020-12-15 11:18:15 +08:00
import store from '@/store/index';
2021-03-17 13:35:15 +08:00
const defaultConfig = {
/** 最小时间*/
minTime: 0,
/** 最大时间*/
maxTime: 3600 * 24 - 1,
/** 边缘高度*/
edge: 600,
/** 间隔高度*/
multiple: 1,
/** 偏移时间*/
translation: 0
};
2021-03-17 13:35:15 +08:00
class EqualRatioParser {
constructor() {
}
/** 加载配置*/
load(config = defaultConfig) {
this.config = config;
return this;
}
/** 解析excel数据转换为Json后台数据*/
importData(sheet, data) {
if (this.config &&
2021-03-17 13:35:15 +08:00
this.config.importData) {
2021-04-08 17:55:35 +08:00
return this.config.importData(sheet, data);
} else {
console.info('no import data function');
}
}
/** 将后台数据解析成图表*/
convertDataToModels(data, stations, kmRangeMap, lineStyle) {
var models = [];
if (data && data.serviceNumberDataList && data.serviceNumberDataList.length) {
/** 按服务遍历数据*/
data.serviceNumberDataList.forEach((service) => {
/** 按车次遍历数据*/
var opt = { name: '', markPointData: [], data: [] };
if (service.tripNumberDataList && service.tripNumberDataList.length) {
service.tripNumberDataList.forEach((train, j) => {
var pointdata = {};
var idx = 0;
var num = 0;
var lastPoint = null;
var nextPoint = null;
/** 如果车次号为空,不显示名称*/
if (train.tripNumber) {
/** 创建标记点名称和坐标*/
2020-08-31 13:25:26 +08:00
// pointdata.name = `${service.serviceNumber}${train.directionCode}${train.tripNumber}`;
pointdata.name = `${service.serviceNumber}${train.tripNumber}`;
pointdata.color = '#000' || lineStyle.color;
pointdata.directionCode = train.right ? '2' : '1';
if (!store.state.map.mapConfig.upRight) {
pointdata.directionCode = train.right ? '1' : '2';
}
2021-03-17 13:35:15 +08:00
pointdata.coord = [train.stationTimeList[1].secondTime, this.getCoordYByElem(stations, kmRangeMap, train.stationTimeList[1], pointdata.directionCode, false)];
/** 给服务对象添加服务名称和标记点*/
2021-03-17 13:35:15 +08:00
opt.markPointData.push(this.__createMartPoint(pointdata));
/** 创建服务号名称*/
opt.name = `${service.serviceNumber}`;
}
/** 计算非折返点车次点坐标集合*/
train.stationTimeList.forEach((elem, index) => {
idx = index;
2021-03-15 17:32:12 +08:00
// if (index == 0 && train.stationTimeList[index].stationCode != train.stationTimeList[index + 1].stationCode ||
// index == train.stationTimeList.length - 2 && train.stationTimeList[index].secondTime != train.stationTimeList[index + 1].secondTime ||
// index > 0 && index < train.stationTimeList.length - 1) {
const aa = `${train.tripNumber}`;
opt.data.push([elem.secondTime, this.getCoordYByElem(stations, kmRangeMap, elem, pointdata.directionCode, false), elem.stationCode, aa]);
2021-03-15 17:32:12 +08:00
// }
});
/** 计算折返点车次坐标点集合*/
if (!train.backup && train.reentry && service.tripNumberDataList[j + 1] && service.tripNumberDataList[j + 1].stationTimeList) {
const currentTimeList = service.tripNumberDataList[j].stationTimeList;
const nextTimeList = service.tripNumberDataList[j + 1].stationTimeList;
if (currentTimeList[currentTimeList.length - 1].secondTime != nextTimeList[0].secondTime) {
lastPoint = train.stationTimeList[train.stationTimeList.length - 1];
nextPoint = service.tripNumberDataList[j + 1].stationTimeList[0];
num = this.computedReentryNumber(train.tripNumber);
const aa = `${train.tripNumber}`;
opt.data.push([lastPoint.secondTime, this.getCoordYByElem(stations, kmRangeMap, lastPoint, pointdata.directionCode, true, num), lastPoint.stationCode, aa]);
opt.data.push([nextPoint.secondTime, this.getCoordYByElem(stations, kmRangeMap, nextPoint, pointdata.directionCode, true, num), nextPoint.stationCode, aa]);
}
}
/** 如果是备用车,按车次添加线*/
if (train.backup) {
/** 创建一条完成的服务数据*/
// opt.name += j;
const length = opt.name.length;
const optName = parseInt(opt.name) + j;
opt.name = optName.toString().padStart(length, '0');
2021-03-17 13:35:15 +08:00
var model = this.__createSeriesModel(opt, Object.assign({ color: hexColor.toCreate() }, lineStyle));
if (model) {
models.push(model);
opt = { name: '', markPointData: [], data: [] };
}
}
});
// 不是备用车,按服务添加线
if (!service.backup) {
/** 创建一条完成的服务数据*/
2021-03-17 13:35:15 +08:00
var model = this.__createSeriesModel(opt, Object.assign({ color: hexColor.toCreate() }, lineStyle));
if (model) {
models.push(model);
}
}
}
});
return models;
} else {
return [];
}
2021-03-17 13:35:15 +08:00
}
/** 更新数据并解析成图表*/
2021-03-17 13:35:15 +08:00
updateDataToModels(data, stations, kmRangeMap, runPlanData, series, lineStyle) {
if (data && data.length) {
data.forEach(elem => {
/** 判断此条记录的服务号是否存在*/
if (!runPlanData[elem.serviceNumber]) {
/** 创建一个新服务号标记*/
runPlanData[elem.serviceNumber] = {};
/** 不存在此服务号则需要创建一条新的line*/
2021-03-17 13:35:15 +08:00
series.push(this.__createSeriesModel({
zlevel: 1,
name: `run${elem.serviceNumber}`,
data: [],
markPointData: []
}, Object.assign({ color: hexColor.toCreate() }, lineStyle)));
}
/** 添加数据*/
series.forEach(serie => {
/** 找到服务号所在图数据的位置*/
if (serie.name == `run${elem.serviceNumber}`) {
/** 添加车组号记录标记*/
if (!runPlanData[elem.serviceNumber][elem.tripNumber]) {
runPlanData[elem.serviceNumber][elem.tripNumber] = [];
}
runPlanData[elem.serviceNumber][elem.tripNumber].push(elem);
runPlanData[elem.serviceNumber][elem.tripNumber].sort((a, b) => {
return parseInt(a.secondTime) - parseInt(b.secondTime);
});
let directionCode = elem.right ? '2' : '1';
if (!store.state.map.mapConfig.upRight) {
directionCode = elem.right ? '1' : '2';
}
/** 如果此记录车组号的数据为第一条时,则打上标签*/
if (runPlanData[elem.serviceNumber][elem.tripNumber].length <= 1) {
2020-08-28 11:30:55 +08:00
const result = serie.markPoint.data.some(ele => ele.name == `(${elem.groupNumber})${elem.serviceNumber}${elem.tripNumber}`);
if (!result) {
2021-03-17 13:35:15 +08:00
serie.markPoint.data.push(this.__createMartPoint({
directionCode: directionCode,
2021-03-17 13:35:15 +08:00
coord: [parseInt(elem.secondTime), this.getCoordYByElem(stations, kmRangeMap, elem, false)],
2020-08-31 13:25:26 +08:00
name: `(${elem.groupNumber})${elem.serviceNumber}${elem.tripNumber}`,
2020-08-28 11:30:55 +08:00
color: lineStyle.color || '#000'
}));
}
}
/** 计算折返点*/
2021-03-17 13:35:15 +08:00
var nextPoint = [parseInt(elem.secondTime), this.getCoordYByElem(stations, kmRangeMap, elem, false), directionCode];
if (serie.data.length > 0) {
var lastPoint = serie.data[serie.data.length - 1];
if (lastPoint[2] !== nextPoint[2]) {
var num = this.computedReentryNumber(elem.tripNumber);
2021-03-17 13:35:15 +08:00
serie.data.push([lastPoint[0], this.getValueYByDirectionCode(lastPoint[1], lastPoint[2], num), lastPoint[2]]);
serie.data.push([nextPoint[0], this.getValueYByDirectionCode(nextPoint[1], lastPoint[2], num), lastPoint[2]]);
}
}
/** 添加车组号数据到对应的服务图数据中*/
serie.data.push(nextPoint);
/** 保证原始数据排序*/
serie.data.sort((a, b) => {
return parseInt(a[0]) - parseInt(b[0]);
});
}
});
});
}
return series;
2021-03-17 13:35:15 +08:00
}
/** 初始化Y轴*/
2021-03-17 13:35:15 +08:00
initializeAxisY(stations) {
return this.__createMarkLineModels(stations, (elem) => {
2021-03-19 10:10:51 +08:00
return this.getEdge() + elem.kmRange * this.getMultiple();
});
2021-03-17 13:35:15 +08:00
}
2021-03-17 13:35:15 +08:00
getAxisYValueByStation(station, index) {
2021-03-19 10:10:51 +08:00
return this.getEdge() + station.kmRange * this.getMultiple();
2021-03-17 13:35:15 +08:00
}
2020-11-19 09:40:03 +08:00
/** 将后台数据转换为试图序列模型*/
convertStationsToMap(stations) {
var map = {};
if (stations && stations.length) {
2020-11-19 09:40:03 +08:00
stations.forEach((elem, index) => {
2021-03-17 13:35:15 +08:00
map[`${elem.kmRange}`] = this.getAxisYValueByStation(elem, index);
});
}
return map;
2021-03-17 13:35:15 +08:00
}
/** 计算y轴最小值*/
2021-03-17 13:35:15 +08:00
computedAxisYMinValue(stations) {
2021-03-19 10:10:51 +08:00
return stations[0].kmRange * this.getMultiple();
2021-03-17 13:35:15 +08:00
}
/** 计算y轴最大值*/
2021-03-17 13:35:15 +08:00
computedAxisYMaxValue(stations) {
2021-03-19 10:10:51 +08:00
return stations[stations.length - 1].kmRange * this.getMultiple() + this.getEdge() * 2;
2021-03-17 13:35:15 +08:00
}
/** 格式化y轴数据*/
2021-03-17 13:35:15 +08:00
computedFormatAxisY(stations, params) {
2020-01-07 14:49:51 +08:00
var yText = '0m';
stations.forEach(elem => {
2021-03-19 10:10:51 +08:00
if (elem.kmRange < parseInt(params.value) / this.getMultiple() - this.getEdge()) {
2020-01-07 14:49:51 +08:00
yText = Math.floor(elem.kmRange) + 'm';
}
});
return yText;
2021-03-17 13:35:15 +08:00
}
/** 根据是否和上一个车次是否相交,计算下一个车次的折返的高度*/
computedReentryNumber(code) {
2020-08-28 14:29:59 +08:00
return 1;
2021-03-17 13:35:15 +08:00
}
/** 根据方向计算y折返偏移量*/
2021-03-17 13:35:15 +08:00
getValueYByDirectionCode(defaultValue, directionCode, num) {
const sign = this.getReverse() ? -1 : 1;
if (directionCode === '1') {
2021-03-19 10:52:57 +08:00
defaultValue -= sign * this.getEdge() / 2 * num;
} else if (directionCode === '2') {
2021-03-19 10:52:57 +08:00
defaultValue += sign * this.getEdge() / 2 * num;
}
2021-03-17 13:35:15 +08:00
return defaultValue;
}
/** 根据elem计算y值*/
2021-03-17 13:35:15 +08:00
getCoordYByElem(stations, kmRangeMap, elem, directionCode, isSpecial, num) {
var defaultValue = 0;
var station = stations.find(it => { return it.code == elem.stationCode; });
if (station) {
2021-03-17 13:35:15 +08:00
defaultValue = kmRangeMap[`${station.kmRange}`];
if (isSpecial) {
2021-03-17 13:35:15 +08:00
defaultValue = this.getValueYByDirectionCode(defaultValue, directionCode, num);
}
}
2021-03-17 13:35:15 +08:00
return defaultValue;
}
2021-03-17 13:35:15 +08:00
getMultiple() {
return this.config.multiple;
}
2021-03-19 10:10:51 +08:00
getTranslation() {
return this.config.translation;
}
2021-03-19 10:10:51 +08:00
getEdge() {
return this.config.edge;
}
2021-03-19 10:10:51 +08:00
getReverse() {
return this.config.reverse;
}
2021-03-19 10:52:57 +08:00
__createMartPoint(...args) {
return this.config.reverse ? createMartPointReverse(...args) : createMartPoint(...args);
}
2021-03-17 13:35:15 +08:00
__createSeriesModel(...args) {
return createSeriesModel(...args);
}
2021-03-17 13:35:15 +08:00
__createMarkLineModels(...args) {
return createMarkLineModels(...args);
}
2021-03-17 13:35:15 +08:00
}
export default EqualRatioParser;