2016-05-13 99 views
1

仅当变量具有值时,才可以将D3形状的文本元素设置为仅显示?在下面的代码中,矩形呈现,但宽度将根据selections(文档ID数组)的值而变化。如果selections为空,我仍然希望形状呈现,但不想要任何文本标签。现在,我看到$ NaN。不确定在这里包括if语句。仅当变量具有值时才显示D3文本标签

Template.Box.onRendered (function() { 
    const self = this; 

    //List of variables to calculate dimensions 
    var value = //Result of calculations 
    var selections = Template.parentData(0).selections; 
    var boxContainer = d3.select("#box" + boxId) 
     .append("svg") 
     .attr("id", "svg-box"); 

    var box = boxContainer.append("rect") 
     .attr("x", start + "%") 
     .attr("y", 0) 
     .attr("height", 50) 
     .attr("width", range + "%") 
     .attr("id", "box" + boxId); 

    var boxValue = boxContainer.append("text") 
     .attr("x", boxSpace + "%") 
     .attr("y", "20px") 
     .text(value) 
     .attr("id", "low" + boxId); 

    self.autorun(function() { 
     //repeat code from above 
    }); 
}); 
+0

价值永远是一个数字? – echonax

+0

是的,但在初始渲染时,计算该数字所需的输入尚不存在。 – Bren

回答

0

您可以将其添加到您的文本属性中。

node.append("text") 
    .style("display", function (d) { return (d.data.i) ? null : "none" }) 

如果没有d.data.i,那么它将显示设置为无。空表示它将被显示。

相关问题