echart力导向图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="GBK">
    <title>Title</title>
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/echarts.min.js"></script>
    <script>
        $(document).ready(function(){
            var myChart = echarts.init(document.getElementById('piedemo'));
            option = {
                tooltip: {
                    show: true,
                },
                legend: {
                    x: "center",
                    y:'30px',
                    data: ["家人", "朋友"]
                },
                animation: false,
                series: [{
                    categories: [{
                        name: '家人',
                        itemStyle: {
                            normal: {
                                color: "#009800",
                            }
                        }
                    }, {
                        name: '朋友',
                        itemStyle: {
                            normal: {
                                color: "#4592FF",
                            }
                        }
                    }],
                    type: 'graph',
                    layout: 'force',
                    symbol: "roundRect",
                    symbolSize: 50,
                    roam: true, //禁止用鼠标滚轮缩小放大效果
                    edgeSymbol: ['circle', 'arrow'],
                    edgeSymbolSize: [0, 10],
                    // 连接线上的文字
                    focusNodeAdjacency: true, //划过只显示对应关系
                    edgeLabel: {
                        normal: {
                            show: true,
                            textStyle: {
                                fontSize: 18
                            },
                            formatter: "{c}"
                        }
                    },
                    categories: [{
                        name: '家人',
                        itemStyle: {
                            normal: {
                                color: "#009800",
                            }
                        },
                    }, {
                        name: '朋友',
                        itemStyle: {
                            normal: {
                                color: "#4592FF",
                            }
                        }
                    }],
                    lineStyle: {
                        normal: {
                            opacity: 1,
                             2,
                            curveness: 0
                        }
                    },
                    force: {
                        repulsion: 5000
                    },
                    label: {
                        normal: {
                            show: true,
                            position : ['20%','38%'],//标签的位置。['50%', '50%'][x,y]
                            textStyle:{
                                color:"#fff"
                            }
                        }
                    },
                    data: [{
                        name: '中爆数字',
                        symbol: "Rect",
                        symbolSize: [250,30],
                        label: {
                            normal: {
                                show: true,
                                position : ['40%', '20%'],//标签的位置。['50%', '50%'][x,y]
                            }
                        }
                    }, {
                        name: '节点2',
                        category: 1,
                        itemStyle: {
                            normal: {
                                color: '#090',
                            },
                            emphasis: {
                                color: "#000"
                            }
                        }
                    }, {
                        name: '节点3',
                        category: 1
                    }, {
                        name: '节点4',
                        category: 0
                    }, {
                        name: '节点5',
                        category: 0
                    }, {
                        name: '节点6',
                        category: 0
                    }],
                    links: [{
                        source: '节点2',
                        target: 0,
                        value: "朋友",
                        lineStyle: {
                            normal: {
                                color: '#38f',
                                curveness: 0 // 线的弯曲度 0-1之间 越大则歪曲度更大
                            }
                        },
                        label: {
                            normal: {
                                textStyle: {
                                    color: '#07ac72'
                                }
                            }
                        }
                    }, {
                        source: '节点3',
                        target: 0,
                        value: "朋友"
                    }, {
                        source:  '节点4',
                        target: 0,
                        value: "家人"
                    }, {
                        source:  '节点5',
                        target: 0,
                        value: "家人"
                    }, {
                        source:  '节点6',
                        target: 0,
                        value: "家人"
                    } ]
                }]
            };

            myChart.setOption(option);
            myChart.on('dblclick', openOrFold);

            function appendPath(params){    //拼接节点关系并显示在左下角,
                var option = myChart.getOption();
                var series = option.series[params.seriesIndex]; //获取图表
                var links= series.links;//获取所有连线
                if(params.dataType=="node"){ //dataType可以是edge(线条)或node(数据)
                    var str = params.data.name;
                    for( i = 0 ; i < links.length; i++){
                        if(links[i].source==str){
                            str =links[i].source+"->" +series.data[links[i].target].name ;
                        }
                    }
                    return str;
                }else if(params.dataType=="edge"){ //当鼠标停留在连线上时,暂不处理
                    return "";
                }
            }

            function openOrFold(params) {  //该事件会提示节点间关系
                var str = appendPath(params);
                document.getElementById("main_1").innerHTML = str;
                var oW = $('#main_1').width()/2;
                $('#main_l').css({marginLeft:-oW});
                return str;
            }
        });
    </script>
    <style>
        .box{position: relative; 800px;
            height: 600px;
        margin: 40px auto;}
        #piedemo{800px;height:600px;margin: 40px auto;}
        #main_1{
            height: 20px;
             80%;
            text-align: center;
            background: rgba(243,112,0,.2);
            z-index: 10000;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <div class="box">
        <div id="main_1"></div>
        <div id="piedemo"></div>
    </div>
</body>
</html>
原创笔记
原文地址:https://www.cnblogs.com/minty/p/7216575.html