4
I would like to know, how can I change the colors of the "slices" of the graph and the font where the content and percentage are written. Is it 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);
}
}
in case, if you want to put only the percentage in bold, I could not do : plotting.setLabelGenerator(new Standardpiesectionlabelgenerator("<html> {0} <b> ({2}) </b> </html>")); There is no way to write HTML here inside ?
– Java
I think it is not possible to use HTML tags in this method.
– Pedro
But there is some way to change ?
– Java
Text is considered as an integer string. Changing only the "%" within the string is complicated.
– Pedro