2016-05-31 3 views
0

マップに複数の円グラフを配置し、csvファイル(この例では「Total」)の対応する値に基づいてサイズを調整したいとします。しかし、どのように半径を調整しても、パイは表示されません。私が逃した重要なことはありますか?これまでサイズ付き円グラフ

マイコード:

d3.csv("Bevoelkerung-Altersstruktur-2010-Summe.csv", function drawPies (data) { 
var pie = d3.layout.pie() 
.sort(null) 
.value(function(d) { return +d}); 

var arc = d3.svg.arc() 
.innerRadius(0) 
.outerRadius(function(d) { 
    return d.Total; });  


var pies = svg.selectAll('.pie') 
.data(data) 
.enter() 
.append('g') 
.attr('class', 'pie') 
.attr("transform", function(d) { 
    return "translate(" + projection([d.lon, d.lat])[0] + "," + projection([d.lon, d.lat])[1] + ")"; 
}); 

var color = d3.scale.ordinal() 
     .range(["#98abc5", "#7b6888", "#a05d56", "#d0743c",]) 
     .domain(d3.range(0,4)); 

pies.selectAll('.slice') 
.data(function(d){ 
return pie([d.Group1, d.Group2, d.Group3, d.Group4]); }) 
.enter() 
.append('path') 
.attr('d', arc) 
.style('fill', function(d,i){ 
    return color(i); 
}); 

Hereは完全なコードへのリンクです。

+1

は、すべてのあなたのコードを追加してくださいでした、データが含まれています。たぶんJSFiddleを作成することもできますか? – thatOneGuy

+0

私はBl.ocksにソースコードを置き、最初の投稿にリンクを追加しました。リンクされたソースコードでは、 ".outerRadius"の値を固定値に変更してパイが見えるようにし、どのように見えるかを知ることができます。 – LeBaton

答えて

1

私は正しい方法でコードを実行できませんでしたので、私はいくつかのことをplnkrで動作させるように動かしました。

// You had all the async calls to remote data files nested which I 
// recommend not doing. I separated your GeoJSON rendering and your 
// pie rendering into two distinct functions. 

// Start GeoJSON rendering 
d3.csv("Jugendarbeitslosigkeit.csv", function(data) { 
    //Load in GeoJSON data 
    d3.json("PolenReg2.json", function(json) { 
    data.forEach(function(d, i) { 
    // ...more code 
    // This is a tricky part 
    // Since we separated the polygon and pie rendering 
    // and the polygon calls will take longer due to size 
    // the group containing the polygons will be rendered 
    // last, thus rendering the group after your pie group. 
    // This will make your pies stay behind the polygon paths 
    // that's why we use the insert. In order to position 
    // the polygons layer below the pies. 
    svg 
    .insert('g', ':first-child') 
    // ... more code 
// End GeoJSON rendering 

// Start Pie rendering 
d3.csv("Bevoelkerung-Altersstruktur-2010-Summe.csv", function(err, data) { 
    // Set our large pie function 
    var pie = d3.layout.pie() 
    .sort(null) 
    .value(function(d) { 
     return +d.key; 
    }); 
    // ... more code 
// End Pie rendering 

重要な部分はここにある:

var pies = svg 
    .append('g') // Add a group with the class 'big-pies' 
    .attr('class', 'big-pies') 
    .selectAll('.pie') // Join data to create groups by each polygon (as far as I understand) 
    .data(data) 
    .enter() 
    .append('g') 
    .attr('class', 'pie') 
    .attr("transform", function(d) { 
     var proj = projection([d.lon, d.lat]); 
     return "translate(" + proj[0] + "," + proj[1] + ")"; 
    }) 
    .selectAll('.slice') // Start pie - data join 
    .data(function(d) { 
     // set slice data with additional total value 
     // so that we can use this value at the attr d 
     // function 
     return pie([{ 
     key: d.Kinder, 
     tot: d.total 
     }, { 
     key: d.Jugendliche, 
     tot: d.total 
     }, { 
     key: d.Erwachsene, 
     tot: d.total 
     }, { 
     key: d.Rentner, 
     tot: d.total 
     }]); 
    }) 
    .enter() 
    .append('path') 
    .attr('d', function(d, i) { 
     // return the arc function with outer radius increased by the total value 
     return d3.svg.arc().innerRadius(0).outerRadius(d.data.tot * 2).call(d, d) 
    }) 
    .style('fill', function(d, i) { 
     return c10(i); 
    }); 

Plnkr:https://plnkr.co/edit/CwiFnNmfIleo5zZ6BseW?p=preview

+0

ありがとう、パイはそのように働いています。しかし、夫婦はこれ以上働かないようです。私が違う尺度で試しても、以前と同じ分類には達しません。 – LeBaton