记录改改直接拿来用的echarts force layout

其实echarts自由度不是很大的科学可视化还是老老实实用D3比较妥帖

不过案例倒是挺多的 (https://ecomfe.github.io/echarts-examples/public/index.html#chart-type-graph)

学习成本基本是零

基本上我改了改这个例子就拿来直接用了

<!DOCTYPE html>
<html style="height: 100%">
   <head>
       <meta charset="utf-8">
   </head>
   <body style="height: 100%; margin: 0">
       <div id="container" style="height: 100%"></div>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts-all-3.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts-stat/ecStat.min.js"></script>
       <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/extension/dataTool.min.js"></script>
       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
       <script type="text/javascript">
var dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
app.title = 'Force Layout';

myChart.showLoading();
$.get('influence_data.gexf', function (xml) {
    myChart.hideLoading();

    var graph = echarts.dataTool.gexf.parse(xml);
    var categories = [
      {name: 'Horror'},
      {name: 'Romance'},
      {name: 'Both'},
      {name: 'None'}
    ];
    graph.links.forEach(function (link) {
        link.lineStyle.normal.width = 2 * Math.sqrt(link.lineStyle.normal.width)
    });
    graph.nodes.forEach(function (node) {
        node.symbolSize = 10 * Math.sqrt(node.symbolSize);
        node.value = node.symbolSize;
        node.category = node.attributes.modularity_class;
        // Use random x, y
        node.x = node.y = null;
        node.draggable = true;
    });
    option = {
        title: {
            text: 'Social Influence',
            subtext: 'Default layout',
            top: 'bottom',
            left: 'right'
        },
        tooltip: {},
        legend: [{
            // selectedMode: 'single',
            // data: categories.map(function (a) {
            //     return a.name;
            // })
            data: categories.map(function (a) {
                return {
                  name: a.name,
                  icon: 'circle'
                  /*icon: {
                    name: 'circle'//'roundRect'
                  }*/
                };
            }),
            show: false
        }],
        animation: false,
        series : [
            {
                name: 'Les Miserables',
                type: 'graph',
                layout: 'force',
                data: graph.nodes,
                links: graph.links,
                edgeSymbol: ['none', 'arrow'], //['circle', 'arrow'],
                edgeSymbolSize: 10,
                categories: categories,
                roam: true,
                label: {
                    normal: {
                        position: 'right'
                    }
                },
                force: {
                    repulsion: 120, // 100
                    gravity: 0.01, //0.1,
                    edgeLength: 30,
                    layoutAnimation: true
                }
            }
        ]
    };

    myChart.setOption(option);
}, 'xml');;
if (option && typeof option === "object") {
    myChart.setOption(option, true);
}
       </script>
   </body>
</html>

至于gexf怎么生成看 贫尼之前的碎碎念

你要是没我这么懒对样式要求不多的话也许这个就够用吧


对了虽然我觉得大家都已经知道

但是还是多废话两句

直接打开这个html (假设名字是graph-force.html)是跑不出数据来的,你需要python的httpserver

打开terminal (或者command line, whatever),cd 到包含这个html的目录底下,输入

python -m SimpleHTTPServer


即可在chrome输入url

http://localhost:8000/graph-force.html


然后就能看到图

不谢

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值