目前我正在尝试设计动态饼图,其中饼图的切片将根据随机生成的数据而改变。以下是代码。
var dataset = [
{ name: 'Smooth', percent: 40.00, class: 'custom-normal' },
{ name: 'Moderate', percent: 10.00, class: 'custom-warning' },
{ name: 'Heavy', percent: 50.00, class: 'custom-danger' }
];
var width = 960,
height = 500,
radius = Math.min(width, height) / 2; //Math.min return the smallest value between width and height (for optimization purposes)
var colorValues = d3.scaleOrdinal().domain(["Smooth", "Moderate", "Heavy"]).range(["#605A4C", "#ff9900", "#ff1a1a"]);
var percent = "percent"; //predefine the legend of dataset (the string index)
var category = "class";
var name = "name";
var pie = d3.pie()
.value(function(d) { return d[percent]; })
.sort(null)
.padAngle(.02); //the gap
var arc = d3.arc()
.innerRadius(radius - 100) //optimization
.outerRadius(radius - 20); //optimization
var svg = d3.select("#chart")
.append("svg")
.attrs({
width: width,
height: height,
class: "shadow"
}).append("g")
.attrs({
transform: 'translate(' + width / 2 + ',' + height / 2 + ')'
});
svg.append('g')
.attrs({
class: 'slices'
});
var path = svg.select('.slices')
.selectAll('path')
.data(pie(dataset))
.enter().append('path')
.attrs({
d: arc
}).each(function(d, i) {
this._current = d;
console.log(this._current);
console.log('okay!');
}).attrs({
class: function(d, i){
return d[category];
},
fill: function(d, i) {
console.log("this is color value" + colorValues());
return colorValues(d[i]);
}
}); //initial details (this._current)
var randomGenerator = setInterval(function() {
var data = dataset.map(function(d, i) {
for (var key in d) {
if (d[key] === "Smooth") {
//console.log("smooth");
dataset[0].percent = Math.floor(Math.random() * 100);
//console.log(dataset[0].percent);
} else if (d[key] === "Moderate") {
dataset[1].percent = Math.floor(Math.random() * 100);
//console.log(dataset[1].percent);
//console.log("moderate");
} else if (d[key] === "Heavy") {
dataset[2].percent = Math.floor(Math.random() * 100);
//console.log(dataset[2].percent);
//console.log("heavy");
}
}
});
}, 3000);
var timer = setInterval(function() {
pie.value(function(d) {
return d[percent];
}); // change the value function
path = path.data(pie(dataset)); // compute the new angles
path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
}, 3000);
// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
var i = d3.interpolate(this._current, a);
console.log(this._current);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}

body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 960px;
}

<meta charset="utf-8">
<div id="mydiv" class="widget">
<div id="chart" class="Chart chart-container"></div>
</div>
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>
&#13;
请注意,每个切片的颜色不会根据我之前定义的内容进行缩放。即:var colorValues = d3.scaleOrdinal().domain(["Smooth", "Moderate", "Heavy"]).range(["#605A4C", "#ff9900", "#ff1a1a"]);
。我认为这应该是选择问题,但我没有发现任何线索。然而,同样,它只获得var dataset
中的第一行数据并适用于所有数据。请帮助我,先谢谢你。
答案 0 :(得分:3)
如果我理解正确,
通过饼图功能推送数据,您正在改变数据。虽然您的初始结构是:
{ name: 'Smooth', percent: 40.00, class: 'custom-normal' }
通过饼图函数运行此数据后,绑定到每个弧的基准具有以下结构:
{
"data": {
"name": "Heavy",
"percent": 48,
"class": "custom-danger"
},
"index": 2,
"value": 50,
"startAngle": 3.1515926535897933,
"endAngle": 6.283185307179586,
"padAngle": 0.02
}
在你的代码中,你根据这个来着色楔子:
fill: function(d, i) {
console.log("this is color value" + colorValues());
return colorValues(d[i]);
}
d
中的function(d) {}
指的是数据,因此它只是输入数据中的单个项目(绑定到特定弧的基准),这是一个对象 - 使用d [编号]建议您预期阵列。无论如何,每次运行此功能都会得到undefined
。
而是访问所需数据的属性:name
(我假设)并使用:
fill: function(d, i) {
console.log("this is color value" + colorValues());
return colorValues(d.data.name);
}
var dataset = [
{ name: 'Smooth', percent: 40.00, class: 'custom-normal' },
{ name: 'Moderate', percent: 10.00, class: 'custom-warning' },
{ name: 'Heavy', percent: 50.00, class: 'custom-danger' }
];
var width = 960,
height = 500,
radius = Math.min(width, height) / 2; //Math.min return the smallest value between width and height (for optimization purposes)
var colorValues = d3.scaleOrdinal().domain(["Smooth", "Moderate", "Heavy"]).range(["#605A4C", "#ff9900", "#ff1a1a"]);
var percent = "percent"; //predefine the legend of dataset (the string index)
var category = "class";
var name = "name";
var pie = d3.pie()
.value(function(d) { return d[percent]; })
.sort(null)
.padAngle(.02); //the gap
var arc = d3.arc()
.innerRadius(radius - 100) //optimization
.outerRadius(radius - 20); //optimization
var svg = d3.select("#chart")
.append("svg")
.attrs({
width: width,
height: height,
class: "shadow"
}).append("g")
.attrs({
transform: 'translate(' + width / 2 + ',' + height / 2 + ')'
});
svg.append('g')
.attrs({
class: 'slices'
});
var path = svg.select('.slices')
.selectAll('path')
.data(pie(dataset))
.enter().append('path')
.attrs({
d: arc
}).each(function(d, i) {
this._current = d;
console.log(this._current);
console.log('okay!');
}).attrs({
class: function(d, i){
return d.data.class;
},
fill: function(d, i) {
console.log("this is color value" + colorValues());
return colorValues(d.data.name);
}
}); //initial details (this._current)
var randomGenerator = setInterval(function() {
var data = dataset.map(function(d, i) {
for (var key in d) {
if (d[key] === "Smooth") {
//console.log("smooth");
dataset[0].percent = Math.floor(Math.random() * 100);
//console.log(dataset[0].percent);
} else if (d[key] === "Moderate") {
dataset[1].percent = Math.floor(Math.random() * 100);
//console.log(dataset[1].percent);
//console.log("moderate");
} else if (d[key] === "Heavy") {
dataset[2].percent = Math.floor(Math.random() * 100);
//console.log(dataset[2].percent);
//console.log("heavy");
}
}
});
}, 3000);
var timer = setInterval(function() {
pie.value(function(d) {
return d[percent];
}); // change the value function
path = path.data(pie(dataset)); // compute the new angles
path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs
}, 3000);
// Store the displayed angles in _current.
// Then, interpolate from _current to the new angles.
// During the transition, _current is updated in-place by d3.interpolate.
function arcTween(a) {
var i = d3.interpolate(this._current, a);
console.log(this._current);
this._current = i(0);
return function(t) {
return arc(i(t));
};
}
&#13;
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 960px;
}
&#13;
<meta charset="utf-8">
<div id="mydiv" class="widget">
<div id="chart" class="Chart chart-container"></div>
</div>
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>
&#13;