您好我正在尝试使用复选框将数据从JSON过滤到我的javafx图表中我遇到的问题是被粘贴的数据按行而不是列进行分组。所以过滤器过滤每一行意味着我有100个复选框,而我只希望每个行都有一个数据变量。
下面我使用了布尔标志来解决问题,在加载时可以解决问题,但是一旦取消选中复选框,所有数据都会消失。
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import static jdk.nashorn.internal.objects.NativeDate.getYear;
public class HyBridController implements Initializable {
private static String markup;
private static List<Recruits> recruits;
private static List<String> vehicle;
private static List<String> region;
private static List<String> year;
private static List<String> qtr;
private DashService service;
private CheckBox[] vehiclecheckBoxes;
private CheckBox[] regioncheckBoxes;
private CheckBox[] yearcheckBoxes;
private CheckBox[] qtrcheckBoxes;
@FXML
private AnchorPane AnchorPane1;
@FXML
private HBox HBox1;
@FXML
private HBox hbox2;
@FXML
private NumberAxis yAxis;
@FXML
private CategoryAxis xAxis;
@FXML
private BarChart<?, ?> BarChart1;
@Override
public void initialize(URL url, ResourceBundle rb) {
service = new DashService();
service.setAddress("http://glynserver.cms.livjm.ac.uk/DashService/SalesGetSales");
service.setOnSucceeded((WorkerStateEvent e) -> {
markup = e.getSource().getValue().toString();
recruits = (new Gson()).fromJson(markup, new TypeToken<LinkedList<Recruits>>() {
}.getType());
region= recruits.stream().map(o -> o.getRegion()).distinct().collect(Collectors.toList());
vehicle = recruits.stream().map(o -> o.getVehicle()).distinct().collect(Collectors.toList());
qtr = recruits.stream().map(o -> o.getRe()).distinct().collect(Collectors.toList());
year = recruits.stream().map(o -> o.getYear()).distinct().collect(Collectors.toList());
constructCheckBoxes();
});
service.start();
}
private void constructCheckBoxes() {
vehiclecheckBoxes = new CheckBox[vehicle.size()];
regioncheckBoxes = new CheckBox[region.size()];
yearcheckBoxes = new CheckBox[year.size()];
qtrcheckBoxes = new CheckBox[qtr.size()];
for (byte index = 0; index < vehicle.size(); index++) {
vehiclecheckBoxes[index] = new CheckBox(vehicle.get(index));
vehiclecheckBoxes[index].setSelected(true);
vehiclecheckBoxes[index].addEventFilter(ActionEvent.ACTION, (ActionEvent e) -> {
constructSeries();
});
HBox1.getChildren().add(vehiclecheckBoxes[index]);
}
for (byte index = 0; index < region.size(); index++) {
regioncheckBoxes[index] = new CheckBox(region.get(index));
regioncheckBoxes[index].setSelected(true);
regioncheckBoxes[index].addEventFilter(ActionEvent.ACTION, (ActionEvent e) -> {
constructSeries();
});
HBox1.getChildren().add(regioncheckBoxes[index]);
}
for (byte index = 0; index < year.size(); index++) {
yearcheckBoxes[index] = new CheckBox(year.get(index));
yearcheckBoxes[index].setSelected(true);
yearcheckBoxes[index].addEventFilter(ActionEvent.ACTION, (ActionEvent e) -> {
constructSeries();
});
HBox1.getChildren().add(yearcheckBoxes[index]);
}
for (byte index = 0; index < qtr.size(); index++) {
qtrcheckBoxes[index] = new CheckBox(qtr.get(index));
qtrcheckBoxes[index].setSelected(true);
qtrcheckBoxes[index].addEventFilter(ActionEvent.ACTION, (ActionEvent e) -> {
constructSeries();
});
HBox1.getChildren().add(qtrcheckBoxes[index]);
}
AnchorPane1.getScene().getWindow().sizeToScene();
constructSeries();
}
private void constructSeries() {
BarChart1.getData().clear();
for (Recruits recruit : recruits){
boolean flag = false;
XYChart.Series series = new XYChart.Series();
for (CheckBox cb : regioncheckBoxes){
if (!cb.isSelected()){
series.setName(cb.getText());
flag = true;
}
}
for (CheckBox cb : vehiclecheckBoxes){
if (!cb.isSelected()){
flag = true;
}
}
for (CheckBox cb : yearcheckBoxes){
if (!cb.isSelected()){
flag = true;
}
}
for (CheckBox cb : qtrcheckBoxes){
if (!cb.isSelected()){
flag = true;
}
}
if (!flag){
series.getData().add(new XYChart.Data<>(recruit.getVehicle(), recruit.getQuantity()));
BarChart1.getData().add(series);
}
}
}
private static class DashService extends Service<String> {
private StringProperty address = new SimpleStringProperty();
public final void setAddress(String address) {
this.address.set(address);
}
public final String getAddress() {
return address.get();
}
public final StringProperty addressProperty() {
return address;
}
@Override
protected Task<String> createTask() {
return new Task<String>() {
private URL url;
private HttpURLConnection connect;
private String markup = "";
@Override
protected String call() {
try {
url = new URL(getAddress());
connect = (HttpURLConnection) url.openConnection();
connect.setRequestMethod("GET");
connect.setRequestProperty("Accept", "application/json");
connect.setRequestProperty("Content-Type", "application/json");
markup = (new BufferedReader(new InputStreamReader(connect.getInputStream()))).readLine();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connect != null) {
connect.disconnect();
}
}
return markup;
}
};
}
}
}