how to change pie chart colors of JFreeChart?

后端 未结 3 681
北恋
北恋 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: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());
    ...
    

提交回复
热议问题