Как обозначить граф направленной силы на d3?

Я создаю направленный граф с помощью d3.js и не могу обойти момент маркировки узлов текстом. Я пробовал бесчисленное количество ответов здесь, в StackOverflow, и в онлайн-учебниках, но я считаю, что проблемы заключаются в моем фундаментальном понимании Javascript. введите здесь описание изображения

Я пробовал разные комбинации .attr/.append/.text, чтобы получить текст из источника и цели, но ничего не происходит.

Это рассматриваемая область:

node.append("title")
    .text(function (d) {return d.target});

node.append("text")
    .attr("dy", -3)
    .text(function (d) {return d.source})
    .attr("class", "font");

Это упрощенная выдержка из стиля:

<style>

.node {
    fill: #ccc; /* Fill of the circles*/
    stroke: #ffffff;
    stroke-width: 2px;
}

.font {
    font: 10px;
    font-family: sans-serif;

}

.link {
    stroke: #777; /* Colour of the lines*/
    stroke-width: 2px;
}
</style>

Это упрощенная выдержка из сценария:

var width = 640,
    height = 480;

    var links = [
    //this is an array
    {source: "Germany", target: "name1"},
    {source: "Germany", target: "name2"},
    {source: "Nigeria", target: "name3"},
    {source: "Environment", target: "name4"},

    ]; 

    //setting up the nodes:

    var nodes = {};


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


//add svg to the body, this is where the actual d3 starts

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

var force = d3.layout.force() //Here we specify the paramaters
    .size([width,height])
    .nodes(d3.values(nodes)) //this is where we pass the nodes of our dataset
    .links(links) // source of links
    .on("tick", tick) //on click of the nodes
    .linkDistance(300) //How far apart the nodes are
    .start(); //Start to render

//add link and nodes
var link = svg.selectAll(".link")
    .data(links) //get the data
    .enter().append('line') //binds the data in the links array to the svg 
    .attr("class", "link") //css styling

var node = svg.selectAll(".node")
    .data(force.nodes()) //way to reference the nodes in the force layout
    .enter().append("circle") 
    .attr("class", "node") //attribute CSS styling
    .attr("r", width * 0.03); //radius of the circle

//text element
node.append("title")
    .text(function (d) {return d.target});

node.append("text")
    .attr("dy", -3)
    .text(function (d) {return d.source})
    .attr("class", "font");

//creating the tick function from the force variable
//the "e" paramater can be used for positioning

function tick(e) {
    node.attr("cx", function(d) {return d.x;}) 
        .attr("cy", function(d) {return d.y;})
        .call(force.drag); //the relative location will activate a drag once the node is clicked

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

    }
</script>

В настоящее время я не получаю никаких сообщений об ошибках, что затрудняет отладку файла. Любая помощь приветствуется. Благодарю вас!


person cakebot    schedule 18.08.2019    source источник


Ответы (1)


В вашем коде две проблемы.

Первая проблема — ваш выбор node:

var node = svg.selectAll(".node")
    .data(force.nodes())
    .enter().append("circle") 
    //etc...

Как видите, это набор кругов. Позже, когда вы попытаетесь...

node.append("text")

... это не сработает, потому что вы не можете добавить элемент <text> к элементу <circle>.

Наиболее распространенным решением является создание node группового (<g>) выделения, к которому добавляются как кружки, так и тексты.

Вторая проблема — это данные для узлов. У вас есть это в ваших текстах:

node.append("text")
    .text(function (d) {return d.source})

Однако в данных нет свойства с именем source. Единственное свойство, которое у вас есть, это name.

Вот ваш код с этими изменениями:

var width = 640,
  height = 480;

var links = [
  //this is an array
  {
    source: "Germany",
    target: "name1"
  },
  {
    source: "Germany",
    target: "name2"
  },
  {
    source: "Nigeria",
    target: "name3"
  },
  {
    source: "Environment",
    target: "name4"
  },

];

//setting up the nodes:

var nodes = {};


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


//add svg to the body, this is where the actual d3 starts

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

var force = d3.layout.force() //Here we specify the paramaters
  .size([width, height])
  .nodes(d3.values(nodes)) //this is where we pass the nodes of our dataset
  .links(links) // source of links
  .on("tick", tick) //on click of the nodes
  .linkDistance(300) //How far apart the nodes are
  .start(); //Start to render

//add link and nodes
var link = svg.selectAll(".link")
  .data(links) //get the data
  .enter().append('line') //binds the data in the links array to the svg 
  .attr("class", "link") //css styling

var node = svg.selectAll(".node")
  .data(force.nodes()) //way to reference the nodes in the force layout
  .enter().append("g");

node.append("circle")
  .attr("class", "node")
  .attr("r", width * 0.03); //radius of the circle

node.append("text")
  .attr("dy", -3)
  .text(function(d) {
    return d.name
  })
  .attr("class", "font");

//creating the tick function from the force variable
//the "e" paramater can be used for positioning

function tick(e) {
  node.attr("transform", function(d) {
      return "translate(" + [d.x, d.y] + ")"
    })
    .call(force.drag); //the relative location will activate a drag once the node is clicked

  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 {
  fill: #ccc;
  /* Fill of the circles*/
  stroke: #ffffff;
  stroke-width: 2px;
}

.font {
  font: 10px;
  font-family: sans-serif;

}

.link {
  stroke: #777;
  /* Colour of the lines*/
  stroke-width: 2px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js"></script>

person Gerardo Furtado    schedule 18.08.2019