2016-08-04 150 views
0

Here is a link to the CodePenD3.js餅圖轉換

我有了它的數據發生變化時,設置被改變(現在我的數據只是隨機挑選,但我的公文正確做到這一點)的餅圖。我的問題是,我不明白使用轉換來更新餅圖。

這是創建餅圖的過程:

function createPieChart() 
{ 
    var width = 320, 
     height = 320, 
     radius = Math.min(width, height)/2; 

    var color = d3.scale.ordinal() 
    .range(["#0083CB", "#006BA5", "#00527f"]); 

    var arc = d3.svg.arc() 
    .outerRadius(radius - 10) 
    .innerRadius(0); 

    var labelArc = d3.svg.arc() 
    .outerRadius(radius - 40) 
    .innerRadius(radius - 40); 

    var pie = d3.layout.pie() 
    .sort(null) 
    .value(function(d) { return d.population; }); 

    var svg = d3.select("body").append("svg") 
    .attr("width", width) 
    .attr("height", height) 
    .append("g") 
    .attr("transform", "translate(" + width/2 + "," + height/2 + ")"); 

    data = updateData(); 

    var g = svg.selectAll(".arc") 
    .data(pie(data)) 
    .enter().append("g") 
    .attr("class", "arc"); 

    g.append("path") 
    .attr("d", arc) 
    .style("fill", function(d) { return color(d.data.age); }); 

    g.append("text") 
    .attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")"; }) 
    .attr("dy", ".35em") 
    .text(function(d) { return d.data.age; }); 

    function type(d) { 
    d.population = +d.population; 
    return d; 
    } 
} 

這是我更新的餅圖功能。我得到的出現來自於D3 LIB錯誤ReferenceError: s is not defined

function updatePieChart() 
{ 
    svg.selectAll("path").data(pie(data)).transition().duration(500) 
    .attrTween("d", arcTween); 
} 

而且arcTween功能:

function arcTween(a) { 
    var i = d3.interpolate(this._current, a); 
    this._current = i(0); 
    return function(t) { return arc(i(t)); }; 
} 

任何幫助將是巨大的。當我搜索時,我只能找到甜甜圈餅圖。

謝謝。

+0

你的變量's'不)的'updateDegrees範圍('之內。什麼是's'? – ksav

回答

1

我修改了一下你的代碼。看看this教程,瞭解如何輸入,更新和退出選擇工作。另外,不要使用兩個庫。你可以用d3來達到你的要求。 希望這有助於。

var data = []; 
 
var targetDegree = [ "Associate degree", "Bachelors degree" ]; 
 

 
var width = 320, 
 
     height = 320, 
 
     radius = Math.min(width, height)/2; 
 

 
    var color = d3.scale.ordinal() 
 
    .range(["#0083CB", "#006BA5", "#00527f"]); 
 

 
    var arc = d3.svg.arc() 
 
    .outerRadius(radius - 10) 
 
    .innerRadius(0); 
 

 
    var labelArc = d3.svg.arc() 
 
    .outerRadius(radius - 40) 
 
    .innerRadius(radius - 40); 
 

 
    var pie = d3.layout.pie() 
 
    .sort(null) 
 
    .value(function(d) { return d.population; }); 
 

 
    var svg = d3.select("body").append("svg") 
 
    .attr("width", width) 
 
    .attr("height", height) 
 
    .append("g") 
 
    .attr("transform", "translate(" + width/2 + "," + height/2 + ")"); \t 
 
\t 
 
function updateData() 
 
{ \t 
 
\t 
 
    var outweigh = Math.random() * 100, 
 
     not_outweigh = Math.random() * 100, 
 
     same = Math.random() * 100;  
 

 
    var data = [ 
 
    { age: "outweigh", population: outweigh }, 
 
    { age: "does not", population: not_outweigh }, 
 
    { age: "same", population: same } 
 
    ]; 
 

 
    return data; 
 
} 
 
updatePieChart(); 
 
\t 
 
function updateDegrees(s) 
 
{ 
 
    var index = targetDegree.indexOf(s); // Check existence 
 
    if (index > -1) // Already exists and needs to be removed 
 
    targetDegree.splice(index, 1); 
 
    else   // Doe snot exist so should be added 
 
    { 
 
\t targetDegree.push(s); 
 
    } 
 
    updatePieChart(); 
 
} 
 

 
$("#bachelor_check").change(function() { 
 
    updateDegrees("Bachelors degree"); 
 
}); 
 

 
$("#associate_check").change(function() { 
 
    updateDegrees("Associate degree"); 
 
}); 
 

 
$('#majors_sel').on('change', function() { 
 
    updatePieChart(); 
 
}); 
 

 
function updatePieChart() 
 
{ 
 
\t data = updateData(); 
 
\t var g = svg.selectAll(".arc") 
 
    .data(pie(data)); 
 

 
\t var arcpaths = g.enter().append('g').attr('class','arc'); 
 
\t 
 
\t arcpaths.append('path').attr("d", arc) 
 
    \t \t .style("fill", function(d) { return color(d.data.age); }); 
 
\t arcpaths.append('text').attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")"; }) 
 
    \t \t .attr("dy", ".35em") 
 
    \t \t .text(function(d) { return d.data.age; }); 
 
\t 
 
\t 
 
\t g.exit().remove(); \t 
 
\t 
 
\t var arcpaths = g.select('path').attr("d", arc) 
 
    .style("fill", function(d) { return color(d.data.age); }); 
 
\t 
 
\t var arctext = g.select("text") 
 
    .attr("transform", function(d) { return "translate(" + labelArc.centroid(d) + ")"; }) 
 
    .attr("dy", ".35em") 
 
    .text(function(d) { return d.data.age; }); 
 
\t 
 

 
    function type(d) { 
 
    d.population = +d.population; 
 
    return d; 
 
    } 
 
} 
 

 

 
function arcTween(a) { 
 
    var i = d3.interpolate(this._current, a); 
 
    this._current = i(0); 
 
    return function(t) { return arc(i(t)); }; 
 
}
.arc text { 
 
    font: 10px sans-serif; 
 
    text-anchor: middle; 
 
    } 
 

 
    .arc path { 
 
    stroke: #fff; 
 
    }
<script src="//d3js.org/d3.v3.min.js"></script> \t 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<select id='majors_sel'> 
 
    <option value="All">All majors</option> 
 
    <option value="Engineering">Engineering</option> 
 
    <option value="Life sciences">Life sciences</option> 
 
    <option value="Physical sciences/math">Physical sciences/math</option> 
 
    <option value="Health">Health</option> 
 
    <option value="Education">Education</option> 
 
    <option value="Business/management">Business/management</option> 
 
    <option value="Computer/information sciences">Computer/information sciences</option> 
 
    <option value="Law">Law</option> 
 
    <option value="Social/behavioral sciences">Social/behavioral sciences</option> 
 
    <option value="Humanities">Humanities</option> 
 
    <option value="Vocational/technical training">Vocational/technical training</option> 
 
    <option value="Undeclared">Undeclared</option> 
 
    <option value="Other (Please specify):">Other</option> 
 
    </select> 
 

 
    <div id='degree_check'> 
 
    <label> 
 
     <input id='bachelor_check' type='checkbox' name='degree' value='Bachelors degree' checked> 
 
    Bachelors</label> 
 
    <label> 
 
     <input id='associate_check' type='checkbox' name='degree' value='Associate degree' checked> 
 
    Associate</label> 
 
    </div>