echarts:导入excel生成桑葚图

前言

前两天帮别人实现了一个小功能,主要是选择excel文件,读取里面的数据,将数据生成桑葚图

echarts官方桑葚图案例

实现

因为就是一个单纯的html文件,用到的库都是通过CDN的方式加载的,会有一些慢

java 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #main {
            width: 800px;
            height: 600px;
            border: 1px solid red;
        }
    </style>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.5.1/dist/echarts.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.5/xlsx.full.min.js"></script>
</head>

<body>
    <input type="file" id="excelFileInput" />
    <div id="main"></div>

    <script type="text/javascript">


        // 解析excel
        document.getElementById('excelFileInput').addEventListener('change', function (event) {
            const file = event.target.files[0];
            const dom = document.getElementById('main')
            var myChart = echarts.init(dom);
            console.log("加载:", window.echarts, dom)
            if (file) {
                const reader = new FileReader();
                reader.onload = function (e) {
                    const data = new Uint8Array(e.target.result);
                    const workbook = XLSX.read(data, { type: 'array' });

                    // 假设我们要解析第一个工作表
                    const firstSheetName = workbook.SheetNames[0];
                    const worksheet = workbook.Sheets[firstSheetName];

                    // 将工作表转换为JSON
                    const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 });

                    console.log("json数据:", jsonData, jsonData.length)

                    // 自己的数据
                    const excelData = {
                        // 节点
                        nodes: [],
                        links: []
                    }

                    // 循环生成数据
                    for (let i = 1; i < jsonData.length; i++) {
                        // 生成节点,避免添加重复数据
                        if (!excelData.nodes.find(e => e.name == jsonData[i][0])) {
                            excelData.nodes.push({
                                name: jsonData[i][0]
                            })
                        }
                        if (!excelData.nodes.find(e => e.name == jsonData[i][1])) {
                            excelData.nodes.push({
                                name: jsonData[i][1]
                            })
                        }
                        // 生成线段
                        if (!excelData.nodes.find(e => e.source == jsonData[i][0] && e.target == jsonData[i][1])) {
                            excelData.links.push({
                                source: jsonData[i][0],
                                target: jsonData[i][1],
                                value: jsonData[i][2]
                            })
                        }

                    }
                    console.log("数据:", excelData)

                    // 指定图表的配置项和数据
                    var option = {
                        title: {
                            text: '桑基图',
                            left: 'center'
                        },
                        // 触发方式
                        tooltip: {
                            trigger: 'item',
                            triggerOn: 'mousemove'
                        },
                        series: [
                            {
                                // 图表类型
                                type: 'sankey',
                                // 节点
                                data: excelData.nodes,
                                // 线条
                                links: excelData.links,
                                emphasis: {
                                    focus: 'adjacency'
                                },
                                // 线的类型
                                lineStyle: {
                                    color: 'gradient',
                                    curveness: 0.5
                                }
                            }
                        ]
                    };

                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption(option);
                };

                reader.readAsArrayBuffer(file);
            }
        });


    </script>
</body>

</html>

excel文件格式

效果图

相关推荐
一条小鲨鱼14 分钟前
所遇到的响应式问题
前端·vue.js
M ? A16 分钟前
你的 Vue 路由,VuReact 会编译成什么样的 React 路由?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
L.Cheng17 分钟前
谷歌浏览器如何禁用自动更新_Chrome关闭后台升级程序
前端·chrome
donecoding23 分钟前
类型与语法的“直觉对齐”:TS 切入的 Go 语言初体验
前端·typescript·go
web守墓人24 分钟前
【linux】Mubuntu v1.0.7发布:支持codex cli完整运行
前端·codex
WYiQIU25 分钟前
宇树科技Web前端岗(AI方向),这不算泄题吧......
前端·vue.js·人工智能·笔记·科技·面试·职场和发展
Januea25 分钟前
Chrome的Fetch/XHR是什么?
前端·chrome
betazhou26 分钟前
TDSQL-PG创建测试表并定时插入数据模拟生产
前端·javascript·数据库·tdsql·tdsql-pg
Pentane.29 分钟前
【数据分析 | 农业项目】蔬菜类商品的自动定价与补货决策 | Tableau & Excel
数据挖掘·数据分析·excel·tableau
W.A委员会34 分钟前
地址栏输入url到显示画面
前端·网络