2012-09-05 37 views
1

有了這個建議,JavaFX的2.X:使用StackPane到層更多種不同類型的圖表

https://forums.oracle.com/forums/thread.jspa?threadID=2435573&tstart=0

我已經tryed使用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); 
} 
} 

回答

0

至於我能看到你的加入series到兩個不同的圖表

即。 lineChart2.getData().addAll(series2); & barChart1.getData().addAll(series1);

但是,如果這是故意的,所有你需要做的,添加其他數據,圖表的一個是簡單地使用:barChart1.getData().addAll(series1, series2);

相關問題