基于sankey图制作的社区变动图

基于sankey图制作了一个社区成员变迁图,矩形与link上的文字均可以经过拖动改变位置。

代码运行结果:


javascript源码:

<!DOCTYPE html>
<meta charset="utf-8">
<title>Sankey Prototype</title>
<style>
    text{
        display:block;
    }

    .link {
        fill: none;
        stroke: #18c0ff;
        stroke-opacity: .2;
    }

    .link:hover {
        stroke-opacity: .5;
    }


    .label {
        font-size: 8;
        fill: red;
        stroke: #000;
        stroke-opacity: .8;
    }
</style>
<body>

<p id="chart">

<script src="d3.js"></script>
<script src="sankey.js"></script>
<script>

var units = "Units";
var nodewidth=160;
var  margin = {top: 80, right: 10, bottom: 30, left: 310},
        width = 1500 - margin.left - margin.right,
        height = 750 - margin.top - margin.bottom;

var formatNumber = d3.format(",.0f"),    // zero decimal places
        format = function(d) { return formatNumber(d) + " " + units; },
        color = d3.scale.category20();


// append the svg canvas to the page
var svg = d3.select("#chart").append("svg")//svg选择id为chart部分
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        .attr("transform",
                "translate(" + (margin.left+20) + "," + margin.top + ")");

// Set the sankey diagram properties
var sankey = d3.sankey()
        .nodeWidth(nodewidth)//矩形宽度
        .nodePadding(10)//矩形垂直距离
        .size([width-40, height]);//布局的大小  布满g

var path = sankey.link();

var find_nodecolor=function(d,nodecolor){
    nodecolor.forEach(function(f,i){
        if(f.Community==d)  aa=f.color;
    });
    return aa;
};
// load the data with d3.csv instead of d3.json
//for another much simpler example uncomment the below
d3.csv("new_data/new_link.csv", function(error, data) {
    var nodecolor=[
        {
            "Community":"1_1",
            "color":"#1f77b4"
        },
        {
            "Community":"1_5",
            "color":"#ff7f0e"
        },
        {
            "Community":"1_6",
            "color":"#98df8a"
        },
        {
            "Community":"1_2",
            "color":"#9467bd"
        },
        {
            "Community":"1_10",
            "color":"#c49c94"
        },
        {
            "Community":"1_8",
            "color":"#f7b6d2&
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值