Modifications in JFreeChart

4

I'd like to know how I can change the colors of the "slices" in the chart and the font in which the content and percentage are written. It's possible? at least the colors.

package grafico;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class GraficoPizzaDemo extends ApplicationFrame {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    public GraficoPizzaDemo() {
        super(null);
        this.setTitle("Grafico de Pizza");
        JPanel jpanel = PanelDemostracao();
        jpanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(jpanel);
    }

    private static PieDataset criaDadosGrafico() {
        DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
        defaultpiedataset.setValue("Conteúdo 1", 43.23D);
        defaultpiedataset.setValue("Conteúdo 2", 10D);
        defaultpiedataset.setValue("Conteúdo 3", 27.5D);
        defaultpiedataset.setValue("Conteúdo 4", 17.5D);
        defaultpiedataset.setValue("Conteúdo 5", 11D);
        defaultpiedataset.setValue("Conteúdo 6", 19.39D);
        return defaultpiedataset;
    }

    private static JFreeChart criaGrafico(PieDataset piedataset) {
        JFreeChart jfreechart = ChartFactory.createPieChart(
                "Gráfico Pizza Demo ", piedataset, true, true, false);

        PiePlot plotagem = (PiePlot) jfreechart.getPlot();

        plotagem.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} ({2})"));//define porcentagem no gráfico
        plotagem.setLabelBackgroundPaint(new Color(220, 220, 220));
        return jfreechart;
    }

    public static JPanel PanelDemostracao() {
        JFreeChart jfreechart = criaGrafico(criaDadosGrafico());
        return new ChartPanel(jfreechart);
    }

    public static void main(String args[]) {
        GraficoPizzaDemo demo = new GraficoPizzaDemo();
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}
    
asked by anonymous 26.05.2017 / 17:19

1 answer

4

To change the color of certain sliced, you can use the following:

    plotagem.setSectionPaint("Conteúdo 1", Color.black);

Where the first parameter is the key of the slice, and the second is the color.

Or, if you prefer to use an RGB color:

   plotagem.setSectionPaint("Conteúdo 1", new Color(0, 255, 0));

And to change the font:

    Font font = new Font("Arial", Font.PLAIN, 25);           
    plotagem.setLabelFont(font);
    
26.05.2017 / 17:29