使用基于 D3 的平行坐标代码,我正在尝试添加一些文本:
g_data.select(".label")
.text(dimensionLabels) //visible
g_data.select(".sublabel1")
.text(dimensionSublabels1) //not visible
g_data.select(".sublabel2")
.text(dimensionSublabels2) //not visible
到之前创建的 svg:text 和 svg:tspan 元素:
var g_data = pc.svg.selectAll(".dimension").data(pc.getOrderedDimensionKeys());
// Enter
g_data.enter()
.append("svg:g")
.attr("class", "dimension")
.attr("transform", function(p) { return "translate(" + position(p) + ")"; })
.style("opacity", 0)
.append("svg:g")
.attr("class", "axis")
.attr("transform", "translate(0,0)")
.each(function(d) {
var axisElement = d3.select(this).call( pc.applyAxisConfig(axis, __.dimensions[d]) );
axisElement.selectAll("path")
.style("fill", "none")
.style("stroke", "#222")
.style("shape-rendering", "crispEdges");
axisElement.selectAll("line")
.style("fill", "none")
.style("stroke", "#222")
.style("shape-rendering", "crispEdges");
})
.append('svg:text') //1st part of label
.attr({
"text-anchor": "middle",
"y": -40,
"x": 0,
"dy": 0,
"class": "label"
})
.append('svg:tspan') //2nd part of label
.attr({
"x": 0,
"dy": 17,
"class": "sublabel1"
})
.append('svg:tspan') //3rd part of label
.attr({
"x": 0,
"dy": 14,
"class": "sublabel2"
})
问题在于,这仅部分起作用,原因我无法解释:一次只能显示一个文本标签。
更具体地说,对于上述情况,文本只会添加到“.label”类 svg 中,其他两个是不可见的。但是如果我注释掉前两行(如下所示),那么 sublabel1 就会变得可见,依此类推。
//g_data.select(".label")
// .text(dimensionLabels) //with this part commented, the next label becomes visible
g_data.select(".sublabel1")
.text(dimensionSublabels1) //visible
g_data.select(".sublabel2")
.text(dimensionSublabels2) //not visible
谁能帮助找出可能导致这种情况的原因,以及我可以做些什么来同时显示所有标签?非常感谢!