MultiplePieChart的自定义标题

时间:2014-06-06 12:44:18

标签: java charts label jfreechart

需要在JFreeChart中创建带有自定义图表标签的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”时,这对我来说没有好处....如何为饼图制作自定义标签?

1 个答案:

答案 0 :(得分: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;
}