从饼图中删除0%

时间:2017-05-03 15:47:24

标签: php jquery codeigniter-3 pie-chart chart.js

我无法从饼图中删除不需要的0%。此数字显示所有可用选项的总百分比。如果选项根据过滤器没有任何值,则显示0%,我想删除。我正在粘贴下面的代码。

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);
}
}
}

这些值是从JSON中的php控制器传递的。我已经尝试了各种方法来使这项工作,但我无法做到这一点。任何援助将不胜感激。

1 个答案:

答案 0 :(得分:3)

试试这个:

set x=f(y)

if(parseInt(value) > 0) versionStatusCtx.fillText(value, posX - w_offset, posY + h_offset); } 将值解析为int以与零进行比较。以前在代码中,值将%符号连接到值,使其成为字符串。