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

JasperStudio中StackedBarChart按条件分色及堆叠显示问题求助

Fixing Stacked Bar Chart Coloring by Value in JasperStudio

Alright, let's get this sorted for you. The core issue here is that your custom renderer was extending BarRenderer instead of StackedBarRenderer—that's why you lost the stacked behavior. Stacked bar charts depend on StackedBarRenderer to handle the stacking logic, so we need to adjust your custom class to inherit from the right parent.

Step 1: Create a Custom Stacked Bar Renderer

Replace your existing CustomBarRenderer with this version that extends StackedBarRenderer (we'll keep your value-based coloring logic intact):

static class CustomStackedBarRenderer extends StackedBarRenderer {
    private static final long serialVersionUID = 1L;

    public CustomStackedBarRenderer() {
        super();
    }

    @Override
    public Paint getItemPaint(int row, int column) {
        Color color = null;
        CategoryDataset cd = getPlot().getDataset();
        
        if (cd != null) {
            double value = cd.getValue(row, column).doubleValue();
            
            // Apply your color rules based on the item's value
            if (value < 70) {
                color = new Color(231, 104, 110); // Red for values <70
            } else if (value >= 70 && value <= 90) {
                color = new Color(255, 190, 6); // Yellow for 70-90
            } else if (value > 90) {
                color = new Color(5, 176, 76); // Green for values >90
            }
        }
        
        // Fall back to the default renderer color if no match (avoids nulls)
        return color != null ? color : super.getItemPaint(row, column);
    }
}

Step 2: Update the Customize Method

Modify your customize method to use this new stacked renderer instead of the basic BarRenderer:

public void customize(JFreeChart chart, JRChart jrChart) {
    CategoryPlot plot = chart.getCategoryPlot();
    
    // Initialize our custom stacked renderer
    CustomStackedBarRenderer customRenderer = new CustomStackedBarRenderer();
    
    // Optional: Keep your bar painter setting if you prefer that style
    customRenderer.setBarPainter(new StandardBarPainter());
    
    // Replace the default renderer with our custom one
    plot.setRenderer(customRenderer);
}

Why This Works

  • Stacked Behavior: By inheriting from StackedBarRenderer, we retain all the built-in stacking logic that makes stacked bar charts work.
  • Value-Based Coloring: The getItemPaint override targets each individual bar item (not entire series), so every segment of the stack gets colored based on its specific value.
  • Fallback Logic: We added a fallback to the parent class's default color to handle edge cases (like null datasets or unexpected values).

Critical Check

Make sure your JasperStudio chart component is set to Stacked Bar Chart (not regular Bar Chart). If you selected the wrong chart type, even the correct renderer won't fix the stacking issue.

Why Your Previous Approach Failed

Your second code snippet worked for stacking because it used the existing StackedBarRenderer (inherited from the chart's default setup), but it only set colors per series—meaning every bar in a series got the same color, regardless of its value. The custom renderer above fixes this by evaluating each data point individually.

内容的提问来源于stack exchange,提问作者Jamshaid

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 21:23:13