2012-09-24 53 views
2

此代碼在下窗格中繪製LineChart上方窗格和條形圖。JavaFX 2.x:在子窗格上擴展垂直線

還有一個垂直線繪製左/右X移動指針。

我想也擴展這個垂直線到下subpane,如附加的圖片

enter image description here

如何做到這一點?

謝謝。

下面是代碼

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.collections.ObservableList; 
import javafx.event.EventHandler; 
import javafx.geometry.Orientation; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.chart.*; 
import javafx.scene.control.SplitPane; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Line; 
import javafx.scene.shape.LineBuilder; 
import javafx.stage.Stage; 

public class XyChartInSplitOnlyCursor extends Application { 
SplitPane    splitPane1 = null; 
BorderPane    pane1; 
BorderPane    pane2; 
Line     LV; 
XYChart.Series   series1 = new XYChart.Series(); 
XYChart.Series   series2 = new XYChart.Series(); 

@Override 
public void start(Stage stage) {  
stage.setTitle("Lines plot"); 

final NumberAxis xAxis = new NumberAxis(1, 12, 1); 
final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); 

xAxis.setAnimated(false); 
xAxis.setScaleX(0); 
yAxis.setAnimated(false); 

yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { 

    @Override 
    public String toString(Number object) { 
     return String.format("%7.5f", object); 
    } 
}); 

final LineChart<Number, Number> lineChart1 = new LineChart<Number, Number>(xAxis, yAxis); 

lineChart1.setCreateSymbols(false); 
lineChart1.setAlternativeRowFillVisible(false); 
lineChart1.setAnimated(false); 
lineChart1.setLegendVisible(false); 

series1.getData().add(new XYChart.Data(1, 0.53185)); 
series1.getData().add(new XYChart.Data(2, 0.532235)); 
series1.getData().add(new XYChart.Data(3, 0.53234)); 
series1.getData().add(new XYChart.Data(4, 0.538765)); 
series1.getData().add(new XYChart.Data(5, 0.53442)); 
series1.getData().add(new XYChart.Data(6, 0.534658)); 
series1.getData().add(new XYChart.Data(7, 0.53023)); 
series1.getData().add(new XYChart.Data(8, 0.53001)); 
series1.getData().add(new XYChart.Data(9, 0.53589)); 
series1.getData().add(new XYChart.Data(10, 0.53476)); 
series1.getData().add(new XYChart.Data(11, 0.530123)); 
series1.getData().add(new XYChart.Data(12, 0.531035)); 

lineChart1.getData().addAll(series1);   

pane1 = new BorderPane(); 
pane1.setCenter(lineChart1); 

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

final CategoryAxis xAxis2 = new CategoryAxis(); 
final NumberAxis yAxis2 = new NumberAxis(); 

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 BarChart<String, Number>BarChart = new BarChart<String, Number>(xAxis2, yAxis2); 

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

BarChart.getData().addAll(series2); 

pane2 = new BorderPane(); 
pane2.setCenter(BarChart); 

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

     splitPane1.getItems().addAll(pane2); 

     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); 
      } 
    } 
}); 

Scene scene = new Scene(splitPane1, 800, 600); 
stage.setScene(scene);   

pane1.setOnMouseMoved(mouseHandler); 

LV=LineBuilder.create() 
     .startX(0) 
     .startY(0) 
     .endX(10) 
     .endY(.535) 
     .strokeWidth(1) 
     .stroke(Color.BLACK) 
     .build(); 
pane1.getChildren().add(LV); 

stage.show(); 
} 
EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { 

@Override 
public void handle(MouseEvent mouseEvent) { 
    if (mouseEvent.getEventType() == MouseEvent.MOUSE_MOVED) { 
     LineChart<Number, Number> lineChart = (LineChart<Number, Number>) pane1.getCenter(); 

     NumberAxis yAxis = (NumberAxis) lineChart.getYAxis(); 
     NumberAxis xAxis = (NumberAxis) lineChart.getXAxis(); 

     double newXlower=xAxis.getLowerBound(), newXupper=xAxis.getUpperBound(); 

     double xAxisShift = getSceneShift(xAxis); 
     double yAxisShift = getSceneShift(yAxis); 

     if(mouseEvent.getX()>xAxisShift && mouseEvent.getX()<xAxisShift+xAxis.getWidth()){ 

     LV.setStartX(mouseEvent.getX()); 
     LV.setStartY(yAxisShift); 
     LV.setEndX(mouseEvent.getX()); 
     LV.setEndY(yAxisShift+yAxis.getHeight()); 
     } 
    } 
} 
}; 
private static double getSceneShift(Node node) { 
double shift = 0; 
do { 
    shift += node.getLayoutX(); 
    node = node.getParent(); 
} while (node != null); 
return shift; 
} 
public static void main(String[] args) { 
launch(args); 
} 
} 

編輯:jewelsea後添加圖片修改

enter image description here

回答

3

不要添加一行到在將splitPane窗格,而是創建一組層通過SplitPane的線。

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

如果您想要的場景調整大小,您可以使用StackPane而不是一個集團和綁定線長度現場高度得到它填補了現場。

下面是使用StackPane和綁定來應用可調整大小的場景中的行的示例。請注意,與最初發布的代碼類似,代碼被明確編碼爲只在鼠標位於頂部圖表的繪圖區域內時移動該行。

import javafx.application.Application; 
import javafx.application.Platform; 
import javafx.collections.ObservableList; 
import javafx.event.EventHandler; 
import javafx.geometry.Orientation; 
import javafx.scene.*; 
import javafx.scene.chart.*; 
import javafx.scene.control.SplitPane; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.*; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Line; 
import javafx.scene.shape.LineBuilder; 
import javafx.stage.Stage; 

