当我使用StackPane时,如何在JavaFX 8中对齐两个XYCharts的X轴?当我将第二个Y轴设置为axis.setSide(Side.RIGHT)时出现问题,因此我有两个Y轴,一个在左边,另一个在右边。
以下是我使用的示例代码:
public class StackedPaneXYChartSample extends Application {
final NumberAxis axisPrimary = new NumberAxis();
final NumberAxis axisSecondary = new NumberAxis();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
stage.setScene(
new Scene(
layerCharts(
createPrimaryChart(),
createSecondaryChart()
)
)
);
stage.show();
}
private NumberAxis createYaxis() {
final NumberAxis axis = new NumberAxis(0, 21, 1);
axis.setPrefWidth(35);
axis.setMinorTickCount(10);
axis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(axis) {
@Override
public String toString(Number object) {
return String.format("%7.2f", object.floatValue());
}
});
return axis;
}
private NumberAxis createYaxis2() {
final NumberAxis axis = new NumberAxis(0, 51, 1);
axis.setPrefWidth(35);
axis.setMinorTickCount(10);
axis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(axis) {
@Override
public String toString(Number object) {
return String.format("%7.2f", object.floatValue());
}
});
axis.setSide(Side.RIGHT);
return axis;
}
private LineChart<Number, Number> createPrimaryChart() {
final LineChart<Number, Number> chart = new LineChart<>(axisPrimary, createYaxis());
setDefaultChartProperties(chart);
chart.getData().addAll(
new XYChart.Series(
FXCollections.observableArrayList(
new XYChart.Data(1, 1),
new XYChart.Data(2, 2),
new XYChart.Data(3, 8),
new XYChart.Data(4, 4),
new XYChart.Data(5, 7),
new XYChart.Data(6, 5),
new XYChart.Data(7, 4),
new XYChart.Data(8, 8),
new XYChart.Data(9, 16.5),
new XYChart.Data(10, 13.9),
new XYChart.Data(11, 17),
new XYChart.Data(12, 10)
)
)
);
return chart;
}
private LineChart<Number, Number> createSecondaryChart() {
final LineChart<Number, Number> chart = new LineChart<>(axisSecondary, createYaxis2());
setDefaultChartProperties(chart);
chart.setCreateSymbols(false);
chart.getData().addAll(
new XYChart.Series(
FXCollections.observableArrayList(
new XYChart.Data(1, 1),
new XYChart.Data(2, 2),
new XYChart.Data(3, 1.5),
new XYChart.Data(4, 3),
new XYChart.Data(5, 2.5),
new XYChart.Data(6, 5),
new XYChart.Data(7, 4),
new XYChart.Data(8, 8),
new XYChart.Data(9, 6.5),
new XYChart.Data(10, 13),
new XYChart.Data(11, 10),
new XYChart.Data(12, 20)
)
)
);
return chart;
}
private void setDefaultChartProperties(final XYChart<Number, Number> chart) {
chart.setLegendVisible(false);
chart.setAnimated(false);
}
private StackPane layerCharts(final XYChart<Number, Number>... charts) {
for (int i = 1; i < charts.length; i++) {
configureOverlayChart(charts[i]);
}
StackPane stackpane = new StackPane();
stackpane.getChildren().addAll(charts);
return stackpane;
}
private void configureOverlayChart(final XYChart<Number, Number> chart) {
chart.setAlternativeRowFillVisible(false);
chart.setAlternativeColumnFillVisible(false);
chart.setHorizontalGridLinesVisible(false);
chart.setVerticalGridLinesVisible(false);
chart.getXAxis().setVisible(false);
chart.getYAxis().setVisible(false);
chart.getStylesheets().addAll(getClass().getResource("overlay-chart.css").toExternalForm());
}
}
我试图绑定两个轴但仍然没有解决问题,我想这样做完全错了。请提前帮助,非常感谢!