无论如何在JavaFX中为所有可视化类型使用一个ObservableList吗?例如,我有一个带有PieChart.Data可观察列表的PieChart,如何在BarChart中使用observablelist中的数据?
修改的
好的,既然不可能,那就让我改一下。
我的应用程序从SQL获取数据,在TableView中查看它,然后使用PieChart可视化TableView中的数据。如何从TableView中获取PieChart和BarChart两者?
这是我的代码:
public class FXMLDocumentController implements Initializable {
String url = "just my db url";
private ObservableList<ObservableList> data;
static ObservableList<PieChart.Data> dataList
= FXCollections.observableArrayList();
private Connection con;
@FXML
private Label label;
@FXML
TableView tableView;
@FXML
private TableColumn<Record, String> Month;
@FXML
private TableColumn<Record, String> Value;
@FXML
private PieChart pieChart;
@FXML
private BarChart barChart;
@FXML
private void handleButtonAction(ActionEvent event) throws SQLException, ClassNotFoundException {
connectToSql();
}
@FXML
private void handleButtonAction2(ActionEvent event) throws ClassNotFoundException {
bindDataToPieChart();
}
@FXML
private void handleButtonAction3(ActionEvent event) throws ClassNotFoundException {
}
@Override
public void initialize(URL url, ResourceBundle rb) {
Month.setCellValueFactory(new PropertyValueFactory("Name"));
Value.setCellValueFactory(new PropertyValueFactory("pieValue"));
tableView.setItems(dataList);
}
private void bindDataToPieChart() {
pieChart.setData(dataList);
}
private static void bindSQLdata(String title, ResultSet rs) throws SQLException {
String test = rs.getString("asd");
System.out.print(test);
Double val = rs.getDouble("val");
dataList.add(new PieChart.Data(test, val));
}
private void connectToSql() throws SQLException {
Connection conn = null;
conn = null;
try {
String dbURL = "jdbc:sqlserver://eskhesse\\sqlexpress;dataBaseName=test;user=sa;password=1234";
conn = DriverManager.getConnection(dbURL);
} catch (SQLException sQLException) {
}
if (conn != null) {
String SQL = "SELECT [asd],[val] FROM [test].[dbo].[test]";
ResultSet rs = conn.createStatement().executeQuery(SQL);
while (rs.next()) {
bindSQLdata("asd", rs);
}
} else {
System.out.print("error");
}
}
}
答案 0 :(得分:1)
BarChart和PieChart不使用相同类型的数据。你可以用绑定来做。
即使他们使用相同的数据,如LineChart和BarChart,它也不会很好,你不能同时显示两者。
import java.util.Iterator;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Scene;
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.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import javafx.util.converter.DoubleStringConverter;
public class ChangePie extends Application {
@Override
public void start(Stage primaryStage) {
//this list type will work for everything except pie chart
ObservableList<XYChart.Data<String, Double>> xyChartData
= FXCollections.observableArrayList(
new XYChart.Data("Grapefruit", 13d),
new XYChart.Data("Oranges", 25d),
new XYChart.Data("Plums", 10d),
new XYChart.Data("Pears", 22d),
new XYChart.Data("Apples", 30d));
//need to make the exact type for pie chart
ObservableList<PieChart.Data> pieChartData = FXCollections.observableArrayList();
//copy the xy data into pie chart data
xyChartData.stream().forEach((xy) -> {
PieChart.Data pcd = new PieChart.Data(xy.getXValue(), xy.getYValue());
pieChartData.add(pcd);
//and then bind the name and value to the xvalue,yvalue
pcd.nameProperty().bind(xy.XValueProperty());
pcd.pieValueProperty().bind(xy.YValueProperty());
});
final PieChart pieChart = new PieChart(pieChartData);
//bar chart needs a category axis
CategoryAxis xAxis = new CategoryAxis();
xyChartData.stream().forEach((xy) -> {
xAxis.getCategories().add(xy.getXValue());
});
//and the data must be added to a series
ObservableList<XYChart.Series<String, Double>> barChartData = FXCollections.observableArrayList(
new XYChart.Series<>(xyChartData));
final BarChart barChart = new BarChart(xAxis, new NumberAxis(), barChartData);
//table view can use the xychart data as long as we type and name it right.
final TableView<XYChart.Data<String, Double>> tv = new TableView<>(xyChartData);
tv.setEditable(true);
TableColumn<XYChart.Data<String, Double>, String> tc1 = new TableColumn<>("Name");
tc1.setPrefWidth(100);
//the name in XYChart.Data is xValueProperty
tc1.setCellValueFactory(new PropertyValueFactory("xValue"));
tc1.setCellFactory(TextFieldTableCell.forTableColumn());
tc1.setOnEditCommit((evt) ->{
evt.getRowValue().setXValue(evt.getNewValue());
});
TableColumn<XYChart.Data<String, Double>, Double> tc2 = new TableColumn<>("Data");
tc2.setPrefWidth(100);
tc2.setCellValueFactory(new PropertyValueFactory("yValue"));
tc2.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
tc2.setOnEditCommit((evt) ->{
evt.getRowValue().setYValue(evt.getNewValue());
});
tv.getColumns().addAll(tc1, tc2);
Button btnLine = new Button("Switch");
FlowPane root = new FlowPane(tv, pieChart, barChart, btnLine);
Scene scene = new Scene(root,800,800);
primaryStage.setScene(scene);
primaryStage.show();
btnLine.setOnAction((evt) ->{
for (Iterator<Node> it = root.getChildren().iterator(); it.hasNext();) {
Node n = it.next();
if(n instanceof LineChart){
it.remove();
root.getChildren().add(new BarChart(xAxis, new NumberAxis(), barChartData));
break;
} else if(n instanceof BarChart){
it.remove();
root.getChildren().add(new LineChart(xAxis, new NumberAxis(), barChartData));
break;
}
}
});
}
public static void main(String[] args) {
launch(args);
}
}
答案 1 :(得分:1)
对于任何想知道我是如何设法解决这个问题的人:
首先,我有一个普通的ObservableList连接到一个名为Record
的类static ObservableList<Record> dataen = FXCollections.observableArrayList();
然后我有两种方法,一种是将数据添加到条形图,另一种是将其添加到饼图中。
private void getBarChartData(){
for (Record record : dataen)
{
series1.getData().add(new XYChart.Data(record.getFieldMonth(),record.getFieldValue()));
}
barChart.getData().addAll(series1);
}
private void getPieChartData(){
for(Record record : dataen)
{
dataList.add(new PieChart.Data(record.getFieldMonth(),record.getFieldValue()));
}
pieChart.setData(dataList);
}