简体   繁体   English

从饼图中删除0%

[英]Remove 0% from pie chart

I am having trouble removing the unwanted 0% from the pie-chart. 我无法从饼图中删除不需要的0%。 This number shows total in % of all the available options. 此数字显示所有可用选项的总百分比。 In the case an option doesn't have any value according to a filter it displays a 0% for it, that I want to remove. 如果选项根据过滤器没有任何值,则显示0%,我想删除。 I am pasting my code below. 我在下面粘贴我的代码。

function drawPieChart(versionStatusCanvas,on_hold, active, cancelled, 
activecount, onholdcount, cancelledcount) {

    var versionStatusData = [{
    value: on_hold,
    color:"#444444",
    label: "On Hold(" +onholdcount+")",
},
{
    value: active,
    color: "#72bb53",
    label: "Active(" +activecount+")",
},
{
    value: cancelled,
    color: "#ff6624",
    label: "Cancelled(" +cancelledcount+")",

}];
var versionStatusCanvas = document.getElementById("versionStatus");
var versionStatusCtx = versionStatusCanvas.getContext("2d");
var versionStatusChart = new Chart(versionStatusCtx).Pie(versionStatusData,{
    animationSteps: 100,
    animationEasing: 'easeInOutQuart',
    showTooltips: false,
    segmentShowStroke : false,
    onAnimationProgress: drawSegmentValues
});

document.getElementById('version-status-legend').innerHTML = 
versionStatusChart.generateLegend();

function drawSegmentValues(){
var radius = versionStatusChart.outerRadius;
var midX = versionStatusCanvas.width/2;
var midY = versionStatusCanvas.height/2
for(var i=0; i<versionStatusChart.segments.length; i++){
    versionStatusCtx.fillStyle="white";
    var textSize = versionStatusCanvas.width/20;
    versionStatusCtx.font= textSize+"px Verdana";
    // Get needed variables
    var value = versionStatusChart.segments[i].value + '%';
    var startAngle = versionStatusChart.segments[i].startAngle;
    var endAngle = versionStatusChart.segments[i].endAngle;
    var middleAngle = startAngle + ((endAngle - startAngle)/2);

    // Compute text location
    var posX = (radius/2) * Math.cos(middleAngle) + midX;
    var posY = (radius/2) * Math.sin(middleAngle) + midY;

    // Text offside by middle
    var w_offset = versionStatusCtx.measureText(value).width/2;
    var h_offset = textSize/4;
    if(value != '0.0%')
    versionStatusCtx.fillText(value, posX - w_offset, posY + h_offset);
}
}
}

The values are passed from the php controller in JSON. 这些值是从JSON中的php控制器传递的。 i have tried various methods to get this working but I am not able to do it. 我已经尝试了各种方法来使这项工作,但我无法做到这一点。 Any assistance would be appreciated. 任何援助将不胜感激。

Try this: 尝试这个:

if(parseInt(value) > 0)
    versionStatusCtx.fillText(value, posX - w_offset, posY + h_offset);
}

parseInt(value) parses the value to an int to compare to zero. parseInt(value)parseInt(value)解析为int以与零进行比较。 Previously in the code the value joined a % symbol to the value making it a string. 以前在代码中,值将%符号连接到值,使其成为字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM