Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

java - How do I apply a color to a single bar of my single series graph?

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 ?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...