Reputation: 2227
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;
}
}
The .fxml
file for this window:
<?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:
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?
Upvotes: 3
Views: 3448
Reputation: 185
It's a bug relevant animated property, you can:
xAxis.setAnimated(false)
or<CategoryAxis animated="false" side="BOTTOM" fx:id="xAxis" />
Upvotes: 1
Reputation: 1
This is fun , but i was facing the same problem with java11 https://youtu.be/2ZKzWciKtIc?t=36 , after my chart data was loaded the category labels never show. After few tries i decided to set the chart data in a timeline and after it finish set the category labels manually.
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(); });
Upvotes: 0
Reputation: 91
I found another work around for this. Simply set xAxis.setAnimated(false)
Upvotes: 9
Reputation: 51525
It's a bug with auto-range of CategoryAxis if the data is set dynamically.
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());
}
Upvotes: 6