2012-08-31 97 views
1

我的代碼如下圖2垂直劃分的SplitPane中的XY線圖。JavaFX 2.x:X和Y軸設置

我的問題:

我想在底部有兩個Y軸,只有一條X軸(於下圖僅供X軸)

public class XyChartInSplit 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(1, 21,0.1); 

    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 LineChart<String, Number>lineChart1 = new LineChart<String, Number>(xAxis1, yAxis1); 

    lineChart1.setCreateSymbols(false); 
    lineChart1.setAlternativeRowFillVisible(false); 
    lineChart1.setLegendVisible(false); 
    lineChart1.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)); 

    //final BorderPane pane1 = new BorderPane(); 
    //pane1.setCenter(lineChart1);       
    lineChart1.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", 1)); 
    series2.getData().add(new XYChart.Data("Feb", 3)); 
    series2.getData().add(new XYChart.Data("Mar", 1.5)); 
    series2.getData().add(new XYChart.Data("Apr", 3)); 
    series2.getData().add(new XYChart.Data("May", 4.5)); 
    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", 20)); 

    // final BorderPane pane2 = new BorderPane(); 
    //pane2.setCenter(lineChart2);   

    lineChart2.getData().addAll(series2); 

    splitPane1 = new SplitPane();         
    splitPane1.setOrientation(Orientation.VERTICAL); 
    splitPane1.getItems().addAll(lineChart1); 
    splitPane1.setDividerPosition(0, 1); 

    Scene scene = new Scene(splitPane1, 800, 600); 

    Platform.runLater(new Runnable() { 
     @Override 
     public void run() { 
      double percSplit; 

      splitPane1.getItems().addAll(lineChart2); 

      ObservableList<SplitPane.Divider> splitDiv = splitPane1.getDividers(); 

      percSplit = 1/(double)(splitDiv.size()+1); 
      for (int i = 0; i< splitDiv.size(); i++) {       
       splitPane1.setDividerPosition(i, percSplit); 
       percSplit += 1/(double)(splitDiv.size()+1); 
       } 
     } 
    }); 

    stage.setScene(scene); 
    stage.show(); 
} 

public static void main(String[] args) { 
    launch(args); 
} 

}

+0

這裏提到了一個想法:http://stackoverflow.com/questions/9667405/how-to-draw-multiple-axis-on-a-chart-using-javafx-圖表 –

+0

嗨Remko謝謝你的建議 –

回答