2017-12-15 14 views
2

現在、私は円グラフのスライスがランダムに生成されたデータに従って変化する動的円グラフを設計しようとしています。以下はコードです。各スライスの色は、私が以前に定義されているものに応じて拡大縮小されていないことをD3円グラフの各選択

var dataset = [ 
 
    { name: 'Smooth', percent: 40.00, class: 'custom-normal' }, 
 
    { name: 'Moderate', percent: 10.00, class: 'custom-warning' }, 
 
    { name: 'Heavy', percent: 50.00, class: 'custom-danger' } 
 
]; 
 
var width = 960, 
 
    height = 500, 
 
    radius = Math.min(width, height)/2; //Math.min return the smallest value between width and height (for optimization purposes) 
 

 
var colorValues = d3.scaleOrdinal().domain(["Smooth", "Moderate", "Heavy"]).range(["#605A4C", "#ff9900", "#ff1a1a"]); 
 
var percent = "percent"; //predefine the legend of dataset (the string index) 
 
var category = "class"; 
 
var name = "name"; 
 

 
var pie = d3.pie() 
 
    .value(function(d) { return d[percent]; }) 
 
    .sort(null) 
 
    .padAngle(.02); //the gap 
 

 
var arc = d3.arc() 
 
    .innerRadius(radius - 100) //optimization 
 
    .outerRadius(radius - 20); //optimization 
 

 
var svg = d3.select("#chart") 
 
    .append("svg") 
 
    .attrs({ 
 
     width: width, 
 
     height: height, 
 
     class: "shadow" 
 
    }).append("g") 
 
    .attrs({ 
 
     transform: 'translate(' + width/2 + ',' + height/2 + ')' 
 
    }); 
 
svg.append('g') 
 
    .attrs({ 
 
     class: 'slices' 
 
    }); 
 

 
var path = svg.select('.slices') 
 
    .selectAll('path') 
 
    .data(pie(dataset)) 
 
    .enter().append('path') 
 
    .attrs({ 
 
     d: arc 
 
    }).each(function(d, i) { 
 
     this._current = d; 
 
     console.log(this._current); 
 
     console.log('okay!'); 
 
    }).attrs({ 
 
     class: function(d, i){ 
 
     return d[category]; 
 
     }, 
 
     fill: function(d, i) { 
 
      console.log("this is color value" + colorValues()); 
 
      return colorValues(d[i]); 
 
     } 
 
    }); //initial details (this._current) 
 

 

 
var randomGenerator = setInterval(function() { 
 
    var data = dataset.map(function(d, i) { 
 
     for (var key in d) { 
 
      if (d[key] === "Smooth") { 
 
       //console.log("smooth"); 
 
       dataset[0].percent = Math.floor(Math.random() * 100); 
 
       //console.log(dataset[0].percent); 
 
      } else if (d[key] === "Moderate") { 
 
       dataset[1].percent = Math.floor(Math.random() * 100); 
 
       //console.log(dataset[1].percent); 
 
       //console.log("moderate"); 
 
      } else if (d[key] === "Heavy") { 
 
       dataset[2].percent = Math.floor(Math.random() * 100); 
 
       //console.log(dataset[2].percent); 
 
       //console.log("heavy"); 
 
      } 
 
     } 
 

 
    }); 
 

 
}, 3000); 
 

 

 
var timer = setInterval(function() { 
 
    pie.value(function(d) { 
 
     return d[percent]; 
 
    }); // change the value function 
 
    path = path.data(pie(dataset)); // compute the new angles 
 
    path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs 
 
}, 3000); 
 

 

 
// Store the displayed angles in _current. 
 
// Then, interpolate from _current to the new angles. 
 
// During the transition, _current is updated in-place by d3.interpolate. 
 
function arcTween(a) { 
 
    var i = d3.interpolate(this._current, a); 
 
    console.log(this._current); 
 
    this._current = i(0); 
 
    return function(t) { 
 
     return arc(i(t)); 
 
    }; 
 
}
body { 
 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    margin: auto; 
 
    position: relative; 
 
    width: 960px; 
 
}
<meta charset="utf-8"> 
 
<div id="mydiv" class="widget"> 
 
    <div id="chart" class="Chart chart-container"></div> 
 
</div> 
 
<script src="https://d3js.org/d3.v4.js"></script> 
 
<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>

注意してください。すなわち:var colorValues = d3.scaleOrdinal().domain(["Smooth", "Moderate", "Heavy"]).range(["#605A4C", "#ff9900", "#ff1a1a"]);。私はそれが選択問題でなければならないと信じていますが、私はそれについて何の手がかりも見つけませんでした。それでも、クラスでも、それはvar datasetのデータの最初の行を取得し、すべてに適用されます。親切に私を喜ばせてくれてありがとう。

答えて

3

私が正しく理解していれば、パイ関数を介して、あなたのデータを押すことにより

は、あなたのデータを変更しています。あなたの最初の構造ですが:あなたがベースのくさびを着色している​​あなたのコードでは

{ 
    "data": { 
    "name": "Heavy", 
    "percent": 48, 
    "class": "custom-danger" 
    }, 
    "index": 2, 
    "value": 50, 
    "startAngle": 3.1515926535897933, 
    "endAngle": 6.283185307179586, 
    "padAngle": 0.02 
} 

{ name: 'Smooth', percent: 40.00, class: 'custom-normal' } 

パイ関数を介してこのデータを実行した後、各アークにバインドされたデータは、以下の構造を有していますこれ:

fill: function(d, i) { 
    console.log("this is color value" + colorValues()); 
    return colorValues(d[i]); 
} 

