kenreal kenreal - 13 days ago 4
HTML Question

XML javascript draw network graph

I am now trying use XML and JavaScript to create a donut chart.

I tried with this d3.v3.min.js library. I tried with my js coding which check and loop the data in XML and store in array. Then I tried run the coding to draw the network graph.

$(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.");
}

});

});


For the part of drawing network graph,

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

}


This is my 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>


I believed that there is missing part or misunderstanding of concept in first part of coding. Anyone please correct me.

Answer

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 coding

$(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);
};


},
});
});

This make me stuck few days. And finally free to post my answer here.

Comments