2012-03-13 26 views
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

何か提案は本当に感謝されます!

+0

'.dataの()'プロットに使用する値またはオブジェクトの配列をとります。 "Object"を渡す代わりに、表示したいデータを渡します。 –

+0

selectAllの直後に遷移を作成するためにメソッドチェーンを使用していたので、私は再描画の遷移として 'var sentiment'を定義しました。したがって、エラー メッセージが暗示されているため、データメソッドはありません。だから私は別のステートメント としてコンフリクトを避けるためにトランジションを作成しました。D3 Googleグループですばやく反応するMike Bostocに感謝しています。 –

答えて

関連する問題