有了这个建议,
https://forums.oracle.com/forums/thread.jspa?threadID=2435573&tstart=0
我尝试使用 StackPane 但第二个图表替换了第一个。
如何在同一张图中绘制两个或多个不同的图表(如我的示例)?
这是我的代码
public class XyChartInSplitStackpane extends Application {
SplitPane splitPane1 = null;
@Override
public void start(Stage stage) {
stage.setTitle("Line plot");
final CategoryAxis xAxis1 = new CategoryAxis();
final NumberAxis yAxis1 = new NumberAxis();
yAxis1.setTickUnit(1);
yAxis1.setPrefWidth(35);
yAxis1.setMinorTickCount(10);
yAxis1.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis1){
@Override
public String toString(Number object){
String label;
label = String.format("%7.2f", object.floatValue());
return label;
}
});
final BarChart<String, Number>barChart1 = new BarChart<String, Number>(xAxis1, yAxis1);
barChart1.setAlternativeRowFillVisible(false);
barChart1.setLegendVisible(false);
barChart1.setAnimated(false);
XYChart.Series series1 = new XYChart.Series();
series1.getData().add(new XYChart.Data("Jan", 1));
series1.getData().add(new XYChart.Data("Feb", 2));
series1.getData().add(new XYChart.Data("Mar", 1.5));
series1.getData().add(new XYChart.Data("Apr", 3));
series1.getData().add(new XYChart.Data("May", 2.5));
series1.getData().add(new XYChart.Data("Jun", 5));
series1.getData().add(new XYChart.Data("Jul", 4));
series1.getData().add(new XYChart.Data("Aug", 8));
series1.getData().add(new XYChart.Data("Sep", 6.5));
series1.getData().add(new XYChart.Data("Oct", 13));
series1.getData().add(new XYChart.Data("Nov", 10));
series1.getData().add(new XYChart.Data("Dec", 20));
barChart1.getData().addAll(series1);
final CategoryAxis xAxis2 = new CategoryAxis();
final NumberAxis yAxis2 = new NumberAxis(1, 21,0.1);
yAxis2.setTickUnit(1);
yAxis2.setPrefWidth(35);
yAxis2.setMinorTickCount(10);
yAxis2.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis2){
@Override
public String toString(Number object){
String label;
label = String.format("%7.2f", object.floatValue());
return label;
}
});
final LineChart<String, Number>lineChart2 = new LineChart<String, Number>(xAxis2, yAxis2);
lineChart2.setCreateSymbols(false);
lineChart2.setAlternativeRowFillVisible(false);
lineChart2.setLegendVisible(false);
lineChart2.setAnimated(false);
XYChart.Series series2 = new XYChart.Series();
series2.getData().add(new XYChart.Data("Jan", 2));
series2.getData().add(new XYChart.Data("Feb", 10));
series2.getData().add(new XYChart.Data("Mar", 8));
series2.getData().add(new XYChart.Data("Apr", 4));
series2.getData().add(new XYChart.Data("May", 7));
series2.getData().add(new XYChart.Data("Jun", 5));
series2.getData().add(new XYChart.Data("Jul", 4));
series2.getData().add(new XYChart.Data("Aug", 8));
series2.getData().add(new XYChart.Data("Sep", 16.5));
series2.getData().add(new XYChart.Data("Oct", 13.9));
series2.getData().add(new XYChart.Data("Nov", 17));
series2.getData().add(new XYChart.Data("Dec", 10));
Set<Node> nodes1 = barChart1.lookupAll(".series0");
for (Node n: nodes1) {
n.setStyle("-fx-blend-mode: src-over");
}
lineChart2.getData().addAll(series2);
Set<Node> nodes2 = lineChart2.lookupAll(".series0");
for (Node n: nodes2) {
n.setStyle("-fx-stroke: red; -fx-background-color: red, white; -fx-blend-mode: src-over; -fx-opacity: 0.5");
}
splitPane1 = new SplitPane();
StackPane stackpane = new StackPane();
stackpane.getChildren().add(barChart1);
stackpane.getChildren().add(lineChart2);
splitPane1.setOrientation(Orientation.VERTICAL);
splitPane1.getItems().addAll(stackpane);
splitPane1.setDividerPosition(0, 1);
Scene scene = new Scene(splitPane1, 800, 600);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}