getlinexiaohaoData(param).then(res => {
const { vehicle, charging, delete: deleted, gasoline, dieselOil, gas, electric } = res.data.data;
const getUniqueNames = (data) => [...new Set(data.map(item => item.name))].sort();
const getDataSeries = (data, names, defaultValue = 0) => {
return names.map(name => {
const index = data.findIndex(item => item.name === name);
return index > -1 ? data[index].amount : defaultValue;
});
};
if (this.params.endTime === this.params.startTime) {
if (vehicle && vehicle.length) {
const uniqueNames = getUniqueNames(vehicle);
this.xdatas = uniqueNames;
this.lineseries[0] = {
name: '消耗金额',
type: 'line',
smooth: false,
data: getDataSeries(vehicle, uniqueNames)
};
} else {
this.lineseries = [];
}
} else {
const combined = [...charging, ...deleted];
this.xdatas = getUniqueNames(combined);
this.lineseries[0] = {
name: '消耗金额',
type: 'line',
smooth: false,
data: getDataSeries(charging, this.xdatas)
};
this.lineseries[1] = {
name: '添加金额',
type: 'line',
smooth: false,
data: getDataSeries(deleted, this.xdatas)
};
const addSeriesData = (data, seriesName) => {
if (data && data.length) {
let seriesData = [];
this.xdatas.forEach((item, index) => {
const indexInData = data.findIndex(item1 => item === item1.name);
if (indexInData > -1) {
data[indexInData].data.forEach((y) => {
seriesData.push({
name: `${data[indexInData].name}-${seriesData.length}`,
value: [index, y]
});
});
} else {
seriesData.push({ name: `${item}-0`, value: [index, '0'] });
}
});
this.lineseries.push({ name: seriesName, type: 'line', smooth: false, data: seriesData });
}
};
if (gasoline) addSeriesData(gasoline, '汽油');
if (dieselOil) addSeriesData(dieselOil, '柴油');
if (gas) addSeriesData(gas, '燃气');
if (electric) addSeriesData(electric, '电');
}
});