我有一段代码试图根据传入的参数(0或1)确定要动态使用的符号和颜色。但是,当我尝试设置逻辑时,出现了属性d的路径错误。代码在下面
svg.selectAll(".point")
.data(data)
.enter()
.append("path")
.attr("class", "point")
.attr("d", function(d){
if(d.zeroOrOne == 1){
return d3.svg.symbol().type("cross");
}else if (d.zeroOrOne == 0){
return d3.svg.symbol().type("circle");
}
})<
.attr('fill', 'none')
.attr('stroke', function(d){
if(d.zeroOrOne == 1){
return 'blue';
}else if (d.zeroOrOne == 0){
return 'red';
}
})
.attr("transform", function(d) { return "translate(" + ratingScale(d.xParam) + "," + winsNomsScale(d.yParam) + ")"; });
如果我删除条件sybmol逻辑,并且仅将d严格地设置到一个符号或其他符号上,则一切正常,但是我无法弄清楚为什么添加条件逻辑会引起问题。这是错误:
错误:属性d:预期的moveto路径命令(“ M”或“ m”),“函数n(n,r){…
答案 0 :(得分:1)
使用单独的函数来创建符号并对其进行调用
var data = [{"zeroOrOne" : 0 }, {"zeroOrOne" : 1 }, {"zeroOrOne" : 2 }];
// use this for generating symbols
var arc = d3.svg.symbol().type(function(d) {
if(d.zeroOrOne == 0) {
return "circle";
}
else if(d.zeroOrOne == 1) {
return "cross";
}
else return "triangle-up";
});
d3.select("#graph").selectAll(".point")
.data(data)
.enter()
.append("path")
.attr("class", "point")
.attr("d", arc)
.attr('fill', 'none')
.attr('stroke', function(d){
if(d.zeroOrOne == 1){
return 'blue';
}else if (d.zeroOrOne == 0){
return 'red';
}
else return "green";
})
.attr("transform", function(d, i) { return "translate(" + (i + 1) * 20 + " ,30 )"; });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<svg id="graph" width="200", height="200" >
</svg>