我正在使用饼图通过JFreeChart软件包在JFrame
中显示一些统计信息。我正在使用PieDataset
传递给createChart
方法。同样,数据集设置为动态更新,因为我在应用程序中使用了观察者模式,并对其进行了彻底调试。我的问题是,尽管能够动态更新数据集,但框架上的饼图无法按原样更新,也许无法采用新的(更新的)值并显示结果。我想知道是否还有其他方法可以动态更新饼图。
public void update(Observable observable, Object o) {
if (o instanceof Game || o instanceof GameController) {
String label = "<html><head><h1>World Domination View</h1></head><body> ";
territoriesList = Game.getInstance().getGameMap().getTerritories();
String[] name = new String[Game.getInstance().getNumPlayers()] ;
Float[] value = new Float[Game.getInstance().getNumPlayers()];
for (int i = 0; i < Game.getInstance().getNumPlayers(); i++) {
int tempTerritoryCount = 0;
float percentage = 0;
for (Territory territory : territoriesList) {
if (territory.getOwner() == Game.getInstance().getPlayerById(i)) {
tempTerritoryCount += 1;
}
}
percentage = (float) 100.0 * tempTerritoryCount / Game.getInstance().getGameMap().getTerritories().size();
label = label + Game.getInstance().getPlayerById(i).getName() + " owns " + percentage
+ " % territories and owns " + PhaseView.getInstance().curPArmies + "<br/>";
worldDominationViewLabel.setText(label);
//dt = createDataset(Game.getInstance().getPlayerById(i).getName(),percentage);
//createDataset(Game.getInstance().getPlayerById(i).getName(),percentage);
//for(int j=0; j<Game.getInstance().getNumPlayers();j++){
name[i] = Game.getInstance().getPlayerById(i).getName();
//}
if(name[i] != null)
{
value[i] = percentage;
}
}
dt = createDataset(name,value);
chartPanel = new ChartPanel(createChart(dt));
chartPanel.setBounds(1024,0,310,300);//390
chartPanel.setVisible(true);
frameFromPhase.add(chartPanel);
worldDominationViewLabel.setText(label + "</body></html>");}}
//the dataset definition
public static PieDataset createDataset(String[] plname,Float[] value){
DefaultPieDataset dataset = new DefaultPieDataset();
for(int i =0; i< plname.length;i++){
dataset.setValue(plname[i],value[i]);
}
return dataset;
}
//chart definition
public static JFreeChart createChart(PieDataset dataset ) {
JFreeChart chart = ChartFactory.createPieChart(
"Player Map Domination: Territorywise", // chart title
dataset, // data
true, // include legend
true,
false);
return chart;
}
答案 0 :(得分:1)