JavaFX chart - CategoryAixis resize issue

I have a user modifiable chart. When the chart gets smaller, CategoryAxis rotates and you can no longer see most category labels. Here is the gif showing the problem:

Problem example

Is there a way to stop the rotation of the labels?

I know that I can add a listener to the rotation property and rotate it back to 0 if the rotation changes. However, when I do this, it does not interfere with the adjustment of the intervals, so the labels are simply truncated (you see only the last few characters of the label).

Here is the code for the included gif, you will see a problem when resizing the window:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

public class HorizontalBarExample extends Application {
    @Override
    public void start(Stage stage)  {
        NumberAxis xAxis = new NumberAxis();
        CategoryAxis yAxis = new CategoryAxis();
        BarChart<Number, String> bc = new BarChart<Number, String>(xAxis, yAxis);
        bc.setBarGap(0d);
        bc.setCategoryGap(0);

        xAxis.setTickLabelRotation(90);
        yAxis.tickLabelRotationProperty().set(0d);

        XYChart.Series<Number, String> series1 = new XYChart.Series<>();
        series1.setName("example");

        for (int i = 0; i < 10; i++)
            series1.getData().add(new XYChart.Data<Number, String>(Math.random() * 5000, "long data label number" + i));

        bc.getData().add(series1);

        Scene scene = new Scene(bc, 800, 600);
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args)  {
        launch(args);
    }
}
+4
source share
1 answer

(IMO).

CategoryAxis Java 8u60 effectiveTickLabelRotation. - , tickLabelRotationProperty. , .

() API

, , - autoRanging, autoRanging false, CategoryAxis::setCategory. , Rotation , , , .

, , . CategoryAxis , , , . CategoryAxis , . ​​ , yAxis.setMinWidth(200);. , CategoryAxis .

, ... :

  • , OR
  • , , OR
  • CategoryAxis , . HorizontallyLabelledCategoryAxis, , CategoryAxis.

3 , .

, , (, ), , , , .

VBox chartHolder = new VBox(bc);
VBox.setVgrow(bc, Priority.ALWAYS);
bc.setMinHeight(300);
Scene scene = new Scene(chartHolder, 800, 600);
+4

Source: https://habr.com/ru/post/1610495/


All Articles