Rob
Rob

Reputation: 1280

Built class to create a JFreeChart, how do I add it to a JPanel in my main interface?

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...

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;

    }
}

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

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...

The part of the interface being talked about here

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.

Upvotes: 0

Views: 441

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

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

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

}                                                     

enter image description here

Upvotes: 1

Related Questions