JFreeChart Bar chart custom color?
Asked Answered
K

4

13

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?

Kester answered 3/7, 2014 at 13:44 Comment(1)
I thank everyone for the help. But I found just doing this to my already established code final GradientPaint gp0 = new GradientPaint( 0.0f, 0.0f, new Color(79, 129, 189), 0.0f, 0.0f, new Color(79, 129, 189) ); fixed the issue. I'm new to coding so not sure if this is better or worse than the already submitted answers. But it was the easiest for me. Thanks to everyone who answered though. I appreciate it.Kester
Y
19

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

Yolande answered 3/7, 2014 at 14:0 Comment(3)
JavaFX is interesting. However, all of the data that these charts are made of come from a mysql database. I use a jdbc connection to loop through values from tables in my database and populate a chart. Would javafx have this feature/ability?Kester
Ofcourse.You just have to implement it.There is no easy chart.setMyData(dataOfAnyKind); // done method.Bud it doesnt matter where you take data from.If its a text file,database,csv file etc .First of all you have to parse it to fit your needs or chart needs.In general you always prepare your data before you do something with it .Same logic apply.So dont be afraid and go for JavaFX.With JfreeChart you will have to do same amount of work + adapt ui which is way harder comared to SceneBuilder with javafx ,fxml.Bud its up to you.Yolande
@Kester As you pointed out in comment.You already took data from db.So all work done.Now focus on ui.And JavaFX have better implementation of charts then jfreechart imo.Yolande
K
6
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;
    }
}
Kindle answered 8/7, 2014 at 19:48 Comment(0)
A
1

Custom Colors in Bar Chart using JfreeChart

CategoryItemRenderer barColor = new CustomRenderer(new Paint[]{});
plot.setRenderer(barColor);

create a new class name is CustomRenderer extends BarRenderer3D or you choose BarRenderer

class CustomRenderer extends BarRenderer3D {

    private Paint[] colors;
    public CustomRenderer(final Paint[] colors) {
        this.colors = colors;
    }

    public Paint getItemPaint(final int row, final int column) {
        if(column==0)
            return Color.blue;
        else if(column==1)
            return Color.CYAN;
        else  
            return Color.RED;
   }
}
Aphrodite answered 6/5, 2017 at 6:41 Comment(0)
A
0

I think easiest way is using getRenderer().setSeriesPaint(index, color) method.

So as an example you can try the below code for a bar chart which has 3 bars grouped.

JFreeChart barChart = ChartFactory.createBarChart(
                "Bar Chart Titke",
                "Category", "Score",
                dataset,PlotOrientation.HORIZONTAL,
                true, true, false);

        CategoryPlot plot = barChart.getCategoryPlot();
        plot.getRenderer().setSeriesPaint(0, new Color(128, 0, 0));
        plot.getRenderer().setSeriesPaint(1, new Color(0, 0, 255));
        plot.getRenderer().setSeriesPaint(2, new Color(0, 230, 255));
Ancilin answered 20/6, 2018 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.