function(d) {}dは、データムを指し、それは、入力データに1つだけの項目である(PARに結合データムticular arc)はオブジェクトであり、d [number]を使用すると、配列を予期することが示唆されます。いずれにしても、この機能を実行するたびにundefinedが得られます。あなたの助けアンドリューため

fill: function(d, i) { 
    console.log("this is color value" + colorValues()); 
    return colorValues(d.data.name); 
} 

var dataset = [ 
 
    { name: 'Smooth', percent: 40.00, class: 'custom-normal' }, 
 
    { name: 'Moderate', percent: 10.00, class: 'custom-warning' }, 
 
    { name: 'Heavy', percent: 50.00, class: 'custom-danger' } 
 
]; 
 
var width = 960, 
 
    height = 500, 
 
    radius = Math.min(width, height)/2; //Math.min return the smallest value between width and height (for optimization purposes) 
 

 
var colorValues = d3.scaleOrdinal().domain(["Smooth", "Moderate", "Heavy"]).range(["#605A4C", "#ff9900", "#ff1a1a"]); 
 
var percent = "percent"; //predefine the legend of dataset (the string index) 
 
var category = "class"; 
 
var name = "name"; 
 

 
var pie = d3.pie() 
 
    .value(function(d) { return d[percent]; }) 
 
    .sort(null) 
 
    .padAngle(.02); //the gap 
 

 
var arc = d3.arc() 
 
    .innerRadius(radius - 100) //optimization 
 
    .outerRadius(radius - 20); //optimization 
 

 
var svg = d3.select("#chart") 
 
    .append("svg") 
 
    .attrs({ 
 
     width: width, 
 
     height: height, 
 
     class: "shadow" 
 
    }).append("g") 
 
    .attrs({ 
 
     transform: 'translate(' + width/2 + ',' + height/2 + ')' 
 
    }); 
 
svg.append('g') 
 
    .attrs({ 
 
     class: 'slices' 
 
    }); 
 

 
var path = svg.select('.slices') 
 
    .selectAll('path') 
 
    .data(pie(dataset)) 
 
    .enter().append('path') 
 
    .attrs({ 
 
     d: arc 
 
    }).each(function(d, i) { 
 
     this._current = d; 
 
     console.log(this._current); 
 
     console.log('okay!'); 
 
    }).attrs({ 
 
     class: function(d, i){ 
 
     return d.data.class; 
 
     }, 
 
     fill: function(d, i) { 
 
      console.log("this is color value" + colorValues()); 
 
      return colorValues(d.data.name); 
 
     } 
 
    }); //initial details (this._current) 
 

 

 
var randomGenerator = setInterval(function() { 
 
    var data = dataset.map(function(d, i) { 
 
     for (var key in d) { 
 
      if (d[key] === "Smooth") { 
 
       //console.log("smooth"); 
 
       dataset[0].percent = Math.floor(Math.random() * 100); 
 
       //console.log(dataset[0].percent); 
 
      } else if (d[key] === "Moderate") { 
 
       dataset[1].percent = Math.floor(Math.random() * 100); 
 
       //console.log(dataset[1].percent); 
 
       //console.log("moderate"); 
 
      } else if (d[key] === "Heavy") { 
 
       dataset[2].percent = Math.floor(Math.random() * 100); 
 
       //console.log(dataset[2].percent); 
 
       //console.log("heavy"); 
 
      } 
 
     } 
 

 
    }); 
 

 
}, 3000); 
 

 

 
var timer = setInterval(function() { 
 
    pie.value(function(d) { 
 
     return d[percent]; 
 
    }); // change the value function 
 
    path = path.data(pie(dataset)); // compute the new angles 
 
    path.transition().duration(750).attrTween("d", arcTween); // redraw the arcs 
 
}, 3000); 
 

 

 
// Store the displayed angles in _current. 
 
// Then, interpolate from _current to the new angles. 
 
// During the transition, _current is updated in-place by d3.interpolate. 
 
function arcTween(a) { 
 
    var i = d3.interpolate(this._current, a); 
 
    console.log(this._current); 
 
    this._current = i(0); 
 
    return function(t) { 
 
     return arc(i(t)); 
 
    }; 
 
}
body { 
 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    margin: auto; 
 
    position: relative; 
 
    width: 960px; 
 
}
<meta charset="utf-8"> 
 
<div id="mydiv" class="widget"> 
 
    <div id="chart" class="Chart chart-container"></div> 
 
</div> 
 
<script src="https://d3js.org/d3.v4.js"></script> 
 
<script src="https://d3js.org/d3-selection-multi.v0.4.min.js"></script>

+0

ありがとう:nameは(私は仮定)と使用:

は、代わりにあなたが欲しいのデータのプロパティにアクセスします。はい、これはまさに私が意味していたものです。しかし、どのようにしてクラスの問題を解決できるのか分かりますか?クラスを個々のスライスにも添付したいと思います。 – cypatrick

+0

申し訳ありません、スニペットを更新して、 'd.data.class'を使用するクラスプロパティにアクセスしました。クラスはもともとd.classにありましたが、pie()を実行した後にd.data.class –

+0

@cypatrickこの回答があなたの問題を解決した場合は、[受諾](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)を考慮する必要があります。これにより、同じ問題に直面している将来の読者が正しい解決策を特定するのに役立ちます。 [*](https://stackoverflow.com/help/accepted-answer)と[* "誰かが私の質問に答えたときにどうすればいいですか?" *](https://stackoverflow.com/help/someone-answers)。 – altocumulus

関連する問題