2
我退出了一個新手到D3.js和一個項目,我需要做一個數據每2秒更改一次的數據結果。但每次我試圖改變數據的時間我得到一個錯誤:D3未捕獲TypeError:path.data不是一個函數
Uncaught TypeError: path.data is not a function
我用this code作爲範例來改變功能
代碼:
<!DOCTYPE html>
<html lang="en">
<head>
\t <meta charset="UTF-8">
\t <title>D3 donuttest</title>
\t <script src="//d3js.org/d3.v3.min.jss"></script> \t
\t <style>
\t \t body {
\t \t font: 10px sans-serif;
\t \t }
\t </style>
</head>
<body>
</body>
\t <div id="chartwrapper">
\t \t
\t </div>
</body>
<script>
\t var wrapper="#chartwrapper";
\t var dummydata=70;
\t var data=[dummydata,100-dummydata];
\t var width = 460,
\t height = 300,
\t radius = Math.min(width, height)/2;
\t var color = d3.scale.category20();
\t var pie = d3.layout.pie()
\t .sort(null);
\t var arc = d3.svg.arc()
\t .innerRadius(radius - 100)
\t .outerRadius(radius - 50);
\t var svg = d3.select(wrapper).append("svg")
\t .attr("width", width)
\t .attr("height", height)
\t .append("g")
\t .attr("transform", "translate(" + width/2 + "," + height/2 + ")");
\t var path = svg.selectAll("path")
\t .data(pie(data))
\t \t .enter().append("path")
\t .attr("fill", function(d, i) { return color(i); })
\t .attr("d", arc)
\t .transition()
\t .ease("bounce")
\t .duration(1000)
\t .attrTween("d", initTween);
var label=svg.append("g")
.attr("class", "display")
.attr("transform", "translate(-30,10)");
var labeltext=label.append("text")
.attr("font-size","2rem")
.text(dummydata + "%");
\t function change(data){
\t \t labeltext.text(data[0] + "%");
\t \t path.data(pie(data));
\t \t path.transition().duration(750).attrTween("d",arcTween);
\t };
//animation tween for when graph is drawn
\t function initTween(b) {
\t \t b.innerRadius = 0;
\t \t var i = d3.interpolate({startAngle: 0, endAngle: 0}, b);
\t \t return function(t) { return arc(i(t)); };
\t }
\t //animation for when data is changed
\t function arcTween(a) {
\t \t var i = d3.interpolate(this._current, a);
\t \t this._current = i(0);
\t \t return function(t) {
\t \t \t return arc(i(t));
\t \t };
\t };
\t //simulating data changes
\t setInterval(function() {
\t dummydata = parseInt(Math.random() * 100);
\t data=[dummydata,100-dummydata];
\t console.log(data)
\t \t change(data);
\t },2000);
</script>