2014-06-06 169 views
0

需要在JFreeChart中使用自定義圖表標籤創建multiplePieChart。在文檔中,圖表標題自動創建。 文檔例如:MultiplePieChart的自定義標題

package demo; 

import java.awt.Dimension; 
import java.awt.Font; 
import javax.swing.JPanel; 
import org.jfree.chart.*; 
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; 

public class MultiplePieChartDemo1 extends ApplicationFrame 
{ 

    public MultiplePieChartDemo1(String s) 
    { 
     super(s); 
     JPanel jpanel = createDemoPanel(); 
     jpanel.setPreferredSize(new Dimension(600, 380)); 
     setContentPane(jpanel); 
    } 

    private static CategoryDataset createDataset() 
    { 
     double ad[][] = { 
      { 
       3D, 4D, 3D, 5D 
      }, { 
       5D, 7D, 6D, 8D 
      }, { 
       5D, 7D, (0.0D/0.0D), 3D 
      }, { 
       1.0D, 2D, 3D, 4D 
      }, { 
       2D, 3D, 2D, 3D 
      } 
     }; 
     CategoryDataset categorydataset = DatasetUtilities.createCategoryDataset("Region ", "Sales/Q", ad); 
     return categorydataset; 
    } 

    private static JFreeChart createChart(CategoryDataset categorydataset) 
    { 
     JFreeChart jfreechart = ChartFactory.createMultiplePieChart("Multiple Pie Chart", categorydataset, TableOrder.BY_ROW, true, true, false); 
     MultiplePiePlot multiplepieplot = (MultiplePiePlot)jfreechart.getPlot(); 
     JFreeChart jfreechart1 = multiplepieplot.getPieChart(); 
     PiePlot pieplot = (PiePlot)jfreechart1.getPlot(); 
     pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}")); 
     pieplot.setLabelFont(new Font("SansSerif", 0, 8)); 
     return jfreechart; 
    } 

    public static JPanel createDemoPanel() 
    { 
     JFreeChart jfreechart = createChart(createDataset()); 
     return new ChartPanel(jfreechart); 
    } 

    public static void main(String args[]) 
    { 
     MultiplePieChartDemo1 multiplepiechartdemo1 = new MultiplePieChartDemo1("JFreeChart: MultiplePieChartDemo1.java"); 
     multiplepiechartdemo1.pack(); 
     RefineryUtilities.centerFrameOnScreen(multiplepiechartdemo1); 
     multiplepiechartdemo1.setVisible(true); 
    } 
} 

但在我的情況下,我有名稱(例如「超級」,「學生」,「VIP」)的網絡流量分配的關稅。當JFreeChart生成「關稅1」,「關稅2」時,對我來說這是沒有好處的。如何爲餅圖製作自定義標籤?

+0

Cross-posted [here](http://www.jfree.org/forum/viewtopic.php?f=3&t=116931)。 – trashgod

回答

1

我認爲DatasetUtilities#createCategoryDataset是專門爲演示而不是產品代碼。試試這個:

private static CategoryDataset createDataset() 
{ 

    DefaultCategoryDataset categorydataset = new DefaultCategoryDataset(); 

    categorydataset.addValue(3D, "Super", "Sales/Q1"); 
    categorydataset.addValue(4D, "Super", "Sales/Q2"); 
    categorydataset.addValue(3D, "Super", "Sales/Q3"); 
    categorydataset.addValue(5D, "Super", "Sales/Q4"); 

    categorydataset.addValue(5D, "Student", "Sales/Q1"); 
    categorydataset.addValue(7D, "Student", "Sales/Q2"); 
    categorydataset.addValue(6D, "Student", "Sales/Q3"); 
    categorydataset.addValue(8D, "Student", "Sales/Q4"); 

    categorydataset.addValue(5D, "VIP", "Sales/Q1"); 
    categorydataset.addValue(7D, "VIP", "Sales/Q2"); 
    categorydataset.addValue(0D, "VIP", "Sales/Q3"); 
    categorydataset.addValue(3D, "VIP", "Sales/Q4"); 

    return categorydataset; 
} 
+0

thx!這是我需要的。 – disable1992