我一直在这个问题上停留了好几天。用d3追加多个svg文字
所以,我有以下形式的对象的数据集:
dataset = [{metric:"revenue",value:0.03},{metric:"sales", value:0.15},{metric:"churn", value: 0.06},{metric:"logins", value: 0.45}]
下面的代码将显示在网格图案中的4个指标名称(网状,meshx是栅格和meshsize的坐标点是网格的大小,所以这只是把文字栅格广场中间):
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text(function(d){
return d.metric;
})
.attr("y",function(d,i){
return meshy[i] + meshsize/2;
})
.attr("x", function(d,i){
return meshx[i] + meshsize/2;
})
.attr("font-size",25)
.attr("font-family","serif")
.attr("text-anchor","middle")
.attr("font-weight","bold");
现在我想提出的指标权价值的度量标准名称下,像这样:
svg.append("text")
.data(dataset)
.text(function(d){
return (d.value);
})
.attr("y",function(d,i){
return meshy[i] + meshsize/2 + 20;
})
.attr("x", function(d,i){
return meshx[i] + meshsize/2 ;
})
.attr("font-size",25)
.attr("font-family","serif")
.attr("text-anchor","middle")
.attr("font-weight","bold");
但是,这只会返回FIRST度量标准名称下的值,其他3个值文本甚至不在DOM中。我尝试了多种方法,包括用.html替换.text,如下所述:https://github.com/mbostock/d3/wiki/Selections#wiki-html没有成功。我也尝试添加段落元素 - 这可行,但p元素位于svg主体下面的列表中,没有明显的方式将它们移动到正确的位置。上面的代码是最接近我得到我需要的,但由于某种原因只有第一个值文本显示。然而,我愿意接受d3中任何可以完成工作的方法:4个公制名称,其值正好在它们下面