如何在javafx中“自动调整”类别轴标签?

时间:2018-05-13 19:08:09

标签: java javafx bar-chart

我正在用JavaFX编写一个小直方图。我使用BarChart作为节点,并将XYChart系列添加到BarChart。在我发现这个问题之前,我的代码工作得非常好:

Picture of category labels grouped at the origin

标签相互重叠。

然后当我手动调整窗口大小时:

Picture of category labels correctly positioned after resizing window

所以,我的问题是如何在不手动操作的情况下调整自身?

以下是我的代码的简要视图:

    //  Make an axis for x and one for y. Then, create a chart for the histogram by using x and y axis.
    final CategoryAxis xAxis = new CategoryAxis();
    final NumberAxis yAxis = new NumberAxis();
    final BarChart<String,Number>histogramChart = new BarChart<String,Number>(xAxis,yAxis);
    BorderPane myBorderPane = new BorderPane();   //  Use a border pane for the histogram and the button. 
    Button btEnter = new Button("ENTER");
    TextField tfDirectory = new TextField();
    Scanner input;
    XYChart.Series seriesOfAlphabet = new XYChart.Series();
    HBox hbxDirectoryAndButton = new HBox();
    //  A final string array for the arphabet
    final static String[] alphabets= {"A","B","C","D","E","F"
                                     ,"G","H","I","J","K","L"
                                     ,"M","N","O","P","Q","R"
                                     ,"S","T","U","V","W","X"
                                     ,"Y","Z"};

    //  Main to launch the application
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        //  Set the title of the stage
        primaryStage.setTitle("Histogram Of Occurences of Letters by Luckas(YingHong Huang");
        //  Set the title of the chart
        histogramChart.setTitle("Histogram");
        xAxis.setLabel("Alphets");          // Set label of the xAxis
        yAxis.setLabel("Occurences");       // Set the label of yAxis
        tfDirectory.setLayoutY(20);         //  Set the height of the text field for the file input
        tfDirectory.setLayoutX(100);        //  Set the length of the text field for the file input
        seriesOfAlphabet.setName("Occurences");
        btEnter.setOnAction(e->{
            openFile();
        });    
        histogramChart.getData().add(seriesOfAlphabet);
        hbxDirectoryAndButton.getChildren().addAll(tfDirectory,btEnter);     
        hbxDirectoryAndButton.setSpacing(10);
        myBorderPane.setCenter(histogramChart);
        myBorderPane.setBottom(hbxDirectoryAndButton);
        myBorderPane.setPadding(new Insets(5,5,5,5));
        Scene histogramScene = new Scene(myBorderPane,1500,700);
        primaryStage.setScene(histogramScene);
        primaryStage.show();
    }

1 个答案:

答案 0 :(得分:0)

我今天遇到了同样的问题。我使用了Javafx Scene Builder,并在“属性”中未选择“动画”解决了该问题。希望对您有所帮助。