【发布时间】:2020-04-08 21:14:52
【问题描述】:
我到处寻找解决方案,我已经尝试了所有方法,但没有任何效果!
当我点击更新数据和饼图时,转换无法正常工作并打印错误(错误:属性d:预期的弧标志('0'或'1'))超过100次。 有人可以帮我吗?
这是代码的开头:
// set the dimensions and margins of the graph
var width = 320
height = 450
margin = 40
// The radius of the pieplot is half the width or half the height (smallest one). I subtract a bit of margin.
var radius = Math.min(width, height) / 2 - margin
// append the svg object to the div called 'my_dataviz'
var svg = d3.select("#recipe-graph")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")");
// create 2 data_set
//Aromatic Profile
//a: woody, b: floral, c: resinous, d: herbaceus, e: citrus, f: minty,
var data1 = {a:56.2, b: 25, c:18.8} //woody, floral, resinous
var data2 = {b: 31.2, d: 50, e:18.8} //floral, herbaceus, citus
var data3 = {d: 33.3, e:11.1, f: 55.6} //herbaceous, citrus, minty
var data4 = {b: 56.2, c: 43.8} //floral, resinous
var data5 = {b: 82.6, e: 17.4} //floral, citrus
// set the color scale
var color = d3.scaleOrdinal()
.domain(["a", "b", "c", "d", "e", "f"])
.range(d3.schemeDark2);
我相信问题出在下面。 (我使用的是最新的 d3.js 库)
// A function that create / update the plot for a given variable:
function update(data) {
// Compute the position of each group on the pie:
var pie = d3.pie()
.value(function(d) {return d.value; })
.sort(function(a, b) { console.log(a) ; return d3.ascending(a.key, b.key);} ) // This make sure that group order remains the same in the pie chart
var data_ready = pie(d3.entries(data))
// map to data
var u = svg.selectAll("path")
.data(data_ready)
// Build the pie chart: Basically, each part of the pie is a path that we build using the arc function.
u
.enter()
.append('path')
.merge(u)
.transition()
.duration(1000)
.attr('d', d3.arc()
.innerRadius(0)
.outerRadius(radius)
)
.attr('fill', function(d){ return(color(d.data.key)) })
.attr("stroke", "white")
.style("stroke-width", "2px")
.style("opacity", 1)
// remove the group that is not present anymore
u
.exit()
.remove()
}
// Initialize the plot with the first dataset
update(data1)
//Change Recipe according to button selected
function selectCategory(category, results) {
var postResults = document.getElementById(results);
postResults.innerHTML = category;
}
【问题讨论】:
标签: javascript d3.js svg transition