7

how to customize the colors of JFreeChart graphic. lets see my java code :

private StreamedContent chartImage ;

public void init(){
    JFreeChart jfreechart = ChartFactory.createPieChart("title", createDataset(), true, true, false);
    File chartFile = new File("dynamichart");
    ChartUtilities.saveChartAsPNG(chartFile, jfreechart, 375, 300);
    chartImage = new DefaultStreamedContent(new FileInputStream( chartFile), "image/png");
}

public PieDataset createDataset() {
    DefaultPieDataset dataset = new DefaultPieDataset();
          dataset.setValue("J-2", 10);
          dataset.setValue("J-1", 15);
          dataset.setValue("J", 50);
          dataset.setValue("J+1", 20);
          dataset.setValue("J+2", 15);
    return dataset;
}

html page :

<p:graphicImage id="MyImage" value="#{beanCreateImage.chartImage}" />
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Karim Oukara
  • 2,638
  • 8
  • 38
  • 51

3 Answers3

13

You can change the color of single pieces like this:

JFreeChart chart = ChartFactory.createPieChart("title", createDataset(), true, true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionPaint("J+1", Color.black);
plot.setSectionPaint("J-1", new Color(120, 0, 120));
// or do this, if you are using an older version of JFreeChart:
//plot.setSectionPaint(1, Color.black);
//plot.setSectionPaint(3, new Color(120, 0, 120));

So with your code, all the pies are colored automatically, after my code changes, the J-1 and J+1 have a fixed color, the rest gets automatically colored.

Comparison

brimborium
  • 9,362
  • 9
  • 48
  • 76
  • how to see the result of new Color(227, 26, 28), – Karim Oukara Oct 22 '12 at 11:31
  • 1
    if `plot.setSectionPaint(String id, Color col)` does not exist, you are using a quite old version of JFreeChart. And yes, then you can simply access the individual sections with their index (which is deprecated in newer versions of JFreeChart). – brimborium Oct 22 '12 at 12:04
  • 1
    What do you mean by *see the result of 'new Color(227, 26, 28)'*? You mean like a [Color Picker](http://www.colorpicker.com/)? – brimborium Oct 22 '12 at 12:06
  • I found that new Color(int x, int y, int z) means : x : red, y: green and z: blue. – Karim Oukara Oct 22 '12 at 12:28
  • Ah ok, you can look that stuff up in the [documentation](http://docs.oracle.com/javase/1.4.2/docs/api/java/awt/Color.html). If you are using a newer IDE like eclipse or netbeans, you can hover over the code (`Color` in this case) with your mouse and you will see additional information. – brimborium Oct 22 '12 at 12:33
10

To set the colous for a chart you can implement the DrawingSupplier inferface in this case I've used DefaultDrawingSupplier:

public class ChartDrawingSupplier extends DefaultDrawingSupplier  {

    public Paint[] paintSequence;
    public int paintIndex;
    public int fillPaintIndex;

    {
        paintSequence =  new Paint[] {
                new Color(227, 26, 28),
                new Color(000,102, 204),
                new Color(102,051,153),
                new Color(102,51,0),
                new Color(156,136,48),
                new Color(153,204,102),
                new Color(153,51,51),
                new Color(102,51,0),
                new Color(204,153,51),
                new Color(0,51,0),
        };
    }

    @Override
    public Paint getNextPaint() {
        Paint result
        = paintSequence[paintIndex % paintSequence.length];
        paintIndex++;
        return result;
    }


    @Override
    public Paint getNextFillPaint() {
        Paint result
        = paintSequence[fillPaintIndex % paintSequence.length];
        fillPaintIndex++;
        return result;
    }   
}

Then include this code in your `init()' method

JFreeChart jfreechart = ChartFactory.createPieChart("title", createDataset(), true, true, false);
Plot plot = jfreechart.getPlot();
plot.setDrawingSupplier(new ChartDrawingSupplier());
...
GrahamA
  • 5,875
  • 29
  • 39
  • how to define the color of Color(227, 26, 28)? – Karim Oukara Oct 22 '12 at 11:30
  • 1
    @berber5 new Color(227, 26, 28) is just a way to define a colour by setting the Red, Green and Blue values. You shold pick a set of colours that suit your application, you can find some examples [here](http://www.w3schools.com/html/html_colors.asp) and [here](http://www.htmlgoodies.com/tutorials/web_graphics/consistent-colors-for-your-site-all-you-need-to-know-about-web-safe-colors.html) – GrahamA Oct 22 '12 at 11:44
  • +1 Interesting, I did not know about the 'DrawingSupplier' classes. Although in his case, I would prefer my way. – brimborium Oct 22 '12 at 12:09
  • You might want to consider using a DefaultDrawingSupplier constructor and call through to `super()` as you can provide that Paint sequence without having to override any methods yourself. – Anthony Chuinard Sep 20 '16 at 20:38
1

You can customize the colors according to the labels while getting the data from the dataset:

// Add custom colors
        PiePlot plot = (PiePlot) chart.getPlot();

        for (int i = 0; i < dataset.getItemCount(); i++) {
            if(dataset.getKey(i).equals("J+1")){ 
                plot.setSectionPaint(i, Color.black);
            }
        }

You can also use a switch-case statement or the one you prefer.

pateto777
  • 249
  • 1
  • 11