lineTotable.js 3.02 KB
/**
 * 组合头部数据
 * @param {Array} headData 
 */
function autoComplateHead(headData) {
    let asisOrgName = [],
        defaultHead = [];
    if (headData.length < 8) {
        asisOrgName = headData.map(item => item);
        for (let i = 0, len = 8 - headData.length; i < len; i++) {
            asisOrgName.push('--');
        }
        asisOrgName.forEach((k, index) => {
            defaultHead.push({
                prop: 'defaultOrg' + index,
                label: k
            })
        })
    } else {
        headData.forEach((k, index) => {
            defaultHead.push({
                prop: 'defaultOrg' + index,
                label: k
            })
        })
    }
    return defaultHead;
}

/**
 * 合并表头和表数据
 * @param {Array} data 
 * @param {Array} headData
 */
function dealData(data, headData) {
    let obj = {},
        arr = [],
        _prop = '';
    data.forEach(item => {
        // console.log(item.data.length < headData.length)
        if (item.length < headData.length) {
            for (let i = 0, len = item.length; i < len; i++) {
                _prop = headData[i].prop;
                obj[_prop] = (item[i] || item[i] == 0) ? item[i] : '--';
            }
            for (let j = item.length; j < headData.length; j++) {
                _prop = headData[j].prop;
                obj[_prop] = '--';
            }
        } else {
            for (let i = 0, len = item.length; i < len; i++) {
                _prop = headData[i].prop;
                obj[_prop] = (item[i] || item[i] == 0) ? item[i] : '--';
            }
        }
        arr.push(obj);
        obj = {};
    })
    return arr;
}

/**
 * 
 * @param {Object} data
 */
export const lineToTab = function(data) {
    let columnData = [],
        rowData = [],
        tabData = [],
        result = {};
    // x轴转化为表格列
    let pushLastArr = JSON.parse(JSON.stringify(data.xaxis.data));
    pushLastArr[pushLastArr.length] = '00:00';
    let tabFirstColumn = [];
    for (let i = 0, len = pushLastArr.length; i < len - 1; i++) {
        tabFirstColumn.push(pushLastArr[i] + '-' + pushLastArr[i + 1]);
    }
    // console.log('tabFirstColumn:', tabFirstColumn)
    // 组合table表头
    let tabHead = [];
    tabHead = data.series.map(item => {
        return item.name;
    })
    tabHead.unshift('时间段');
    let tabHeadData = autoComplateHead(tabHead);
    // console.log('tabHeadData:', tabHeadData);
    // 组合报表列数据
    data.series.forEach((item, index) => {
        for (let i = 0; i < item.data.length; i++) {
            rowData[i] = [];
        }
    });
    data.series.forEach((item, index) => {
        item.data.forEach((dataItem, dataIndex, dataArr) => {
            rowData[dataIndex].push(dataItem)
        })
    });
    // 合并第一列数据
    rowData.forEach((item, index) => {
        item.unshift(tabFirstColumn[index])
    })
    columnData = rowData;
    tabData = dealData(columnData, tabHeadData);
    result = {
        headData: tabHeadData,
        bodyData: tabData
    }
    return result
}