从饼图中删除 0%
Remove 0% from pie chart
我无法从饼图中删除不需要的 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);
}
}
}
值从 php 控制器传递到 JSON。我已经尝试了各种方法来使它正常工作,但我无法做到。任何援助将不胜感激。
试试这个:
if(parseInt(value) > 0)
versionStatusCtx.fillText(value, posX - w_offset, posY + h_offset);
}
parseInt(value)
将值解析为 int 以与零进行比较。以前在代码中,该值将一个 % 符号连接到该值,使其成为一个字符串。
我无法从饼图中删除不需要的 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);
}
}
}
值从 php 控制器传递到 JSON。我已经尝试了各种方法来使它正常工作,但我无法做到。任何援助将不胜感激。
试试这个:
if(parseInt(value) > 0)
versionStatusCtx.fillText(value, posX - w_offset, posY + h_offset);
}
parseInt(value)
将值解析为 int 以与零进行比较。以前在代码中,该值将一个 % 符号连接到该值,使其成为一个字符串。