4
我正在嘗試使用D3.js繪製動態堆積的條形圖。基本要求是我將每隔幾秒鐘獲得一個新的數據點,並且我應該能夠重新繪製(轉換)堆疊圖表。這是我寫的代碼:D3中的動態堆積條形圖
<html>
<head>
<title>Page Title</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="d3/d3.js"></script>
<script src="d3/d3.csv.js"></script>
<script src="d3/d3.layout.js"></script>
</head>
<body>
<style type="text/css">
.chart rect {
stroke: white;
}
</style>
<script>
var t = 1297110663, // start time (seconds since epoch)
v = 70, // start value (subscribers)
sentidata = d3.range(33).map(next); // starting dataset
var w = 20,
h = 80;
var x = d3.scale.linear()
.domain([0, 1])
.range([0, w]);
var y = d3.scale.linear()
.domain([0, 200])
.rangeRound([0, h]);
var z = d3.scale.ordinal()
.range(["lightpink", "darkgray"]);
//This function creates random test data of format : time, posvalue, negvalue
function next() {
return {
time: ++t,
posvalue: v = ~~Math.max(10, Math.min(90, v + 10 * (Math.random() - .5))),
negvalue: v = ~~Math.max(10, Math.min(90, v + 10 * (Math.random() - .5)))
};
}
//This function readjusts the data with one new data point and calls redraw function to replot the graph
setInterval(function() {
sentidata.shift();
sentidata.push(next());
redraw(sentidata);
}, 1500);
var svg = d3.select("body").append("svg:svg")
.attr("class", "chart")
.attr("width", w * sentidata.length - 1)
.attr("height", h);
//Transpose the data into layers by Sentiment
var sent = d3.layout.stack()(["posvalue","negvalue"].map(function(sentiment){
return sentidata.map(function(d){
return {x:d.time, y:+d[sentiment]};
});
}));
//Add a group for each Sentiment
var sentiment = svg.selectAll("g.sentiment")
.data(sent)
.enter()
.append("svg:g")
.attr("class","sentiment")
.style("fill", function(d,i){return z(i);})
.style("stroke", function(d,i){return d3.rgb(z(i)).darker();});
//Add a rectangle for each time value
var rect = sentiment.selectAll("rect")
.data(Object)
.enter()
.append("svg:rect")
.attr("x",function(d, i) { return x(i) - .5; })
.attr("y",function(d){return h - y(d.y0)-y(d.y);})
.attr("height", function(d){return y(d.y);})
.attr("width",w);
svg.append("line")
.attr("x1", 0)
.attr("x2", w * sentidata.length)
.attr("y1", h - .5)
.attr("y2", h - .5)
.style("stroke", "#000");
function redraw(data) {
//Transpose the data into layers by Sentiment
var sent = d3.layout.stack()(["posvalue","negvalue"].map(function(sentiment){
return data.map(function(d){
return {x:d.time, y:+d[sentiment]};
});
}));
//Add a group for each Sentiment
var sentiment = svg.selectAll("g.sentiment")
.data(sent)
.transition()
.duration(1000)
.attr("class","sentiment")
.style("fill", function(d,i){return z(i);})
.style("stroke", function(d,i){return d3.rgb(z(i)).darker();});
var rect = sentiment.selectAll("rect")
.data(Object)
.transition()
.duration(1000)
.attr("y",function(d){return h - y(d.y0)-y(d.y);})
.attr("height", function(d){return y(d.y);});
}
</script>
</body>
</html>
初始堆疊圖表得到很好的繪圖。但是當我調用重繪方法時,它給了我「[對象對象]沒有方法'數據'」錯誤。這個錯誤是未來,當我嘗試初始化變量RECT
var rect = sentiment.selectAll("rect")
.data(Object)
不知道我做錯了。我在找東西similar
任何建議將非常感謝!
'。數據()'取值或對象數組要用於圖。不要傳遞「對象」,而是傳遞要顯示的數據。 – 2012-03-14 08:53:51
明白了。由於 我使用方法鏈來在 調用selectAll之後立即創建轉換,因此我已將'var sentiment'定義爲我重繪中的轉換。因此它沒有數據方法,因爲暗示了錯誤 消息。因此,我將該轉換創建爲單獨的聲明 以避免衝突。感謝Mike Bostoc在D3 Google Groups上快速響應。 – 2012-03-14 17:33:39