0

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

下面是类的代码(减去适当的导入语句):

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 包含许多选项,用于(创建)然后显示的图表,并且按钮是不言自明的......

这里说的界面部分

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

4

1 回答 1

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

Baiscally,我通过表单设计器将布局管理器更改为graphDisplayPanelto BorderLayout,并添加了repaint对尝试强制重绘管理器更新 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();

}                                                     

在此处输入图像描述

于 2013-05-28T03:56:07.703 回答