3

how to paint different bars in different colors, I tried to use renderer, here is my sample code:

    public IntervalXYDataset createDataset() throws InterruptedException {
    parseFile();
    final XYSeries series = new XYSeries("Analysis");

    int i=0;
    while(parsedArray[i]!=0)
        {

        series.add(xaxisArray[i], yaxisArray[i]);

        i++;
    }

    final XYSeriesCollection dataset = new XYSeriesCollection(series);

     dataset.setIntervalWidth(0.15);//set width here

    return dataset;
}

and this is how I am drawing the graph:

public className (final String title) throws InterruptedException {
    super(title);
    IntervalXYDataset dataset = createDataset();
    JFreeChart chart = createChart(dataset);
    final ChartPanel chartPanel = new ChartPanel(chart);
     XYPlot plot = (XYPlot) chart.getPlot();
    plot.getRenderer().setSeriesPaint( 0, Color.black);//0 works and paints all 40 bars in black, 1 and above fails. 
             // plot.getRenderer().setSeriesPaint( 1, Color.green);// this fails
    chartPanel.setPreferredSize(new java.awt.Dimension(2000,1000));//(width,height) of display
    setContentPane(chartPanel);

}

I am able to set the width as I have commented in my program, however I now want to set the color for different bars, for example I want to get hold of bar in chart and draw red for array[0] and blue for [3] and orange for cell[17], can you please guide me on this. Thank you very much.


N West
  • 6,768
  • 25
  • 40
David Prun
  • 8,203
  • 16
  • 60
  • 86

3 Answers3

3

What you want to do is the following:

XYPlot plot = (XYPlot) chart.getPlot();
plot.getRenderer().setSeriesPaint(1, Color.yellow);

Replace 1 with the (zero-based) index of the bar whose color you would like to change.

Edit to respond to comment:

List<Color> myBarColors = .....

XYPlot plot = (XYPlot) chart.getPlot();
XYItemRenderer renderer = plot.getRenderer();

for (int i = 0; i < 40; i++) {
    renderer.setSeriesPaint(i, myBarColors.get(i));
}

Edit 2: Misunderstood OPs problem, new solution in comments.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Jordan Bentley
  • 1,309
  • 1
  • 8
  • 28
  • This is a straightforward approach; but `Color.yellow` is the fourth default color, so it fails for more than three series. – trashgod Sep 14 '11 at 20:01
  • Hello, I tried doing this, it works for value '0' as in setSeriesPaint( 0, Color.yellow); but I have around 40 bars - this loops for 40 times:-(series.add(xaxisArray[i], yaxisArray[i]);) can you please help me fix this error. I want to set color for each bar. Thank you so much. – David Prun Sep 14 '11 at 21:00
  • 1
    @helloMaga I don't know *exactly* what you're trying to do, but I updated my answer to show setting colors in a loop. – Jordan Bentley Sep 14 '11 at 21:22
  • Hello Jordan, please see my question above, I have declared my problem. – David Prun Sep 14 '11 at 21:36
  • @helloMaga Ok, I took another look at the original post. I think that you're using the wrong class for your dataset. I don't have anything in front of me to test this, but try using CatagoricalDataset or XYBarDataset. If you use those the setSeriesPaint() method should work for what you're trying to do. – Jordan Bentley Sep 15 '11 at 00:36
2

The most flexible approach is to override the getItemPaint() method of AbstractRenderer in a custom XYBarRenderer, as shown here for XYLineAndShapeRenderer.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

I found the answer Create two series, and then add how many ever bars you want and set color for each series. using setSeriesPaint

David Prun
  • 8,203
  • 16
  • 60
  • 86