Reputation: 189
Suppose I have a simple ScatterChart example, as posted here
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
public class AutoAlignment extends Application {
private void init(Stage primaryStage) {
Group root = new Group();
primaryStage.setScene(new Scene(root));
CategoryAxis xAxis = new CategoryAxis();
NumberAxis yAxis = new NumberAxis(58.7,59.35,0.1);
xAxis.setTickLabelRotation( 360 );
xAxis.setLabel( "Time" );
yAxis.setLabel( "Price" );
Series test = new ScatterChart.Series("Series 1", FXCollections.<ScatterChart.Data>observableArrayList());
test.getData().addAll( new XYChart.Data("11:44",59.23),
new XYChart.Data("11:45",59.28),
new XYChart.Data("11:46",59.23),
new XYChart.Data("11:47",59.2),
new XYChart.Data("11:48",59.14),
new XYChart.Data("11:49",59.15),
new XYChart.Data("11:50",59.19),
new XYChart.Data("11:51",59.15),
new XYChart.Data("11:52",59.13),
new XYChart.Data("11:53",59.2),
new XYChart.Data("11:54",59.15),
new XYChart.Data("11:55",59.16));
ObservableList<XYChart.Series> data = FXCollections.observableArrayList(test);
ScatterChart chart = new ScatterChart(xAxis, yAxis, data);
root.getChildren().add(chart);
}
@Override public void start(Stage primaryStage) throws Exception {
init(primaryStage);
primaryStage.show();
}
public static void main(String[] args) { launch(args); }
}
Using a small yAxis range, the graph shrinks and produces a large white space gap between the yAxis label and the actual chart.
If I remove the yAxis parameters and just do
NumberAxis yAxis = new NumberAxis();
then the graph comes out normal. What causes the graph to shrink this way, and how can I prevent this? I want the Axis to be customized, but also not shrunk like the first case. Thank you.
Upvotes: 0
Views: 212
Reputation: 209358
It looks like the axis is leaving lots of space for labels that might include a large number of decimal places.
Adding a tickLabelFormatter
seems to fix the problem:
yAxis.setTickLabelFormatter(new StringConverter<Number>() {
@Override
public String toString(Number object) {
return String.format("%.1f", object.doubleValue());
}
@Override
public Number fromString(String string) {
return new Double(string);
}
});
Upvotes: 1