XML javascript绘制网络图

时间:2016-11-09 05:28:15

标签: javascript jquery html xml d3.js

我现在正在尝试使用XML和JavaScript来创建圆环图。

我尝试使用这个d3.v3.min.js库。我尝试使用我的js编码,它检查并循环XML中的数据并存储在数组中。然后我尝试运行编码来绘制网络图。

$(document).ready(function(){   

        $.ajax({
        type: "GET",
        url: "data.xml",
        dataType: "xml",
        success: function(xml){

            var links = [];
            $(xml).find('name').each(function(){
                var name = $(this);
                var source = $name.attr("source");
                var target = $name.attr("target");

                links.push([source, target]);

            });


               var nodes ={};
            // Compute the distinct nodes from the links.
            links.forEach(function(link) {
            link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
            link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
                    });

            visualizeIt();

        },

        error: function() {
            alert("An error occurred while processing XML file.");      
        }

    });        

});

对于绘制网络图的部分,

function visualizeIt() {



var width = 960,
    height = 500;

var force = d3.layout.force()
    .nodes(d3.values(nodes))
    .links(links)
    .size([width, height])
    .linkDistance(60)
    .charge(-300)
    .on("tick", tick)
    .start();

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

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

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

node.append("circle")
    .attr("r", 8);

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

function tick() {
  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 + ")"; });
}
function mouseover() {
  d3.select(this).select("circle").transition()
      .duration(750)
      .attr("r", 16);
}
function mouseout() {
  d3.select(this).select("circle").transition()
      .duration(750)
      .attr("r", 8);}

}

这是我的data.xml

<?xml version="1.0" encoding="utf-8" ?>
<note>
<name source = "Math" target = "Science" />
<name source = "Bio" target = "Geo" />
<name source = "Science" target = "Astro" />
<name source = "Science" target = "Histo" />
</note>

我认为在编码的第一部分中缺少部分或对概念的误解。任何人请指正。

1 个答案:

答案 0 :(得分:0)

XML

<?xml version="1.0" encoding="utf-8" ?>
<course>
<source>Math</source>
</course>
<subcourse>
<target>Science</target>
</subcourse>
</course>

<course>
<source>Bio</source>
</course>
<subcourse>
<target>Geo</target>
</subcourse>
</course>

javascript编码

$(document).ready(function(){ 
$.ajax({
        type: "GET",
        url: "data.xml",
        dataType: "xml",
        success: function(xml){
        var links = [];
        var nodes = {};
        $(xml).find('course').each(function(){
                var getSource = $(this).find('source').text();

                $(this).find('target').each(function(){
                    var getTarget = $(this).text();

                    links.push({source:getSource, target:getTarget});                   
                });
            }); 

// Compute the distinct nodes from the links.
links.forEach(function(link) {
  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});

var width = 960,
    height = 500;

var force = d3.layout.force()
    .nodes(d3.values(nodes))
    .links(links)
    .size([width, height])
    .linkDistance(60)
    .charge(-300)
    .on("tick", tick)
    .start();

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

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

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

node.append("circle")
    .attr("r", 8);

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

function tick() {
  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 + ")"; });
}

function mouseover() {
  d3.select(this).select("circle").transition()
      .duration(750)
      .attr("r", 16);
}

function mouseout() {
  d3.select(this).select("circle").transition()
      .duration(750)
      .attr("r", 8);
};


},
});
});

这让我陷入困境几天。最后可以在这里免费发布我的答案。