gpt4 book ai didi

javascript - D3饼图中的每个选择

转载 作者:行者123 更新时间:2023-11-29 18:59:10 25 4
gpt4 key购买 nike

目前我正在尝试设计动态饼图,饼图的切片会根据随机生成的数据而变化。下面是代码。

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>

请注意,每个切片的颜色并未根据我之前定义的进行缩放。即:var colorValues = d3.scaleOrdinal().domain(["Smooth", "Moderate", "Heavy"]).range(["#605A4C", "#ff9900", "#ff1a1a"]) ;。我认为这应该是选择问题,但是我对此一无所知。然而,类也是如此,它只获取 var dataset 中的第一行数据并应用于所有数据。

最佳答案

如果我没理解错的话,

通过 pie 函数推送您的数据,您正在更改您的数据。虽然您的初始结构是:

{ name: 'Smooth', percent: 40.00, class: 'custom-normal' }

通过 pie 函数运行此数据后,绑定(bind)到每个圆弧的数据具有以下结构:

{
"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]);
}

function(d){} 中的d 指的是数据,因此它只是输入数据中的单个项目(绑定(bind)到特定弧的数据),这是一个对象 - d[number] 的使用表明您预期一个数组。无论如何,每次运行此函数时,您都会得到 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));
};
}
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>

关于javascript - D3饼图中的每个选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47824901/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com