Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFreeChart Bar chart custom color?

I am using JFreeCharts in java to create a bar chart. My question is fairly simple... how can I choose a custom color for all of the bars in a bar chart? I'm not sure if this customization would be done in a GradientPaint. An example of my code that determines bar color is:

   final GradientPaint gp0 = new GradientPaint(
                    0.0f, 0.0f, Color.blue, 
                    0.0f, 0.0f, Color.blue
                );

I'm not sure if this is the right way to go for custom colors or not. Basically, I don't know if GradientPaint is the right way to go or not. If it is, could someone let me know how I could edit this code to make it a custom color rather than blue?

I'm not sure if this helps, but say the information for the custom color was

  • hue: 142
  • Sat: 109
  • Lum:126
  • Red: 79
  • Green: 129
  • Blue: 189

With this is there a way to customize the color of the chart?

like image 637
user3794422 Avatar asked Jul 03 '14 13:07

user3794422


2 Answers

It was a while since i coded with jfreechart.Bud if i remember corectly this was code that i wrote to change bar paint ;).

    CategoryPlot cplot = (CategoryPlot)chart.getPlot();
    cplot.setBackgroundPaint(SystemColor.inactiveCaption);//change background color

    //set  bar chart color

    ((BarRenderer)cplot.getRenderer()).setBarPainter(new StandardBarPainter());

    BarRenderer r = (BarRenderer)chart.getCategoryPlot().getRenderer();
    r.setSeriesPaint(0, Color.blue);

Im looking at the code for my first application ever written.Im not sure if it will work now.

For future i recommend to google out or purchase PDF guide to jfreechart.You find all the references and samples there.Bud if you can ,skip to JavaFX i strongly recommend it ,working with jfreechart is pain.To be honest.Implementing charts in javafx is easy and looks way better ;)

like image 183
Tomas Bisciak Avatar answered Nov 13 '22 21:11

Tomas Bisciak


CategoryPlot plot = chart.getCategoryPlot();
BarRenderer renderer = (BarRenderer) plot.getRenderer();

// set the color (r,g,b) or (r,g,b,a)
Color color = new Color(79, 129, 189);
renderer.setSeriesPaint(0, color);

This will set all bars to that specific color. If you would like the colors to change for each row (say, for a stacked bar chart), you can call dataset.getRowCount(), with dataset being of type CategoryDataset, to return to you the number of rows involved for each column of the bar chart. Then, you could index the series in the renderer.setSeriesPaint() call based on the index of the row.

for (int i = 0; i < dataset.getRowCount(); i++){
    switch (i) {
    case 0:
        // red
        color = new Color(255, 0, 0);
        break;
    case 1:
        // blue
        color = new Color(0, 0, 255);
        break;
    default:
        // green
        color = new Color(0, 255, 0);
        break;
    }
}
like image 27
bradykey Avatar answered Nov 13 '22 22:11

bradykey