Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8198830

BarChart: auto-range of CategoryAxis not working on dynamically setting data

XMLWordPrintable

    • b25

      To reproduce, run the example below and press init button to fill the barChart with data

      - expected: categoryAxis auto-range, that is labels appear centered below the bars
      - actual: categoryAxis auto-range not working, labels appear stacked one above the other at the leading corner of the axis

      hack-around: don't rely on auto-range, instead set the categories manually (in the example below, uncomment the category config) - which is working but not acceptable in production environments .. see also https://stackoverflow.com/q/48995257/203657. Note that StackedBarChart might have a similar issue (didn't try) as both used to be off-sync in earlier versions (see fixed JDK-8094710)

      The example:

      public class BarChartBug extends Application {

          private NumberAxis yAxis;
          private CategoryAxis xAxis;
          private BarChart<String, Number> barChart;

          private Parent createContent() {
              xAxis = new CategoryAxis();
              yAxis = new NumberAxis();
              barChart = new BarChart<>(xAxis, yAxis);

              Button initData = new Button("init");
              initData.setOnAction(e -> {
                  xAxis.setLabel("Numer indeksu");
                  yAxis.setLabel("Ilo punktw");
                  XYChart.Series<String, Number> series1 = new XYChart.Series<>();
                  series1.getData().add(new XYChart.Data<String, Number>("Tom", 10));
                  series1.getData().add(new XYChart.Data<String, Number>("Andrew", 7));
                  series1.getData().add(new XYChart.Data<String, Number>("Patrick", 5));

                  // hack-around:
                  // xAxis.setCategories(FXCollections.observableArrayList("Tom", "Andrew", "Patrick"));
                  barChart.getData().addAll(series1);

                  initData.setDisable(true);

              });
              BorderPane pane = new BorderPane(barChart);
              pane.setBottom(initData);
              return pane;

          }

          @Override
          public void start(Stage stage) throws Exception {
              stage.setScene(new Scene(createContent()));
              stage.show();
          }

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

          @SuppressWarnings("unused")
          private static final Logger LOG = Logger
                  .getLogger(BarChartBug.class.getName());

      }

            mmack Markus Mack
            fastegal Jeanette Winzenburg
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: