我正在尝试使用Java从保存在sql数据库中的数据生成统计图(BarCharts和LineCharts)。 该数据可能会不时更改。
我尝试使用ObservableLists,但收到错误消息。
以下是我到目前为止尝试过的内容; 注意:这位于DatabaseHandler类中
public ObservableList<BarChart.Data> getItemGraphStatistics(){
ObservableList<BarChart.Data> data = FXCollections.observableArrayList();
String sql = "SELECT itemID, COUNT(*) FROM (SELECT itemID FROM SALES UNION ALL SELECT itemID FROM STOCK) t GROUP BY itemID";
System.out.println(sql );
ResultSet rs = handler.execQuery(sql);
try{
while(rs.next()){
String item= rs.getString("itemID");
int count = rs.getInt(2);
data.add(new BarChart.Data(item+ "(" + count + ")", count));
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}
运行上述代码时
java.lang.ClassCastException: javafx.scene.chart.XYChart$Data cannot be cast to javafx.scene.chart.XYChart$Series
当我尝试将BarChart.Data更改为BarChart.Series时,无法将int转换为observableList错误消息。
以下是上述示例的示例;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXRadioButton;
import com.jfoenix.controls.JFXTextField;
import java.net.URL;
import java.util.Calendar;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import sales.manager.database.DatabaseHandler;
/**
* FXML Controller class
*
* @author Garande
*/
public class PerformanceAnalyzerController implements Initializable {
@FXML
private AnchorPane analysisHandler;
@FXML
private AnchorPane graphHandler;
@FXML
private PieChart piechart;
@FXML
private BarChart<?, ?> barchart;
@FXML
private LineChart<?, ?> linechart;
@FXML
private NumberAxis yBarGraph;
@FXML
private CategoryAxis xBarGraph;
@FXML
private NumberAxis yLineGraph;
@FXML
private CategoryAxis xLineGraph;
DatabaseHandler handler = DatabaseHandler.getInstance();
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void performAnalysisOperation(ActionEvent event) {
//Performing analysis operation
barchart.setData(handler.getItemGraphStatistics());
graphHandler.getChildren().add(barchart);
}
}
答案 0 :(得分:1)
data
的{{1}}属性包含一个XYChart
。每个ObservableList<XYChart.Series>
还都有一个XYChart.Series
属性,但它拥有一个data
。问题是您的方法返回了一个ObservableList<XYChart.Data>
,然后您尝试在ObservableList<XYChart.Data>
上进行设置-类型不匹配。我不确定您如何获得XYChart.data
,因为您的代码甚至都不会编译。
您需要将数据列表包装在系列中,然后将系列添加到图表中。您还应该avoid using raw types。 ClassCastException
和Series
都具有类型参数,但您未指定它们。根据您构建Data
的方式,您的代码应类似于(为简洁起见,省略了一些代码):
Data
并且:
public class PerformanceAnalyzerController {
@FXML private BarChart<String, Number> barChart;
@FXML
private void performAnalysisOperation(ActionEvent event) {
//Performing analysis operation
ObservableList<XYChart.Data<String, Number>> data = handler.getItemsGraphicsStatisitics();
XYChart.Series<String, Number> series = new XYChart.Series<>("Series Name", data);
barchart.getData().setAll(series);
graphHandler.getChildren().add(barchart);
}
}
注意:我将// Use generic types
public ObservableList<XYChart.Data<String, Number>> getItemGraphStatistics(){
ObservableList<XYChart.Data<String, Number>> data = FXCollections.observableArrayList();
String sql = "SELECT itemID, COUNT(*) FROM (SELECT itemID FROM SALES UNION ALL SELECT itemID FROM STOCK) t GROUP BY itemID";
System.out.println(sql );
ResultSet rs = handler.execQuery(sql);
try{
while(rs.next()){
String item= rs.getString("itemID");
int count = rs.getInt(2);
// add diamond operator "<>"
data.add(new XYChart.Data<>(item+ "(" + count + ")", count));
}
} catch (SQLException ex) {
Logger.getLogger(DatabaseHandler.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}
的使用替换为BarChart.Data
,但在这种情况下我认为这并不重要。