echarts使用dataset管理数据,模拟100万后端数据转化option。

效果图

image.png

后端数据格式使用的是比较通用的表格格式(很多前端表格插件都使用这种格式)

import { map, filter, isEqual, find, forEach, isNil, random } from 'lodash'
const createRandomRow = length => {
    const row = () => {
        const models = [
            'IPhone12mini', '小米10 Ultra', '华为Mate40', '红米K30 Ultra', 'IPhone 12 Pro', '真我X50 Pro', '小米9 Pro', '荣耀V20', 'IPad8', 'IPad Air4', 'IPad Mini5', 'IPad 11Pro 2',
            'Air Pods2', 'Air Pods Pro', 'Home Pod', 'Home Pod Mini'
        ]
        const date = `${random(2018, 2021, false)}-${random(1, 12, false)}-${random(1, 30, false)}`
        return { date, model: models[random(0, 15, false)], shipment: random(0, 10000, false) }
    }
    return map(new Array(length), () => row())
}
export const TASK_RESULT = {
    headers: [
        { name: 'date', label: '日期', type: 'dimension' },
        { name: 'model', label: '机型', type: 'dimension' },
        { name: 'shipment', label: '出货量', type: 'measure', unit: '台' },
    ],
    
    data: createRandomRow(10000 * 10),
}
export const createTwoDimensionOption = () => {
    const { headers, data } = TASK_RESULT
    console.time('生成图表')
    const [ xAxis, color ] = filter(headers, header => isEqual(header.type, 'dimension'))
    const measure = find(headers, header => isEqual(header.type, 'measure'))
    const option = {
        legend: {},
        tooltip: {
            trigger: 'axis',
        },
        xAxis: {type: 'category'},
        yAxis: {},
        dataset: {
            source: []
        },
    }
    const filterXAxisMap = new Map() 
    const xAxisList = [] 
    
    forEach(data, row => {
        const value = row[xAxis.name]
        if (!filterXAxisMap.has(value)) {
            filterXAxisMap.set(value, filterXAxisMap.size)
            xAxisList.push(value)
        }
    })
   
    option.dataset.source.push([xAxis.name, ...xAxisList])
    const sourceRowLength = xAxisList.length + 1
    const createEmptySourceRow = () => new Array(sourceRowLength).fill(0)
    const sourceColorRowLocationMap = new Map()
    forEach(data, row => {
        const xAxisValue = row[xAxis.name] 
        const colorValue = row[color.name] 
        const measureValue = row[measure.name] 
        const currentColorRowIndex = sourceColorRowLocationMap.get(colorValue)
        const currentMeasureInsertIndex = filterXAxisMap.get(xAxisValue) + 1
        if (isNil(currentColorRowIndex)) {
            const newSourceRow = createEmptySourceRow()
            newSourceRow[0] = colorValue
            newSourceRow[currentMeasureInsertIndex] = measureValue
            option.dataset.source.push(newSourceRow)
            sourceColorRowLocationMap.set(colorValue, sourceColorRowLocationMap.size + 1)
        } else {
            option.dataset.source[currentColorRowIndex][currentMeasureInsertIndex] = measureValue
        }
    })
    console.timeEnd('生成图表')
    option.series = new Array(option.dataset.source.length - 1).fill({type: 'bar', smooth: true, seriesLayoutBy: 'row'})
    return option
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容