构建类来创建JFreeChart,如何将其添加到主界面的JPanel中?

时间:2013-05-28 03:51:31

标签: java swing jfreechart

刚刚遇到JFreeChart,我正在为我的项目尝试...我根据解析的选项创建了一个名为“CountryVsCountryChart”的类,我想用它创建一个新图表。我想知道的是如何将这个类的对象添加到主界面的JPanel中?我希望能够通过在JComboBox中选择一个选项来做到这一点,但我想我能够处理...

以下是该类的代码(减去相应的import语句):

public class CountryVsCountryChart extends JPanel
{
    private static final long serialVersionUID = 1L;
    private ArrayList<Player> players;
    private StatUtilities stats;

    public CountryVsCountryChart(String applicationTitle, String chartTitle, ArrayList<Player> players, int option) {
        //super(applicationTitle);

        this.players = players;
        stats = new StatUtilities();

        // This will create the dataset 
        PieDataset dataset = createDataset(option);
        // based on the dataset we create the chart
        JFreeChart chart = createChart(dataset, chartTitle);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);
        // default size
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
}



/** * Creates a sample dataset */

    private  PieDataset createDataset(int graphDisplayOption) {

        ArrayList<String> countries = new ArrayList<String>();
        for (Player p : players) {
            countries.add(p.getCountryName());
        }

        //Get unique country names
        Set<String> countryNames = new HashSet<String>(countries);

        DefaultPieDataset result = new DefaultPieDataset();

        /*
         * The below code block uses a switch statement to determine
         * which type of stats to display in the graph (country by country).
         * 
         * Options for the switch statement are as follows:
         * 
         * 1 = Average Balls Bowled
         * 2 = Average of Bowling Averages
         * 3 = Average Career Lengths
         * 4 = Average Economy Rates
         * 5 = Average Number of 5 Wicket Innings
         * 6 = Average Innings Played
         * 7 = Average Matches Played
         * 8 = Average Runs Conceded
         * 9 = Average Strike Rates
         * 10 = Average WicketsTaken
         */
        for(String c: countryNames)
        {
            switch(graphDisplayOption)
            {
                case 1:
                    result.setValue(c, stats.aveBallsBowled(players, c));
                    break;
                case 2:
                    result.setValue(c, stats.aveBowlingAverage(players, c));
                    break;
                case 3:
                    result.setValue(c, stats.aveCareerLength(players, c));
                    break;
                case 4:
                    result.setValue(c, stats.aveEconRate(players, c));
                    break;
                case 5:
                    result.setValue(c, stats.aveFiveWicketsInns(players, c));
                    break;
                case 6:
                    result.setValue(c, stats.aveInningsPerCountry(players, c));
                    break;
                case 7:
                    result.setValue(c, stats.aveMatchesPerPlayer(players, c));
                    break;
                case 8:
                    result.setValue(c, stats.aveRunsConceded(players, c));
                    break;
                case 9:
                    result.setValue(c, stats.aveStrikeRate(players, c));
                    break;
                case 10:
                    result.setValue(c, stats.aveWickets(players, c));
                    break;
            }
        }

        return result;

    }


/** * Creates a chart */

    private JFreeChart createChart(PieDataset dataset, String title) {

        JFreeChart chart = ChartFactory.createPieChart3D(
            title,                  // chart title
            dataset,                // data
            true,                   // include legend
            true,
            false
        );

        PiePlot3D plot = (PiePlot3D) chart.getPlot();
        plot.setStartAngle(290);
        plot.setDirection(Rotation.CLOCKWISE);
        plot.setForegroundAlpha(0.5f);
        return chart;

    }
}

下一段代码用于按钮监听器 - 对于按钮,我单击以在jpanel中显示图表。目前只有两行代码用于测试目的。代码在我的主接口类中称为“AppInterface”:

private void comparePlayerStatsBtnActionPerformed(java.awt.event.ActionEvent evt) {                                                      

        /*
         * Include below information in a loop to generate chart, based on option selected.
         */
        CountryVsCountryChart chart = new CountryVsCountryChart("Test Chart", "A Test Chart", players, 1);
        /**/

        graphDisplayPanel.add(chart);
    }   

另外,不确定这是否有帮助,但这里是我所指的界面部分的屏幕转储。白色面板是我希望图形显示的地方,JComboBox包含多个选项,用于显示(创建)图表然后显示,按钮是自解释的......

The part of the interface being talked about here

至于发布SSCCE - 我不确定是否包括整个图表类。由于我是JFreeChart的新手,我认为有人可能需要分析整个班级 - 因为它的结构可能也是一个问题(也是如此)。如果您想运行该项目,可以在此处从GitHub克隆它 - https://github.com/rattfieldnz/Java_Projects/tree/master/PCricketStats

1 个答案:

答案 0 :(得分:1)

在您的CountryVsCountryChart中,您实际上并没有添加任何内容......

public class CountryVsCountryChart extends JPanel
{
    private static final long serialVersionUID = 1L;
    private ArrayList<Player> players;
    private StatUtilities stats;

    public CountryVsCountryChart(String applicationTitle, String chartTitle, ArrayList<Player> players, int option) {
        //super(applicationTitle);

        this.players = players;
        stats = new StatUtilities();

        // This will create the dataset 
        PieDataset dataset = createDataset(option);
        // based on the dataset we create the chart
        JFreeChart chart = createChart(dataset, chartTitle);
        // we put the chart into a panel
        ChartPanel chartPanel = new ChartPanel(chart);

        // Don't forget me...
        setLayout(new BorderLayout());
        add(chartPanel); 
}

通过形式设计,我将graphDisplayPanel的布局管理器更改为BorderLayout并添加了对repaint的调用,以尝试强制重绘管理器更新UI。 / p>

private void comparePlayerStatsBtnActionPerformed(java.awt.event.ActionEvent evt)
{                                                      

    /*
     * Include below information in a loop to generate chart, based on option selected.
     */
    CountryVsCountryChart chart = new CountryVsCountryChart("Test Chart", "A Test Chart", players, 1);
    /**/

    graphDisplayPanel.add(chart);
    repaint();

}                                                     

enter image description here