简体   繁体   English

D3更新DOM,但未显示任何内容

[英]D3 updating DOM, but nothing is showing

I am building a force-directed graph, and as far as I can tell, I have written the code correctly. 我正在构建一个力导向图,据我所知,我已经正确编写了代码。 The DOM looks exactly right after this code runs. 这段代码运行后,DOM看起来恰好。 And yet, nothing is displayed. 但是,什么也没有显示。

var type_node_list = typeNodes(data);
shuffle(type_node_list);
initializePosition(type_node_list);

var links = typeLinks(type_node_list);

var svg = d3.select('#root');

var link = svg
    .append('g')
    .attr('id', 'links')
    .selectAll('line')
    .data(links)
    .enter()
    .append('line')
    .attr('class', 'link');

var type_nodes = svg
    .append('g')
    .attr('id', 'nodes')
    .selectAll('.node')
    .data(type_node_list)
    .enter()
    .append(createTypeNode);

function updateTypeNodeLocations() {
    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;});

    type_nodes
        .attr('x', nodeX)
        .attr('y', nodeY);
}
updateTypeNodeLocations();
/*
var position_the_types = d3.forceSimulation()
  .nodes( type_node_list )
  .force("charge",d3.forceManyBody().strength(-10))
  ;
position_the_types.on('tick',updateTypeNodeLocations());
*/

The simulation portion is commented out because I'm trying to get the first part working. 模拟部分已被注释掉,因为我正在尝试使第一部分工作。 When I uncomment it, it only calls the 'tick' event once, even though the processing is clearly not complete. 当我取消注释时,即使处理显然没有完成,它也只会调用一次“ tick”事件。 And there is nothing in the JavaScript console to explain it. JavaScript控制台中没有任何内容可以解释它。

See http://jsfiddle.net/jarrowwx/gof5knaj/36/ for the full code. 有关完整代码,请参见http://jsfiddle.net/jarrowwx/gof5knaj/36/

I had things working this morning, and something changed and now nothing I do seems to work. 今天早上我的工作正常,某些事情发生了变化,现在我似乎什么也没做。 I checked the D3 github, and the last commit appears to have been 11 days ago, so it's not likely caused by a change to the library. 我检查了D3 github,最后一次提交似乎是在11天前,所以这不太可能是由于库的更改引起的。

Has anybody experienced something like this before? 以前有人经历过这样的事情吗? Any pointers on what I'm doing wrong? 关于我在做什么的任何指示? Have I uncovered a D3 bug? 我是否发现了D3错误?

The problem lie in my function createTypeNode . 问题出在我的函数createTypeNode

I created the image element via: document.createElement('image') . 我通过以下方式创建了图像元素: document.createElement('image') That doesn't work. 那不行 To create an image via JavaScript, one must use Namespaces. 要通过JavaScript创建图片,必须使用命名空间。

See: Programmatically creating an SVG image element with javascript 请参阅: 使用javascript以编程方式创建SVG图像元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM