【问题标题】:JavaFX BarChart xAxis labels bad positioningJavaFX BarChart xAxis 标签定位错误
【发布时间】:2018-08-06 07:23:39
【问题描述】:

我的窗口有以下控制器:

package window;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;

import java.util.Map;
import java.util.SortedMap;

public class StatisticsController {

    @FXML
    private BarChart<?, ?> barChartHistogram;

    private SortedMap<String, Integer> _points;

    @FXML
    private CategoryAxis xAxis;
    @FXML
    private NumberAxis yAxis;

    public void onLoad(SortedMap<String, Integer> points) {
        xAxis.setLabel("Numer indeksu");
        yAxis.setLabel("Ilość punktów");
        //barChartHistogram.setBarGap(0);
        XYChart.Series series1 = new XYChart.Series();
        int a = 10;
        series1.getData().add(new XYChart.Data("Tom", 10));
        series1.getData().add(new XYChart.Data("Andrew", 7));
        series1.getData().add(new XYChart.Data("Patrick", 5));


        /*for (Map.Entry<String, Integer> p: points.entrySet()) {
            series1.getData().add(new XYChart.Data<>(Integer.toString(a), p.getValue()));
            a += 10;
        }*/
        barChartHistogram.getData().addAll(series1);
        _points = points;
    }

}

此窗口的.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.layout.AnchorPane?>

<AnchorPane prefHeight="700.0" prefWidth="1000.0" xmlns="http://javafx.com/javafx/9.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="window.StatisticsController">
   <children>
      <BarChart fx:id="barChartHistogram" layoutX="12.0" layoutY="14.0" prefHeight="372.0" prefWidth="1500.0">
        <xAxis>
          <CategoryAxis side="BOTTOM" fx:id="xAxis" />
        </xAxis>
        <yAxis>
          <NumberAxis fx:id="yAxis" side="LEFT" />
        </yAxis>
      </BarChart>
   </children>
</AnchorPane>

除了一件事,一切都完美无缺:

您可以看到 x 轴标签工作正常,但 x 轴标签(我的意思是列名)应该是 TomAndrewPatrick 放置在完全相同的位置(通过这个乱七八糟,你实际上可以阅读Patrick) x 轴的 0 位置。问题出在哪里?我应该对 UI 进行一些更改吗?

【问题讨论】:

  • 我运行了你的代码,没有问题。
  • 好的。那么它可以在哪里呢?它可以使用 Java 9,而不是 8 吗?
  • 我使用Java 8
  • 好吧,Java 8 没用,检查一下
  • 偶然发现了非常相似的问题。经过长时间的调试并尝试了几种解决方法后,我不得不承认这个问题确实无法修复,并且源于直方图类中的错误。他们怎么会错过这么明显的事情。

标签: java javafx bar-chart


【解决方案1】:

我为此找到了另一种解决方法。只需设置xAxis.setAnimated(false)

【讨论】:

  • 这是更好的解决方法。手动设置很容易出错。谢谢。
【解决方案2】:

如果数据是动态设置的,它是 CategoryAxis 的bug with auto-range

下面是一个(非 fxml)示例,该示例演示了一个粗略的破解问题:手动设置类别而不是让图表/轴找到它们。

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.setTitle(FXUtils.version());
        stage.show();
    }

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

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

}

【讨论】:

  • 你说的完全正确,我自己也明白了。还是谢谢!
【解决方案3】:

这是一个与 bug 相关的动画属性,您可以:

  • 设置xAxis.setAnimated(false)
  • 设置动画属性为假:&lt;CategoryAxis animated="false" side="BOTTOM" fx:id="xAxis" /&gt;

【讨论】:

    【解决方案4】:

    这很有趣,但我遇到了 java11 https://youtu.be/2ZKzWciKtIc?t=36 的同样问题,在我的图表数据加载后,类别标签永远不会显示。经过几次尝试,我决定在时间轴中设置图表数据,并在完成后手动设置类别标签。

    Duration duration = Duration.millis(300);
    Platform.runLater(()->{
        Timeline timeline = new Timeline(
            new KeyFrame(duration,event -> {
                barChart.getData().clear();
                barChart.getData().add(series1);
                barChart.getData().addAll(series2);
                //Clear all bofore to evitate Exceptions
                catAxis.getCategories().clear();                           
            })
        );
        timeline.setCycleCount(1);
        timeline.setOnFinished(event -> {
        //Set animated as false, so if true all labels will be one in top of the others 
        barChart.setAnimated(false);
        //The trick that worked forr me
        catAxis.getCategories().addAll(lists);
    });
    timeline.play();  });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多