You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ChartJS环形图数值为1的类别无背景色问题求助

Fix: Tiny "Pending" Category Not Visible in Chart.js Doughnut Chart

Hey there, I’ve dealt with this exact frustration before—when a tiny data point gets squashed into an invisible line in a doughnut chart, even though it’s correctly set up. Let’s break down why this happens and how to fix it quickly.

Why This Happens

Your "Pending" category has a value of 1, which is extremely small compared to the other values (112, 140, etc.). Chart.js calculates arc width based on relative proportions, so this tiny slice gets compressed into a nearly invisible line—especially with the default doughnut cutout size.

Solutions to Make the Tiny Slice Visible

1. Force a Minimum Angle for All Arcs

Chart.js 3.0+ has a built-in minAngle option for arc elements, which ensures every slice takes up at least a specified number of degrees, regardless of its data value. This is the most effective fix.

2. Narrow the Doughnut Cutout

Reducing the size of the inner hollow (increasing the width of the doughnut ring) gives small slices more space to be visible.

Modified Code Example

Here’s how to implement both fixes in your chart setup:

const ctx = document.getElementById('yourChartId').getContext('2d');
const taskChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
        labels: ["Overdue", "Waiting for me to sign", "Waiting for partner to sign", "In progress", "Pending"],
        datasets: [{
            data: [112, 140, 35, 3, 1],
            backgroundColor: ["#FF4657", "#FFD336", "#00DCFF", "#00C597", "#0062FF"],
            borderWidth: 0 // Optional: Remove border to save space for small slices
        }]
    },
    options: {
        responsive: true,
        plugins: {
            tooltip: {
                // Optional: Show exact values in tooltip to clarify proportions
                callbacks: {
                    label: function(context) {
                        const label = context.label || '';
                        const value = context.parsed || 0;
                        const total = context.dataset.data.reduce((a, b) => a + b, 0);
                        const percentage = Math.round((value / total) * 100);
                        return `${label}: ${value} (${percentage}%)`;
                    }
                }
            }
        },
        // Narrow the inner cutout to widen the doughnut ring
        cutout: '60%', // Default is 50%—adjust lower for a wider ring
        // Force every slice to take up at least 5 degrees
        elements: {
            arc: {
                minAngle: 5
            }
        }
    }
});

Key Notes

  • minAngle: Start with 5 degrees and adjust if needed—too large, and your larger slices will look distorted.
  • cutout: Values are percentages of the chart’s width. 0% makes it a pie chart, 90% makes an extremely thin ring.
  • Tooltip Clarity: Adding exact values and percentages helps users understand that the small slice represents a tiny proportion, even if it looks bigger visually due to minAngle.

After applying these changes, your "Pending" slice will show up as a clear blue segment instead of a thin line, while still maintaining readability for the larger categories.

内容的提问来源于stack exchange,提问作者Pran R.V

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 16:22:27