将json数据动态加载到D3节点中

时间:2015-02-10 12:51:05

标签: javascript d3.js force-layout

我是一个javascript / d3新手。我试图将我的servlet中的json数据解析到我的javascript页面 - 然后使用d3 forcelayout设计(http://bl.ocks.org/mbostock/950642)。

我能够读取放置在我的项目中的json文件(在下面的asterix中突出显示)。但是我希望用户能够按下按钮来更新网络(从java中读取的数据)并直接从我的数据变量加载数据。因此,最初加载页面时不会显示网络。

注意:我在retrievejson()函数和document.getElementById(“demo”)中收到一个响应.innerHTML将我格式化的json粘贴到页面上。但是,我无法将我的数据变量加载到.nodes和.links中。有人可以告诉我如何做到这一点吗?网页上显示的json片段显示在帖子的底部。

请参阅下面的相关HTML代码。

var xmlhttp = new XMLHttpRequest();
var data = "no data";

function loadjson(){
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            retrievedjson(xmlhttp.responseText);
        }
    }
    xmlhttp.open("Get", "/nodenetwork/DemoServlet", true);
    xmlhttp.send();
}


function retrievedjson(response) {
    alert("got response");
    var json = JSON.parse(response);
    data = response;
    document.getElementById("demo").innerHTML = data;
}

var width = 960,
    height = 500

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

var force = d3.layout.force()
    .nodes(data.nodes)
    .links(data.links)
    .gravity(.05)
    .distance(100)
    .charge(-100)
    .size([width, height]);

    *** This works fine when not commented out ** <!--
    d3.json("json/data.json", function(error, json) {
  force
      .nodes(json.nodes)
      .links(json.links)
      .start();
   -->

  var link = svg.selectAll(".link")
      .data(json.links)
    .enter().append("line")
      .attr("class", "link");

  var node = svg.selectAll(".node")
      .data(json.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("image")
      .attr("xlink:href", "https://github.com/favicon.ico")
      .attr("x", -8)
      .attr("y", -8)
      .attr("width", 16)
      .attr("height", 16);

  node.append("text")
      .attr("dx", 12)
      .attr("dy", ".35em")
      .text(function(d) { return d.sourceName });

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  });
});

JSON片段:

{"nodes":[{"sourceName":"Citrix","targets":[]},{"sourceName":"Neon","targets":[]},{"sourceName":"Unknown","targets":[]},{"sourceName":"Clayton","targets":[{"targetSystem":"Citrix","interfaceData":"Frequent 3.4","teams":"H"}]}],
"links":[{"source": 1,"target": 0, "value": 1},{"source": 2,"target": 0, "value": 1},{"source": 2,"target": 1, "value": 1}]}

谢谢:)

1 个答案:

答案 0 :(得分:2)

&#13;
&#13;
var xmlhttp = new XMLHttpRequest();
var data = "no data";

function loadjson(){
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            retrievedjson(xmlhttp.responseText);
        }
    }
    xmlhttp.open("Get", "/nodenetwork/DemoServlet", true);
    xmlhttp.send();
}


function retrievedjson(response) {
    alert("got response");
    var json = JSON.parse(response);
    data = response;
    document.getElementById("demo").innerHTML = data;
}

var width = 960,
    height = 500

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);
var data = {"nodes":[{"sourceName":"Citrix","targets":[]},{"sourceName":"Neon","targets":[]},{"sourceName":"Unknown","targets":[]},{"sourceName":"Clayton","targets":[{"targetSystem":"Citrix","interfaceData":"Frequent 3.4","teams":"H"}]}],
"links":[{"source": 1,"target": 0, "value": 1},{"source": 2,"target": 0, "value": 1},{"source": 2,"target": 1, "value": 1}]};

var force = d3.layout.force()
    .nodes(data.nodes)
    .links(data.links)
    .gravity(.05)
    .distance(100)
    .charge(-100)
    .size([width, height]);
json = data;
    
    //d3.json("json/data.json", function(error, json) {
  force
      .nodes(json.nodes)
      .links(json.links)
      .start();

  var link = svg.selectAll(".link")
      .data(json.links)
    .enter().append("line")
      .attr("class", "link");

  var node = svg.selectAll(".node")
      .data(json.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("image")
      .attr("xlink:href", "https://github.com/favicon.ico")
      .attr("x", -8)
      .attr("y", -8)
      .attr("width", 16)
      .attr("height", 16);

  node.append("text")
      .attr("dx", 12)
      .attr("dy", ".35em")
      .text(function(d) { return d.sourceName });

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  });
//});
&#13;
.link {
  stroke: #ccc;
}

.node text {
  pointer-events: none;
  font: 10px sans-serif;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
&#13;
&#13;

 var force = d3.layout.force()
    .nodes(data.nodes)
    .links(data.links)
    .gravity(.05)
    .distance(100)
    .charge(-100)
    .size([width, height]);

force.nodes(json.nodes)
      .links(json.links)
      .start();

  var link = svg.selectAll(".link")
      .data(json.links)
    .enter().append("line")
      .attr("class", "link");

  var node = svg.selectAll(".node")
      .data(json.nodes)
    .enter().append("g")
      .attr("class", "node")
      .call(force.drag);

  node.append("image")
      .attr("xlink:href", "https://github.com/favicon.ico")
      .attr("x", -8)
      .attr("y", -8)
      .attr("width", 16)
      .attr("height", 16);

  node.append("text")
      .attr("dx", 12)
      .attr("dy", ".35em")
      .text(function(d) { return d.sourceName });

  force.on("tick", function() {
    link.attr("x1", function(d) { return d.source.x; })
        .attr("y1", function(d) { return d.source.y; })
        .attr("x2", function(d) { return d.target.x; })
        .attr("y2", function(d) { return d.target.y; });

    node.attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; });
  });

将上面的代码保存在函数传递数据(响应)作为此函数的参数中,我认为这可能对你有用... 如果没有,请求更多。