By using PiePlot
object, you can customize colors to the sections of Piechart.
Step 1: Get the PiePlot object from Pie chart.
PiePlot plot
= (PiePlot) chart.getPlot();
Step 2: Paint each section to your favorite color using
setSectionPaint method of PiePlot object.
plot.setSectionPaint("section1",
new Color(255, 0, 0));
import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.data.general.DefaultPieDataset; public class Main { public static void main(String args[]) { /* Create dataset */ DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue("2004-2005", 58); dataset.setValue("2005-2006", 41); dataset.setValue("2006-2007", 85.3); dataset.setValue("2007-2008", 81); /* create chart */ JFreeChart chart = ChartFactory.createPieChart("Simple Piechart", dataset); /* Get PiePlot object */ PiePlot plot = (PiePlot) chart.getPlot(); /* Set colors to sections */ plot.setSectionPaint("2004-2005", new Color(255, 0, 0)); plot.setSectionPaint("2005-2006", new Color(0, 255, 0)); plot.setSectionPaint("2006-2007", new Color(0, 0, 255)); plot.setSectionPaint("2007-2008", new Color(0, 0, 0)); /* create and display chart on frame */ ChartFrame frame = new ChartFrame("First", chart); frame.pack(); frame.setVisible(true); } }
No comments:
Post a Comment