gpt4 book ai didi

php - 从饼图中删除 0%

转载 作者:行者123 更新时间:2023-12-01 07:43:21 26 4
gpt4 key购买 nike

我无法从饼图中删除不需要的 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 Controller 传递。我尝试了各种方法来使其正常工作,但我无法做到。任何援助将不胜感激。

最佳答案

试试这个:

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

parseInt(value) 将值解析为 int 以便与零进行比较。之前在代码中,值将 % 符号加入到值中,使其成为字符串。

关于php - 从饼图中删除 0%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43764678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com