建立类来创建JFreeChart,如何在主界面中将其添加到JPanel? [英] Built class to create a JFreeChart, how do I add it to a JPanel in my main interface?

查看:53
本文介绍了建立类来创建JFreeChart,如何在主界面中将其添加到JPanel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Have just come across JFreeChart and am trying it out for my project... I created a class, called "CountryVsCountryChart", that I want to use to create a new chart, according to the options parsed through. What I want to know is how can I add an object of this class to a JPanel in my main interface? I want to be able to do this via selecting an option in a JComboBox, but I think I would be able to handle that...

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

Here is the the code for the class (minus the appropriate import statements) below:

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"中:

The next bit of code is for a button listener - for a button I click to display the chart in the jpanel. At the moment it is only two lines of code for testing purposes. The code is in my main interface class called "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包含许多要创建(然后创建)的图表的选项,并且按钮不言自明...

Also, not sure if this will help, but here is a screen dump of the part of my interface I'm referring to. The white panel is where I want the graph to show up, the JComboBox contains numerous options for what chart to (create) then display, and the button is self explanatory...

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

As for posting SSCCE's - I wasnt sure whether to include the whole chart class or not. Since I am new to JFreeChart, I thought someone might need to analyse the whole class - as the structure of it may be a problem (as well). If you want to run the project, you can clone it from GitHub here - https://github.com/rattfieldnz/Java_Projects/tree/master/PCricketStats.

推荐答案

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

In your CountryVsCountryChart, you've not actually added anything to it...

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); 
}

通常,我通过desinger形式将graphDisplayPanel的布局管理器更改为BorderLayout,并向repaint添加了调用以尝试强制重新绘制管理器更新UI.

Baiscally, I changed the layout manager for graphDisplayPanel to BorderLayout, via the form desinger and added a call to repaint to try and force the repaint manager to update the UI.

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();

}                                                     

这篇关于建立类来创建JFreeChart,如何在主界面中将其添加到JPanel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