问题描述
我的窗口有以下控制器:
I have the following Controller for my window:
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>
除了一件事之外,一切都完美无缺:
Everything works perfectly except one thing:
正如你所看到的 x 轴标签工作正常,但 x 轴标签(我的意思是像列名),应该是 Tom
、Andrew
和 Patrick
被放置在完全相同的位置(通过这个混乱你实际上可以读取 Patrick
)x 轴的 0 位置.哪里有问题?我应该对 UI 或其他东西做一些更改吗?
As you can see the x axis label works fine, but the x axis labels (I mean like column names), which should be Tom
, Andrew
and Patrick
are placed in exact same position (through this mess you can actually read Patrick
) a the 0 position of x axis. Where is the problem? Should I make some changes to the UI or something?
推荐答案
这是一个 错误如果数据是动态设置的,则自动调整 CategoryAxis 的范围.
下面是一个(非 fxml)示例,演示了一个粗略的问题:手动设置类别,而不是让图表/轴找到它们.
Below is a (non-fxml) example that demonstrates the issue with a crude hack-around: set the categories manually instead of let the chart/axis find them.
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());
}
这篇关于JavaFX BarChart xAxis 标签定位错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!