java - 我怎样才能 "auto-adjust"javafx中的类别轴标签?

标签 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

所以,我的问题是如何让它在不手动执行的情况下自行调整?

这是我的代码的简要 View :

    //  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();
    }

最佳答案

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

关于java - 我怎样才能 "auto-adjust"javafx中的类别轴标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50319724/

相关文章:

java - 如何获得循环中产生的 char 值的总和?

JavaFX - 如何将 FXML 文件注入(inject) Main 的 FXML 按钮?

javascript - 将外部数据 (.tsv) 加载到 d3.js 中

python - 如何在图中表示非常大和非常小的值

java - 同步对象,需要澄清

java - 泛型类的注释参数

java - 仅当使用 fill() 时,处理草图才会抛出 java.lang.AssertionError

java - 让复选框记住其状态的最佳方法是什么?

javascript - JavaFX WebView : can't get JS bridge to work in Java11+

r - 穿过 x 轴的堆叠条形图