如何在D3.js V4中制作静态力图?

时间:2017-02-08 23:08:56

标签: javascript d3.js queue

请注意,此问题与D3的V4版本有关。这是一个很新的问题,所以没有多少问题可以解决这个版本的问题。

我试图将D3力图静态化。我当然在" https://bl.ocks.org/mbostock/1667139"上找到了这个例子。我添加了数据加载队列,它在动态力量示例中有效。

当我在下面运行脚本时,middel中会显示一个黑点。我很难搞清楚自己做错了什么。

<!DOCTYPE html>
<meta charset="utf-8">


<style>

.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
}


</style>

<head>
</head>

<body>

<script src="/sources/d3/d3.min.js"></script>
<script src="/sources/jquery-3.1.1.min.js"></script>

<svg width="960" height="600"></svg>

<script>

var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height"),
    g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

//var n = 100,
    //nodes = d3.range(n).map(function(i) { return {index: i}; }),
    //links = d3.range(n).map(function(i) { return {source: i, target: (i + 3) % n}; });

var simulation = d3.forceSimulation(attributes)
    .force("charge", d3.forceManyBody().strength(-80))
    .force("link", d3.forceLink(edges).distance(20).strength(1).iterations(10))
    .force("x", d3.forceX())
    .force("y", d3.forceY())
    .stop();

var loading = svg.append("text")
    .attr("dy", "0.35em")
    .attr("text-anchor", "middle")
    .attr("font-family", "sans-serif")
    .attr("font-size", 10)
    .text("Simulating. One moment please…");

// LOAD DATA
d3.queue()
    .defer(d3.csv, "/data/attributes")
    .defer(d3.csv, "/data/edges")
    .await(analyze);

function analyze(error, attributes, edges) {
        if(error) { console.log(error); }   
// Use a timeout to allow the rest of the page to load first.
d3.timeout(function() {
  loading.remove();

  // See https://github.com/d3/d3-force/blob/master/README.md#simulation_tick
  for (var i = 0, n = Math.ceil(Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay())); i < n; ++i) {
    simulation.tick();
  }

  //console.log(nodes);
  //console.log(links);
  console.log(attributes);
  console.log(edges);

  g.append("g")
      .attr("stroke", "#000")
      .attr("stroke-width", 1.5)
    .selectAll("line")
    .data(edges)
    .enter().append("line")
      .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; });

  g.append("g")
      .attr("stroke", "#fff")
      .attr("stroke-width", 1.5)
    .selectAll("circle")
    .data(attributes)
    .enter().append("circle")
      .attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; })
      .attr("r", 4.5);
});
};

</script> 

数据:属性

id,name,age,gender
id001,Mark,25,male
id002,Lene,30,female
id003,Simon,22,male
id004,Sussie,45,female
id005,Kim,23,male

数据:边缘

source,target
id001,id002
id001,id003
id002,id004
id003,id004

1 个答案:

答案 0 :(得分:0)

因此,解决方案是将“var simulation”部分移动到加载数据的部分。另外,必须“id(function(d){return d.id;})”链接/边缘数据。 (我真的不明白为什么,但我再也不熟悉D3编程。可以在评论部分提供澄清,我会更新答案。

<!DOCTYPE html>
<meta charset="utf-8">

<style>

.links line {
  stroke: #999;
  stroke-opacity: 0.6;
}

.nodes circle {
  stroke: #fff;
  stroke-width: 1.5px;
}


</style>

<head>
</head>

<body>

<script src="/sources/d3/d3.min.js"></script>
<script src="/sources/jquery-3.1.1.min.js"></script>

<svg width="960" height="600"></svg>

<script>

var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height"),
    g = svg.append("g").attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");

var loading = svg.append("text")
    .attr("dy", "0.35em")
    .attr("text-anchor", "middle")
    .attr("font-family", "sans-serif")
    .attr("font-size", 10)
    .text("Simulating. One moment please…");

// LOAD DATA //
d3.queue()
    .defer(d3.csv, "/data/attributes")
    .defer(d3.csv, "/data/edges")
    .await(analyze);

function analyze(error, attributes, edges) {
    if(error) { console.log(error); }
    console.log(attributes);
    console.log(edges); 

// THIS PART NEED TO GO INSIDE THE DATA LOADING FUNCTION
// ALSO NOTICE THE ".id(function(d) { return d.id; })"
var simulation = d3.forceSimulation(attributes)
    .force("charge", d3.forceManyBody().strength(-80))
    .force("link", d3.forceLink(edges).id(function(d) { return d.id; }).distance(20).strength(1).iterations(10)) // the ".id(function(d)..." binds the data to the edges
    .force("x", d3.forceX())
    .force("y", d3.forceY())
    .stop();

// Use a timeout to allow the rest of the page to load first.
d3.timeout(function() {
  loading.remove();

  // See https://github.com/d3/d3-force/blob/master/README.md#simulation_tick
  for (var i = 0, n = Math.ceil(Math.log(simulation.alphaMin()) / Math.log(1 - simulation.alphaDecay())); i < n; ++i) {
    simulation.tick();
  }

  g.append("g")
      .attr("stroke", "#000")
      .attr("stroke-width", 1.5)
    .selectAll("line")
    .data(edges)
    .enter().append("line")
      .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; });

  g.append("g")
      .attr("stroke", "#fff")
      .attr("stroke-width", 1.5)
    .selectAll("circle")
    .data(attributes)
    .enter().append("circle")
      .attr("cx", function(d) { return d.x; })
      .attr("cy", function(d) { return d.y; })
      .attr("r", 4.5);

});
};
</script>