2017-08-24 25 views
1

我偶然玩的D3js庫以可視化的一些SQL-JSON_LD數據的ARC,想做到以下幾點:添加<id> + <data>到D3-餅圖

  • 將各個ID -tag以及數據集(矩陣用各種元素),以每片

我的代碼現在看起來像這樣

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<style> 
 
    path { 
 
    fill: #ccc; 
 
    stroke: #333; 
 
    stroke-width: 1.5px; 
 
    transition: fill 250ms linear; 
 
    transition-delay: 150ms; 
 
    } 
 
    
 
    path:hover { 
 
    fill: #999; 
 
    stroke: #000; 
 
    transition-delay: 0; 
 
    } 
 
</style> 
 

 
<body> 
 
    <script src="//d3js.org/d3.v3.min.js"></script> 
 
    <script> 
 
    var data = { 
 
    {"year":"2017-07-01","value":"1"}, 
 
    {"year":"2017-07-02","value":"1"}, 
 
    {"year":"2017-07-03","value":"2"}, 
 
    {"year":"2017-07-04","value":"3"}, 
 
    {"year":"2017-07-05","value":"5"}, 
 
    {"year":"2017-07-06","value":"8"}, 
 
    {"year":"2017-07-07","value":"13"}, 
 
    {"year":"2017-07-08","value":"21"}, 
 
    {"year":"2017-07-09","value":"24"}, 
 
    {"year":"2017-07-10","value":"55"}, 
 
    {"year":"2017-07-11","value":"89"},}; 
 

 
    var width = 960, 
 
     height = 500; 
 
     arc_ids = d3.range(data.length); // for naming the arcs 
 

 
    var outerRadius = height/2 - 20, 
 
     innerRadius = outerRadius/3, 
 
     cornerRadius = 10; 
 

 
    var pie = d3.layout.pie() 
 
     .padAngle(.02); 
 

 
    var arc = d3.svg.arc() 
 
     .padRadius(outerRadius) 
 
     .innerRadius(innerRadius); 
 

 
    var svg = d3.select("body").append("svg") 
 
     .attr("width", width) 
 
     .attr("height", height) 
 
     .attr("id","viz_pieChart") // adds an ID to the whole chart 
 
     .append("g") 
 
     .attr("transform", "translate(" + width/2 + "," + height/2 + ")"); 
 

 
    svg.selectAll("path") 
 
     .data(pie(data.map(function(d) { return parseInt(d.value); }))) 
 
     .attr("id", function(d, i) { console.log('CP1'); return "arc-" + arc_ids[i]; }) // This was intended to add an individual id to each arc, but it doesn't 
 
     .attr("data", function(d) { return d.data; }) // attach data to arc according to value, as e.g.: {"year":"2017-07-01","value":"1"} 
 
     .enter().append("path") 
 
     .each(function(d) { 
 
     d.outerRadius = outerRadius - 20; 
 
     }) 
 
     .attr("d", arc) 
 
     .on("mouseover", arcTween(outerRadius, 0)) 
 
     on("click", function(d){console.log(d.id);console.log(d.data.toString())}); //print id of the clicked arc as well as saved data 
 
     .on("mouseout", arcTween(outerRadius - 20, 150)); 
 

 
    function arcTween(outerRadius, delay) { 
 
     return function() { 
 
     d3.select(this).transition().delay(delay).attrTween("d", function(d) { 
 
      var i = d3.interpolate(d.outerRadius, outerRadius); 
 
      return function(t) { 
 
      d.outerRadius = i(t); 
 
      return arc(d); 
 
      }; 
 
     }); 
 
     }; 
 
    } 
 
//test whether an arc can be reached, e.g. the 2nd Element 
 
console.log(document.getElementById('slice-1')); // gives an error 
 
    </script>

我也查this1this2this3因爲他們似乎有希望的,但它仍然無法爲我工作。

之後,我想使用弧的附加數據將其打印到另一個svg圖形。但第一次的地址必須工作。

對於具有多個特定問題的post,我很抱歉!

謝謝你的幫助!

+0

你說你想要做什麼,但什麼是你的問題?發佈的代碼有問題嗎?它的輸出是什麼?什麼是期望的輸出? – tima

+0

