美文网首页Web
echarts使用dataset管理数据,模拟100万后端数据转

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

作者: 爱吃猫的老虎 | 来源:发表于2020-11-09 16:57 被阅读0次

效果图

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
}

相关文章

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

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

  • 计算机毕业设计之SpringBoot+Vue.js国内疫情实时追

    特色/创新点 websocket实时前后端数据交互显示 数据可视化-百度echarts的使用 springboot...

  • 2019-11-28

    关于mockjs模拟数据的使用 一、 本文主要结合webpack+vue+mockjs,前后端分离,模拟后端接口数...

  • 利用node.js来mock数据

    1.什么是mock数据? 模拟后端数据 2.mock数据解决的问题 使用mock数据可以在后端开发人员尚未完成接口...

  • mock

    mock使用优势:前后端同时开发的时候,后端接口数据没有出来,前端可以mock假数据,模拟开发 1,安装mock依...

  • json-server模拟数据

    json-server可以模拟后端提供的数据,让前端流程走通,实现前后端分离json-server模拟数据 大概流...

  • 计算机毕业设计Python+Spark+LSTM中药推荐系统 中

    开发技术 前端:vue.js、echarts 后端:springboot、vue.js 数据库:mysql 大数据...

  • Vue—Echarts 柱状图

    使用Vue做后台管理系统的时候避免不了使用Echarts来展示对应的数据,下面是使用Echarts柱状图来展示对应...

  • mockJs的使用

    模拟数据 mock,为前后端分离,提前准备数据,不必要等待后台提供数据。但是前提是要约定好数据结构。 使用方法: ...

  • ECharts - 数据图表的使用

    欢迎移步我的博客阅读:《ECharts - 数据图表的使用》 关于ECharts(ECharts) ECharts...

网友评论

    本文标题:echarts使用dataset管理数据,模拟100万后端数据转

    本文链接:https://www.haomeiwen.com/subject/titrbktx.html