echarts 关系图

先放一张效果图
在这里插入图片描述
下面呈现代码

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>ECharts</title>
    <!-- 引入刚刚下载的 ECharts 文件 -->
    <script src="https://cdn.bootcdn.net/ajax/libs/echarts/5.1.2/echarts.js"></script>
  </head>
  <body>
    <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
    <div id="relationChart" style="width: 800px;height:800px;"></div>
    <script type="text/javascript">
        // 基于准备好的dom,初始化echarts实例
        myChart = echarts.init(document.getElementById('relationChart'));
        // 数据格式
        let data = {
            nodes: [{
                name: '王大妈',
                category: 0
            }, {
                name: '王二麻子',
                category: 1
            }, {
                name: '李四',
                category: 1
            }],
            links: [{
                source: '王大妈',
                target: '李四',
                count: '1'
            }, {
                source: '王大妈',
                target: '王二麻子',
                count: '3'
            }]
        }

        const color1 = '#fe4848';
        const color2 = '#5c85ff';

        // 节点配置
        data.nodes.forEach(node => {
            // 判断节点类型
            if (node.category === 0) {
                // 设置节点图片
                // node.symbol = 'image://'+"/images/fk-gxt-zs.png",
                // 节点图片大小
                node.symbolSize = 180;
                // 节点样式
                node.itemStyle = {
                    // 图形颜色
                    color: color1
                };
            } else if (node.category === 1) {
                // node.symbol = 'image://'+"/images/fk-gxt-qt.png",
                node.symbolSize = 100
                node.itemStyle = {
                    color: color2
                };
            }
        });

        // 节点关系数据
        data.links.forEach(link => {
            // 关系标签
            link.label = {
                // 文字水平居中
                align: 'center',
                // 字号
                fontSize: 12
            };
            // 关系线的样式
            link.lineStyle = {
                color: color2
            }
            
        });

        option = {
            series: [{
                type: 'graph',
                layout: 'force',
                // 节点是否可拖拽
                draggable: true,
                // 是否开启鼠标缩放和平移漫游
                roam: true,
                // 是否在鼠标移到节点上的时候突出显示节点以及节点的边和邻接节点
                focusNodeAdjacency: true,
                // edgeSymbol: ['', 'arrow'],  // 线两端的标记类型   arrow箭头
                // edgeSymbolSize: [80, 10],
                // 线标签配置
                edgeLabel: {
                    normal: {
                        // 是否显示
                        show: true,
                        // 标签位置;可选:'start' 线的起始点,'middle' 线的中点,'end' 线的结束点
                        position:'middle',
                        // 文本样式
                        textStyle: {
                            fontSize: 20,
                        },
                        // 标签文本的显示
                        formatter(x) {
                            return '数量'+x.data.count
                            // return x.data.name+x.data.value;
                        }
                    }
                },
                // 节点文本配置
                label: {
                    // 是否显示
                    show: true,
                    // 字号
                    fontSize:14,
                    // 文字偏移
                    offset: [0, 15],
                    // 显示文本
                    formatter:function(e){
                        return e.data.value
                    }
                },
                force: {
                    // 节点之间的斥力因子
                    repulsion: 2000,
                    // 线两边的节点距离
                    edgeLength: 300,
                },
                data: data.nodes,
                links: data.links
            }]
        }

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
        // 图表事件监听
        myChart.on('click',function(e){
            
        })

    </script>
  </body>
</html>

其他具体配置可查看官方文档:https://echarts.apache.org/zh/option.html#series-graph.type

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值