謝謝你tima,我提出了我的問題以及更精確的代碼。 現在,代碼不起作用。特別是每個弧的id和數據的附加/地址不起作用。 – Tanoshimi

+0

對於每個弧上的id,你可以只用 返回「arc-」+ i; 然後你會得到arc-1,arc-2,arc-3等的id。 –

回答

-1

必須追加路徑之前給它一個id或數據

<!DOCTYPE html> 
 
<meta charset="utf-8"> 
 
<style> 
 
    path { 
 
    fill: #ccc; 
 
    stroke: #333; 
 
    stroke-width: 1.5px; 
 
    transition: fill 250ms linear; 
 
    transition-delay: 150ms; 
 
    } 
 
    
 
    path:hover { 
 
    fill: #999; 
 
    stroke: #000; 
 
    transition-delay: 0; 
 
    } 
 
</style> 
 

 
<body> 
 
    <script src="//d3js.org/d3.v3.min.js"></script> 
 
    <script> 
 
    var data = [ 
 
    {"year":"2017-07-01","value":"1"}, 
 
    {"year":"2017-07-02","value":"1"}, 
 
    {"year":"2017-07-03","value":"2"}, 
 
    {"year":"2017-07-04","value":"3"}, 
 
    {"year":"2017-07-05","value":"5"}, 
 
    {"year":"2017-07-06","value":"8"}, 
 
    {"year":"2017-07-07","value":"13"}, 
 
    {"year":"2017-07-08","value":"21"}, 
 
    {"year":"2017-07-09","value":"24"}, 
 
    {"year":"2017-07-10","value":"55"}, 
 
    {"year":"2017-07-11","value":"89"}]; 
 

 
    var width = 960, 
 
     height = 500; 
 
     arc_ids = d3.range(data.length); // for naming the arcs 
 

 
    var outerRadius = height/2 - 20, 
 
     innerRadius = outerRadius/3, 
 
     cornerRadius = 10; 
 

 
    var pie = d3.layout.pie() 
 
     .padAngle(.02); 
 

 
    var arc = d3.svg.arc() 
 
     .padRadius(outerRadius) 
 
     .innerRadius(innerRadius); 
 

 
    var svg = d3.select("body").append("svg") 
 
     .attr("width", width) 
 
     .attr("height", height) 
 
     .attr("id","viz_pieChart") // adds an ID to the whole chart 
 
     .append("g") 
 
     .attr("transform", "translate(" + width/2 + "," + height/2 + ")"); 
 

 
    svg.selectAll("path") 
 
     .data(pie(data.map(function(d) { 
 
     return parseInt(d.value); 
 
     }))) 
 
      .enter().append("path") 
 
     .each(function(d) { 
 
     d.outerRadius = outerRadius - 20; 
 
     }) 
 
     .attr("id", function(d, i) { return "arc-" + arc_ids[i]; }) 
 
     
 
     // This was intended to add an individual id to each arc, but it doesn't 
 
     .attr("data", function(d) { return d.data; }) // attach data to arc according to value, as e.g.: {"year":"2017-07-01","value":"1"} 
 

 
     .attr("d", arc) 
 
     .on("mouseover", arcTween(outerRadius, 0)) 
 
     .on("click", function(d){ 
 
     console.log(this.id); 
 
     console.log(d.data.toString()) 
 
     }) //print id of the clicked arc as well as saved data 
 
     .on("mouseout", arcTween(outerRadius - 20, 150)); 
 

 
    function arcTween(outerRadius, delay) { 
 
     return function() { 
 
     d3.select(this).transition().delay(delay).attrTween("d", function(d) { 
 
      var i = d3.interpolate(d.outerRadius, outerRadius); 
 
      return function(t) { 
 
      d.outerRadius = i(t); 
 
      return arc(d); 
 
      }; 
 
     }); 
 
     }; 
 
    } 
 
//test whether an arc can be reached, e.g. the 2nd Element 
 
console.log(document.getElementById('slice-1')); // gives an error 
 
    </script>

+0

嗨青蛙,謝謝你的提示。有用! :) 現在只是一個小小的補充:我如何確保在創建/更新時,正確的ID是否附加到每個元素上,而不僅僅是在點擊它們之後? – Tanoshimi

+0

現在有效。這只是我身邊的一個錯誤。感謝您的幫助! – Tanoshimi