ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 更新D3圈包布局

2019-06-30 03:21:57  阅读:270  来源: 互联网

标签:javascript transition d3-js data-visualization circle-pack


我正在尝试使用json中收到的数据动态更新d3 circle pack布局.每一秒我都会调用d3.json()来获取新的json.我的实现只是在旧的实现下创建一个新的,而不是更新现有的可视化.我想动态更新现有的布局……

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="d3.v2.js"> 
</script>

<script type="text/javascript" src="jquery-1.4.min.js"></script>

<link rel="stylesheet" href="style.css" type="text/css">
<link rel="stylesheet" href="syntax.css" type="text/css">
<link rel="stylesheet" href="pack.css" type="text/css">

</head>

<body>

<div id="chart"> </div>
<script type="text/javascript">

    var width = 960,
        height = 960,
        format = d3.format(",d");

    var pack = d3.layout.pack()
        .size([width - 4, height -4])
        .value(function(d) { return d.size; });

    var vis = null;
    var node = null;

    vis = d3.select("#chart").append("svg")
    .attr("width", width)
    .attr("height", height)
    .attr("class", "pack");
/*      vis.append("g")
    .attr("transform", "translate(2, 2)"); */

    function update(json){


        // Creating the circle packed core
        var gNodes = vis.data([json]).selectAll("g.node")
          .data(pack.nodes);

               //remove old data
        gNodes.exit().remove();


    }


setInterval(function(){
    d3.json("http://10.0.1.4:8080/cluster", function(json) {        
        update(json);
//update the visualization

        vis
          .selectAll("circle")
          .data([json]).selectAll("g.node")
          .data(pack.nodes)
          .attr("class", function(d) { return d.children ? "node" : "leaf node"; })
          .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
          .transition()
          .duration(500)
          .attr("r", function(d) { return d.children ? coreSize : d.r; });

        var node = gNodes
          .enter().append("g")
          .attr("class", function(d) { return d.children ? "node" : "leaf node"; })
          .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
        node.append("title")
        .text(function(d) { return (d==null? "": d.name + (d.children ? "" : ": " + format(d.size))); });

        node.append("circle")
        .attr("r", function(d) { return (d==null? 0: d.r); });

        node.filter(function(d) { return (d==null? "" : !d.children); }).append("text")
        .attr("text-anchor", "middle")
        .attr("dy", ".3em")
        .text(function(d) { return (d==null?"":d.name.substring(0, d.r / 3)); });

    });
    }, 1000);

    </script>

解决方法:

看看我的例子here.

基本上,有初始加载的代码,其中所有圆,工具提示等都在初始位置创建和定位.同样,创建布局(包).

然后,在按下每个按钮时,将新数据加载到包中,然后重新计算包.那个关键代码在这里:

if (dataSource == 0)
    pack.value(function(d) { return d.size; });
if (dataSource == 1)
    pack.value(function(d) { return 100; });
if (dataSource == 2)
    pack.value(function(d) { return 1 +
             Math.floor(Math.random()*501); });

var data1 = pack.nodes(data);

(我有三个按钮,这就是为什么3个ifs)

之后,元素被转移到新的位置,其属性会在您确定时更改.

以下是一些过渡行动的照片:

开始:

过渡:

结束:

标签:javascript,transition,d3-js,data-visualization,circle-pack
来源: https://codeday.me/bug/20190630/1332327.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有