gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:32:12 25 4
gpt4 key购买 nike

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

The part of the interface being talked about here

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

最佳答案

在您的 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。

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

关于java - 构建类来创建 JFreeChart,如何将其添加到主界面中的 JPanel 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16783592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com