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

Load json data into D3 nodes dynamically

我是 javascript / d3 新手。我正在尝试将 json 数据从我的 servlet 解析到我的 java 脚本页面 - 然后使用 d3 forcelayout 设计 (http://bl.ocks.org/mbostock/950642)。

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

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

的底部

请参阅下面的相关 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}]}

谢谢:)

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 + ")"; });
  });
//});
.link {
  stroke: #ccc;
}

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

 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 + ")"; });
  });

将上面的代码保留在函数中,将数据(响应)作为参数传递给该函数,我认为这对您有用... 如果没有,要求更多。