public class XyChartInSplitOnlyCursor extends Application { 
    SplitPane splitPane1 = null; 
    BorderPane pane1; 
    BorderPane pane2; 
    Line LV1, LV2; 
    XYChart.Series series1 = new XYChart.Series(); 
    XYChart.Series series2 = new XYChart.Series(); 

    @Override 
    public void start(Stage stage) { 
    stage.setTitle("Lines plot"); 

    final NumberAxis xAxis = new NumberAxis(1, 12, 1); 
    final NumberAxis yAxis = new NumberAxis(0.53000, 0.53910, 0.0005); 

    xAxis.setAnimated(false); 
    xAxis.setScaleX(0); 
    yAxis.setAnimated(false); 

    yAxis.setTickLabelFormatter(new NumberAxis.DefaultFormatter(yAxis) { 
     @Override 
     public String toString(Number object) { 
     return String.format("%7.5f", object); 
     } 
    }); 

    final LineChart<Number, Number> lineChart1 = new LineChart<Number, Number>(xAxis, yAxis); 

    lineChart1.setCreateSymbols(false); 
    lineChart1.setAlternativeRowFillVisible(false); 
    lineChart1.setAnimated(false); 
    lineChart1.setLegendVisible(false); 

    series1.getData().add(new XYChart.Data(1, 0.53185)); 
    series1.getData().add(new XYChart.Data(2, 0.532235)); 
    series1.getData().add(new XYChart.Data(3, 0.53234)); 
    series1.getData().add(new XYChart.Data(4, 0.538765)); 
    series1.getData().add(new XYChart.Data(5, 0.53442)); 
    series1.getData().add(new XYChart.Data(6, 0.534658)); 
    series1.getData().add(new XYChart.Data(7, 0.53023)); 
    series1.getData().add(new XYChart.Data(8, 0.53001)); 
    series1.getData().add(new XYChart.Data(9, 0.53589)); 
    series1.getData().add(new XYChart.Data(10, 0.53476)); 
    series1.getData().add(new XYChart.Data(11, 0.530123)); 
    series1.getData().add(new XYChart.Data(12, 0.531035)); 

    lineChart1.getData().addAll(series1); 

    pane1 = new BorderPane(); 
    pane1.setCenter(lineChart1); 

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

    final CategoryAxis xAxis2 = new CategoryAxis(); 
    final NumberAxis yAxis2 = new NumberAxis(); 

    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 BarChart<String, Number> BarChart = new BarChart<String, Number>(xAxis2, yAxis2); 

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

    BarChart.getData().addAll(series2); 

    pane2 = new BorderPane(); 
    pane2.setCenter(BarChart); 

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

     splitPane1.getItems().addAll(pane2); 

     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); 
     } 
     } 
    }); 

    LV1 = LineBuilder.create() 
      .strokeWidth(2) 
      .stroke(Color.FORESTGREEN) 
      .build(); 

    StackPane stack = new StackPane(); 
    Pane glassPane = new Pane(); 
    glassPane.getChildren().add(LV1); 
    glassPane.minWidthProperty().bind(splitPane1.widthProperty()); 
    glassPane.minHeightProperty().bind(splitPane1.heightProperty()); 
    glassPane.setMouseTransparent(true); 
    stack.getChildren().addAll(splitPane1, glassPane); 
    Scene scene = new Scene(stack, 800, 600); 
    LV1.endYProperty().bind(scene.heightProperty()); 
    stage.setScene(scene); 
    pane1.setOnMouseMoved(mouseHandler); 

    stage.show(); 
    } 

    EventHandler<MouseEvent> mouseHandler = new EventHandler<MouseEvent>() { 
    @Override public void handle(MouseEvent mouseEvent) { 
     XYChart<Number, Number> chart1 = (XYChart<Number, Number>) pane1.getCenter(); 
     plotLine(chart1, LV1, mouseEvent.getX() + 1); 
    } 
    }; 

    private void plotLine(XYChart<Number, Number> chart, Line line, double x) { 
    Axis xAxis = chart.getXAxis(), yAxis = chart.getYAxis(); 
    final double min = getSceneShift(xAxis); 
    final double max = min + xAxis.getWidth(); 
    boolean setCrosshair = false; 
    if (x > min && x < min + xAxis.getWidth()) { 
     LV1.setStartX(x); LV1.setEndX(x); 
     setCrosshair = true; 
    } else if (x <= min){ 
     LV1.setStartX(min); LV1.setEndX(min); 
    } else if (x >= max){ 
     LV1.setStartX(max); LV1.setEndX(max); 
    } 
    if (setCrosshair) { 
     chart.setCursor(Cursor.CROSSHAIR); 
    } else { 
     chart.setCursor(Cursor.DEFAULT); 
    } 
    } 

    private static double getSceneShift(Node node) { 
    double shift = 0; 
    do { 
     shift += node.getLayoutX(); 
     node = node.getParent(); 
    } while (node != null); 
    return shift; 
    } 

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

Sample program output

+0

嗨jewelsea,我中有你的建議 一幕一幕=新場景(新集團(splitPane1,LV),800,600)後更新了我的問題,以圖片; 但你可以看到我沒有得到預期的結果。 我錯過了一些東西,或者作爲替代,有沒有辦法使用綁定,如果是的話,如何? –

+0

要僅使用一個組,您還需要做額外的工作來擴展這兩個圖形上的線條。我更新了答案,提供了StackPane + Bind的示例,因爲原始示例似乎可以調整大小。 – jewelsea

+0

嗨jewelsea,很棒的工作,非常感謝你! –