假设我有一个简单的ScatterChart示例,如此处所述
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); }
}
使用较小的yAxis范围,图形会缩小并在yAxis标签和实际图表之间产生较大的空白间隙。
如果我删除了yAxis参数,只需执行
NumberAxis yAxis = new NumberAxis();
然后图表正常。是什么导致图形缩小这种方式,我该如何防止这种情况?我希望Axis可以定制,但也不像第一种情况那样缩小。谢谢。
答案 0 :(得分:1)
看起来轴为可能包含大量小数位的标签留下了大量空间。
添加tickLabelFormatter
似乎可以解决问题:
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);
}
});