2016-09-06 14 views
0

d3jsを使用して表示されるテキストを自動更新する必要があります。ソースはjsonファイルです。私は以下のd3jsコードを持っています(グラフのmbostokの自動リフレッシュコードに基づいています)。テキストは毎回そのようにされていません。自動更新テキストd3js

jsonファイルの値を変更しようとしましたが、表示されるテキストが更新されていないことがわかりました。 updateData()では、コンソールに値を表示して正しく表示しました。表示されるテキストに正しい値が表示されない理由を確認しないでください。

final_json_file.json

jsoncir.html

<!DOCTYPE html> 
<meta charset="utf-8"> 
<style> /* set the CSS */ 

body { font: 12px Arial;} 

path { 
    stroke: steelblue; 
    stroke-width: 2; 
    fill: none; 
} 

.axis path, 
.axis line { 
    fill: none; 
    stroke: grey; 
    stroke-width: 1; 
    shape-rendering: crispEdges; 
} 

</style> 
<body> 

<!-- load the d3.js library -->  
<script src="http://d3js.org/d3.v3.min.js"></script> 

<script> 

// Set the dimensions of the canvas/graph 
var margin = {top: 100, right: 20, bottom: 30, left: 50}, 
    width = 900 - margin.left - margin.right, 
    height = 570 - margin.top - margin.bottom; 


// Set the ranges 
var x = d3.time.scale() 
.range([0, width]); 
var y = d3.scale.linear().range([height, 0]); 



// Adds the svg canvas 
var svg = d3.select("body") 
    .append("svg") 
     .attr("width", width + margin.left + margin.right) 
     .attr("height", height + margin.top + margin.bottom) 
    .append("g") 
     .attr("transform", 
       "translate(" + margin.left + "," + margin.top + ")"); 

// Get the data 
d3.json("final_json_file.json", function(error, data) { 
    data.forEach(function(d) { 

     d.close = d.cnt; 


    }); 


    svg.selectAll("circle") 
     .data(data) 
     .enter().append("circle") 
     .attr("class", "point") 
     .attr("r", 40.5) 
     .attr("fill", "#BADBDA") 


     svg.selectAll("text") 
     .data(data) 
     .enter().append("text") 
     .attr("dx", 12) 
     .attr("dy", ".35em") 
     .attr("text-anchor", "middle") 
     .text(function(d) { return d.close }) 


}); 



var inter = setInterval(function() { 
       updateData(); 
     }, 12000); 


     // ** Update data section (Called from the onclick) 
function updateData() { 

    // Get the data again 
d3.json("final_json_file.json", function(error, data) { 
    data.forEach(function(d) { 

     d.close = d.cnt; 

     console.log(d.close); 

    }); 


    // Select the section we want to apply our changes to 
    var svg = d3.select("body").transition(); 


//  console.log('ehllo'); 

    // Make the changes 
     svg.select("text") // change the line 
       .duration(12000) 
       .text(function(d) { return d.close +1}); 





    }); 
} 


</script> 
</body> 

入力JSONファイル[{ "CNT":49976、 "st_time": "2016年9月1日"}]

答えて

0

あなた有界データをtext要素に更新しないでください。最初にsvg.selectAll("text").(data)を使用して要素にデータをバインドしましたが、updateData関数ではデータを更新しませんでした。新しいデータを要素にバインドし、テキストを更新する必要があります。

// Select the section we want to apply our changes to 
var svg = d3.select("body"); 

// Make the changes 
svg.selectAll("text") 
    .data(data) 
    .transition() 
    .duration(1000) 
    .text(function (d) { 
    return d.close + 1 
    });