我是Javafx的新手,并尝试阅读和理解互联网上有关如何使用Scenebuilder从Derby数据库创建数据图表的教程。
我设法创建了一个条形图 - 工作正常。
但是我在饼图中显示相同的数据时遇到了一些麻烦。场景构建器启动但不显示饼图。
如果将它显示为饼图,我错过了什么? 感谢您的帮助: - )
我的代码:
public class DocumentController implements Initializable {
@FXML
PieChart piechart;
private Connection connexion;
ArrayList < String > p = new ArrayList < String > ();
ArrayList < Double > c = new ArrayList < > ();
@Override
public void initialize(URL url, ResourceBundle rb) {
loadData();
}
public void loadData() {
String query = "select P, C From PIE "; //ORDER BY P asc
ObservableList < PieChart.Data > piechartdata;
piechartdata = FXCollections.observableArrayList();
try {
connexion = connectDB();
ResultSet rs = connexion.createStatement().executeQuery(query);
while (rs.next()) {
piechartdata.add(new PieChart.Data(rs.getString("P"), rs.getDouble("C")));
p.add(rs.getString("P"));
c.add(rs.getDouble("C"));
}
} catch (Exception e) {
}
}
private Connection connectDB() {
try {
String dbString = "jdbc:derby://localhost:1527/Pie";
String user = "pie";
String password = "pie";
Connection conn = DriverManager.getConnection(dbString, user, password);
System.out.println("Connection Ok");
return conn;
} catch (SQLException ex) {
Logger.getLogger(DocumentController.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
&#13;
答案 0 :(得分:0)
我想你忘了将ObservableList的数据设置为你的pieChart
尝试这样的事情:
piechart.setData(piechartdata);
piechart.setVisible(true);