JavaFX BarChart xAxis标记错误的定位 [英] JavaFX BarChart xAxis labels bad positioning
问题描述
我的窗口有以下控制器:
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标记错误的定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!