erp*_*erp 5 javascript d3.js nvd3.js
我正在与NVD3合作制作一张旭日形图.我是D3的新手,因此我使用NVD3来抽象一些更复杂的东西.现在我正在使用我从网上获得的一个简单示例,但我正在尝试根据size
JSON中的属性为节点(弧)着色.我知道NVD3可以选择color
在图表选项中使用一个函数,所以这就是我尝试使用的方式:
chart: {
type: 'sunburstChart',
height: 450,
color: function(d) {
if (d.size > 3000) {
return "red";
} else if (d.size <= 3000) {
return "green";
} else {
return "gray";
}
},
duration: 250
}
Run Code Online (Sandbox Code Playgroud)
但正如你所看到的那样,我正在研究的那个结果只是灰色,因为它实际上并没有得到一个值d.size
(它只是未定义,我不知道为什么).
使用我试图避免的常规D3,我可以得到这样的愿望结果:
var path = g.append("path")
.attr("class","myArc")
.attr("d", arc)
.attr("name",function(d){return "path Arc name " + d.name;})
.style("fill", function(d) {
if(d.size > 3000) {
return "green";
} else if( d.size < 3000){
return "red";
} else {
return "gray";
}
})
.on("click", click)
... //etc
Run Code Online (Sandbox Code Playgroud)
我修改了一个常规的D3 sunburst示例,以获得所需的结果:
我知道标签是顶起来的,但这并不重要.我想获得与常规D3相同的结果,但是需要抽象NVD3.我还没有找到任何提及使用它的好例子color: function()
.提前致谢.
首先使用 github 发行版中的这些 javascript 库:
<script src="http://krispo.github.io/angular-nvd3/bower_components/nvd3/build/nv.d3.js"></script>
<script src="http://krispo.github.io/angular-nvd3/bower_components/angular-nvd3/dist/angular-nvd3.js"></script>
Run Code Online (Sandbox Code Playgroud)
图表选项应该是这样的:
$scope.options = {
"chart": {
"type": "sunburstChart",
"height": 450,
"duration": 250,
"width": 600,
"groupColorByParent": false,//you missed this
color: function(d, i) {
//search on all data if the name is present done to get the size from data
var d2 = d3.selectAll("path").data().filter(function(d1) {
return (d1.name == d)
})[0];
//now check the size
if (d2.size > 3000) {
return "red";
} else if (d2.size <= 3000) {
return "green";
} else {
return "gray";
}
},
duration: 250
}
}
Run Code Online (Sandbox Code Playgroud)
工作代码在这里