我使用 d3.js 使用以下代码创建了一个圆环图,要求是使用来自后端的颜色来显示圆环图,而不是使用内置颜色d3.scale.category**()'s
<script>
var width = 260,
height = 150,
radius = Math.min(width, height) / 2;
var color = d3.scale.category20();
var domain = ["bbb", "ddd", "ccc", "23", "hello"];
var pie = d3.layout.pie()
.value(function(d) { return d.apples; })
.sort(null);
var arc = d3.svg.arc()
.innerRadius(radius - 10)
.outerRadius(radius - 2);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.attr("style","left: 480px;top: 120px;position: absolute;")
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
d3.tsv("data.tsv", function(error, data) {
var path = svg.datum(data).selectAll("path")
.data(pie)
.enter().append("path")
.attr("fill", function(d, i) { console.log(color(i)); return color(i); })
.attr("d", arc);
});
</script>
使用后端或数组中自己的调色板组合或类似以下内容的任何方法来覆盖颜色:var colors = ["#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", " #8c564b”、“#e377c2”、“#7f7f7f”、“#bcbd22”、“#17becf”];
提前致谢!