只需更新杰夫的答案:
颜色:
将颜色参考传递给函数:r.customAttributes.segment = function (x, y, r, a1, a2, color) {...
在数据后添加颜色数组 var:var data = [90, 90, 90, 90], color = ['#ff0000', '#00ff00', '#0000ff', '#ff0000'], ...
在最后一个循环中:
start = 0;
for (i = 0; i < ii; i++) {
var val = 360 / total * data[i];
var col = color[i]; //Add color from array
(function (i, val) {
//Here we pass the color (col)
paths.push(r.path().attr({segment: [200, 200, 1, start, start + val, col], stroke: "#fff"}).click(function () {
...
编辑:
然后(感谢Matt Ball的var props {
部分)
更改 r.customAttributes.segment = function (x, y, r, a1, a2, color) {... 用这个:
var flag = (a2 - a1) > 180;
a1 = (a1 % 360) * Math.PI / 180;
a2 = (a2 % 360) * Math.PI / 180;
var props = {
path: [["M", x, y], ["l", r * Math.cos(a1), r * Math.sin(a1)], ["A", r, r, 0, +flag, 1, x + r * Math.cos(a2), y + r * Math.sin(a2)], ["z"]]
};
if (color) {
props.fill = color;
}
return props;