考慮如果你要表達不同區域的每季的營業額, 而每個圓餅圖代表著一個區域. 這時你便需要不只一個圓餅圖來呈現你的圖表. 這理要介紹如何實現多維度的圓餅圖. 首先你的資料將會以二維的陣列來表達, 並透過容器 CategoryDataset 來存放你的資料與對應的 label. 而在建立該容器可透過 DatasetUtilities 的方法 [])]createCategoryDataset() 來完成. 有了資料, 接下來便是圖表的建立. 透過 ChartFactory 的方法 createMultiplePieChart() 來建立多維度圓餅圖的圖表.
範例代碼 :
底下為建立 多維度圓餅圖的範例代碼, 當中也介紹如何修改圖表 Label 的字型與大小與 Label 顯示的內容.
- MultiplePieChartDemo1.java :
- package john.jfreechart.piechart;
- import java.awt.Font;
- import org.jfree.chart.ChartFactory;
- import org.jfree.chart.ChartPanel;
- import org.jfree.chart.JFreeChart;
- import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
- import org.jfree.chart.plot.MultiplePiePlot;
- import org.jfree.chart.plot.PiePlot;
- import org.jfree.data.category.CategoryDataset;
- import org.jfree.data.general.DatasetUtilities;
- import org.jfree.ui.ApplicationFrame;
- import org.jfree.ui.RefineryUtilities;
- import org.jfree.util.TableOrder;
- /**
- * A simple demonstration application showing how to create a chart consisting of multiple
- * pie charts.
- * @author John-Lee
- *
- */
- public class MultiplePieChartDemo1 extends ApplicationFrame {
- /**
- * Creates a new demo.
- *
- * @param title the frame title.
- */
- public MultiplePieChartDemo1(String title) {
- super(title);
- CategoryDataset dataset = createDataset();
- JFreeChart chart = createChart(dataset);
- ChartPanel chartPanel = new ChartPanel(chart, true, true, true, false, true);
- chartPanel.setPreferredSize(new java.awt.Dimension(650, 400));
- setContentPane(chartPanel);
- }
- /**
- * Creates a sample dataset.
- *
- * @return A sample dataset.
- */
- private CategoryDataset createDataset() {
- double[][] data = new double[][] {
- {3.0, 4.0, 3.0, 5.0},
- {5.0, 7.0, 6.0, 8.0},
- {5.0, 7.0, 3.0, 8.0},
- {1.0, 2.0, 3.0, 4.0},
- {2.0, 3.0, 2.0, 3.0}
- };
- CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
- "Region ", // rowKeyPrefix (Index of different PieCharts)
- "Sales/Q", // columnKeyPrefix (Index of PieChart's content)
- data
- );
- return dataset;
- }
- /**
- * Creates a sample chart with the given dataset.
- *
- * @param dataset the dataset.
- *
- * @return A sample chart.
- */
- private JFreeChart createChart(CategoryDataset dataset) {
- JFreeChart chart = ChartFactory.createMultiplePieChart(
- "Multiple Pie Chart", // chart title
- dataset, // dataset
- TableOrder.BY_ROW,
- true, // include legend
- true,
- false
- );
- MultiplePiePlot plot = (MultiplePiePlot) chart.getPlot();
- JFreeChart subchart = plot.getPieChart();
- PiePlot p = (PiePlot) subchart.getPlot();
- p.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}"));
- p.setLabelFont(new Font("SansSerif", Font.PLAIN, 10));
- // This controls the space between the edges of the pie plot and the plot area itself
- //(the region where the section labels appear).
- //p.setInteriorGap(0.03);
- return chart;
- }
- /**
- * Starting point for the demonstration application.
- *
- * @param args ignored.
- */
- public static void main(String[] args) {
- MultiplePieChartDemo1 demo = new MultiplePieChartDemo1("Multiple Pie Chart Demo 1");
- demo.pack();
- RefineryUtilities.centerFrameOnScreen(demo);
- demo.setVisible(true);
- }
- }
執行結果 :