how to change pie chart colors of JFreeChart?

后端 未结 3 647
北恋
北恋 2021-01-18 06:30

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

private StreamedContent chartImage ;

public void init(){
    JFreeChart jfreech         


        
相关标签:
3条回答
  • 2021-01-18 07:04

    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.

    0 讨论(0)
  • 2021-01-18 07:14

    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

    0 讨论(0)
  • 2021-01-18 07:14

    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());
    ...
    
    0 讨论(0)
提交回复
热议问题