JavaFX中BarChart的奇怪行为

时间:2018-05-31 00:29:17

标签: java javafx charts collections observable

我有一个JavaFX BarChart,我根据组合框选择的项目更改图表中的值。完整代码在

之下
import javafx.scene.chart.XYChart;
import javafx.scene.control.ComboBox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class TestController {

    @FXML
    private BarChart testChart;

    @FXML
    private ComboBox cmb1;

    @FXML
    protected void initialize() {
        Random random = new Random();
        Map<String, List<XYChart.Series<String, Float>>> costMap = new HashMap<>();
        for (int i = 0; i < 10; i++) {
            List<XYChart.Series<String, Float>> lstCost = new ArrayList<>(2);
            XYChart.Series series = new XYChart.Series();
            series.setName("Cost");
            series.getData().add(new XYChart.Data("Cost", random.nextFloat()));
            XYChart.Series series2 = new XYChart.Series();
            series2.setName("Bill");
            series2.getData().add(new XYChart.Data("Bill", random.nextFloat()));
            lstCost.add(series);
            lstCost.add(series2);
            costMap.put(i + "", lstCost);
        }

        cmb1.getItems().addAll(costMap.keySet());

        cmb1.valueProperty().addListener((observable, oldValue, newValue) -> {
            if (oldValue != null) {
                testChart.getData().clear();
            }
            testChart.getData().addAll(costMap.get(newValue));
        });
    }
}

FXML很简单如下(test.fxml)

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.chart.BarChart?>
<?import javafx.scene.chart.CategoryAxis?>
<?import javafx.scene.chart.NumberAxis?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.121"
            xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.TestController">
    <children>
        <BarChart fx:id="testChart" layoutX="14.0" layoutY="46.0" prefHeight="340.0" prefWidth="572.0"
                  AnchorPane.bottomAnchor="14.0" AnchorPane.leftAnchor="14.0" AnchorPane.rightAnchor="14.0"
                  AnchorPane.topAnchor="46.0">
            <xAxis>
                <CategoryAxis side="BOTTOM"/>
            </xAxis>
            <yAxis>
                <NumberAxis side="LEFT"/>
            </yAxis>
        </BarChart>
        <ComboBox fx:id="cmb1" layoutX="14.0" layoutY="14.0" prefWidth="150.0"/>
    </children>
</AnchorPane>

奇怪的是,当图表正确反映值时,我会浏览所有项目。但是,如果我再次选择前一个值,则值会完全改变。 我无法弄清楚这种奇怪行为的原因是什么。有人能告诉我这里有什么问题吗?

据我了解testChart.getData().clear()操纵原始数据集。但这不应该发生。

被修改

如果有人想在这里尝试是主要的课程

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        AnchorPane root = FXMLLoader.load(getClass().getResource("test.fxml"));
        Scene scene = new Scene(root, 1024, 768);
        stage.setScene(scene);
        stage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

0 个答案:

没有答案