chartData.js 4.16 KB
import moment from 'moment'

const groupArray = (data) => {
    const temp = [];
    const obj = {};
    data.forEach(item => {
        // const index = temp.findIndex(y => y.pid === x.pid);
        if (!obj[item.unit]) {
            temp.push({
                unit: item.unit,
                name: item?.name,
                subList: [item]
            })
            obj[item.unit] = item
        } else {
            temp.forEach(i=>{
                if(i.unit === item.unit){
                    i.subList.push(item)
                }
            })
        }
    })
    return temp;
}

export const chartData = (data) => {
    if (!data || data?.length === 0) return {}
    const realData = groupArray(data)
    console.log(realData)
    const xData = []
    const yAxis = []
    const yyData = [];
    const nameData = []
    const ParamsBgColor = ['rgba(61,151,255,0.2)', 'rgba(245,119,35,0.2)', 'rgba(133,199,0,0.2)', 'rgba(90,159,0,0.2)', 'rgba(113,19,0,0.2)']
    const ParamsFontColor = ['rgba(61,151,255,1)', 'rgba(245,119,35,1)', 'rgba(133,199,0,1)', 'rgba(90,159,0,1)', 'rgba(113,19,0,1)']
    const colorArr = (index) => {
        const num = index % 5
        return {
            bg: ParamsBgColor[num],
            color: ParamsFontColor[num],
        };
    }

    realData[0]?.subList[0]?.trendIndexList?.forEach((item) => {
        const timestamp = moment(parseInt(item.xvalueDateTime, 10))
            .format("YYYY-MM-DD HH:mm:ss")
        xData.push(timestamp);
    });
    realData?.forEach((item, index) => {
        item?.subList?.forEach((items , indexs)=>{
            const yData = [];
            nameData.push(items?.name)
            items?.trendIndexList?.forEach(child=>{
                yData.push(child.yvalue);
            })
            yyData.push({
                name: items?.name,
                type: 'line',
                smooth: true,
                // lineStyle: {
                //     color: colorArr(indexs).color
                // },
                // itemStyle: {
                //     color: colorArr(indexs).color
                // },
                data: yData,
                yAxisIndex: index
            });
        })
        yAxis.push({
            type: 'value',
            name: item?.unit ? `单位:${item?.unit}` : '单位:--',
            axisTick: {
                lineStyle: {
                    type: "dashed"
                }
            },
            position: index > 0 ? 'right' : 'left',
            offset: index > 0 ? -80 * (index - 1) : 0,
            splitLine: {
                lineStyle: {
                    type: "dashed"
                }
            }
        })
    });
    return {
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: nameData || [],
            type: 'scroll'
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '10%',
            containLabel: true
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xData,
            axisTick: {
                show: false
            }
        },
        yAxis,
        color: ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
        dataZoom: [{
            type: 'inside',
            start: 0,
            end: 100,
            zoomLock: true
        }, {
            start: 0,
            end: 10,
            handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
            handleSize: '60%',
            handleStyle: {
                color: '#fff',
                shadowBlur: 3,
                shadowColor: 'rgba(0, 0, 0, 0.6)',
                shadowOffsetX: 2,
                shadowOffsetY: 2
            }
        }],
        series: yyData
    }
}

export const generateList = (data, dataList)=> {
    data?.forEach((item) => {
      const { name, id } = item;
      dataList.push({ id, name });
      if (item?.meterAttrList) {
        generateList(item.meterAttrList, dataList);
      }
    })
    return dataList
  };