Charts javafx2.x:使用StackPane对更多不同类型的图表进行分层

Charts javafx2.x:使用StackPane对更多不同类型的图表进行分层,charts,javafx,Charts,Javafx,有了这个建议, 我曾尝试使用StackPane,但第二张图表取代了第一张 如何在同一个图表中绘制两个或多个不同的图表,如我的示例所示 这是我的密码 public class XyChartInSplitStackpane extends Application { SplitPane splitPane1 = null; @Override public void start(Stage stage) { stage.setTitle("Line plot"); f

有了这个建议,

我曾尝试使用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);
}
}

据我所知,你将这个系列添加到了两个不同的图表中

即。 lineChart2.getData.addAllseries2;&barChart1.getData.addAllseries1

但是,如果这是有意为其中一个图表添加其他数据,则只需使用:barChart1.getData.addAllseries1,series2