How do I apply a color to a single bar of my singl

2019-03-02 07:17发布

I am new to JFreeChart and I am trying to see what action do what.

In my chart I only have one series, and I would like -according to the value- to set a different color for the bar. For example : 0-20 -> RED, 20-80 -> YELLOW, 80-100 -> GREEN

CategoryPlot plot = chart.getCategoryPlot();
CategoryDataset dataset = plot.getDataset(0);

Number value = dataset.getValue(dataset.getRowKey(0), dataset.getColumnKey(0));
Double val = value.doubleValue();

if (val <= 20.0) {
    BarRenderer renderer = (BarRenderer) plot.getRenderer();
    Paint tmp = renderer.getItemPaint(row, column);

    /*
    ** Help Please
    */      
}

return chart;

This is where I reached, I am stuck here and don't know really where to go. I saw in the documentation that Paint is an interface but none of the class implementing this interface does provide a setXXX() method. So, my two questions are :

  • How do I set a color to a single Bar ?
  • How do I apply that to my chart ?

1条回答
相关推荐>>
2楼-- · 2019-03-02 08:06

You'll need to create your own subclass of BarRenderer and override getItemPaint(). Instead of choosing a color based on column, choose it based on your value. Here's an outline to show how the existing BarRenderer works.

plot.setRenderer(new MyRender());
...
class MyRender extends BarRenderer {

    @Override
    public Paint getItemPaint(int row, int col) {
        System.out.println(row + " " + col + " " + super.getItemPaint(row, col));
        return super.getItemPaint(row, col);
    }
}
查看更多
登录 后发表回答