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
});
});
价值永远是一个数字? – echonax
是的,但在初始渲染时,计算该数字所需的输入尚不存在。 – Bren