2017-04-01 27 views
0

我目前正在使用JavaFX堆疊兩個不同的圖表(BarChart和LineChart)。 LineChart已被分成三個區域來顯示BarChart中的點落入的位置。對齊CategoryAxis和NumberAxis

我遇到了一個問題,我的當前代碼中BarChart的CategoryAxis和LineChart的NumberAxis在xAxis上沒有很好的對齊。 這是它目前的樣子:

enter image description here

正如你所看到的(點擊圖片可放大),一些沿x軸的數值並不一致。我無法爲LineChart和BarChart使用相同的NumberAxis,因爲BarChart只支持CategoryAxis。有沒有什麼辦法可以解決這個問題,使xAxis的值一致?

代碼:

NumberAxis xAxis = new NumberAxis(0, 6, 1); 
    xAxis.setLabel("X"); 

    NumberAxis yAxis = new NumberAxis(-5, 5, 0.5); 
    yAxis.setLabel("Y"); 

    XYChart.Series<Number, Number> dataSeries1 = new XYChart.Series<>(); 
    XYChart.Series<Number, Number> dataSeries2 = new XYChart.Series<>(); 

    dataSeries1.setName("2SD"); 
    dataSeries2.setName("3SD"); 

    dataSeries1.getData().add(new XYChart.Data<>(0, 0.6)); 
    dataSeries1.getData().add(new XYChart.Data<>(1, 0.9)); 
    dataSeries1.getData().add(new XYChart.Data<>(2, 1.7)); 
    dataSeries1.getData().add(new XYChart.Data<>(3, 1.8)); 
    dataSeries1.getData().add(new XYChart.Data<>(4, 1.8)); 
    dataSeries1.getData().add(new XYChart.Data<>(5, 2.1)); 
    dataSeries1.getData().add(new XYChart.Data<>(6, 2.3)); 

    dataSeries2.getData().add(new XYChart.Data<>(0, 1.0)); 
    dataSeries2.getData().add(new XYChart.Data<>(1, 1.3)); 
    dataSeries2.getData().add(new XYChart.Data<>(2, 2.4)); 
    dataSeries2.getData().add(new XYChart.Data<>(3, 2.5)); 
    dataSeries2.getData().add(new XYChart.Data<>(4, 2.7)); 
    dataSeries2.getData().add(new XYChart.Data<>(5, 3.1)); 
    dataSeries2.getData().add(new XYChart.Data<>(6, 3.3)); 

    final LineChart<Number, Number> lineChart = 
      new LineChart<Number, Number>(xAxis, yAxis, FXCollections.observableArrayList(dataSeries2, dataSeries1)) { 
       @Override 
       protected void layoutPlotChildren() { 
        super.layoutPlotChildren(); 

        XYChart.Series a = getData().get(0); 
        XYChart.Series b = getData().get(1); 
        ObservableList<XYChart.Data<Number, Number>> aData = a.getData(); 
        ObservableList<XYChart.Data<Number, Number>> bData = b.getData(); 

        for(int i = 0; i < aData.size()-1; i++){ 

         // Color in the area between 2SD and 3SD 
         double x = getXAxis().getDisplayPosition(aData.get(i).getXValue()); 
         double y = getYAxis().getDisplayPosition(bData.get(i).getYValue()); 
         double x2 = getXAxis().getDisplayPosition(aData.get((i+1)).getXValue()); 
         double y2 = getYAxis().getDisplayPosition(bData.get((i+1)).getYValue()); 

         Polygon middlePolygon = new Polygon(); 

         middlePolygon.getPoints().addAll(x,y, 
           x, getYAxis().getDisplayPosition(aData.get(i).getYValue()), 
           x2,getYAxis().getDisplayPosition(aData.get((i+1)).getYValue()), 
           x2,y2); 
         getPlotChildren().add(middlePolygon); 
         middlePolygon.toFront(); 
         middlePolygon.setFill(Color.ORANGE); 

         // Color in the area above 3SD 
         double y3 = getXAxis().getDisplayPosition(0); 
         double y4 = getXAxis().getDisplayPosition(0); 

         Polygon topPolygon = new Polygon(); 

         topPolygon.getPoints().addAll(x,y3, 
           x, getYAxis().getDisplayPosition(aData.get(i).getYValue()), 
           x2,getYAxis().getDisplayPosition(aData.get(i+1).getYValue()), 
           x2,y4); 
         getPlotChildren().add(topPolygon); 
         topPolygon.toFront(); 
         topPolygon.setFill(Color.RED); 

         // Color in the area below 2SD 

         double xb = getXAxis().getDisplayPosition(bData.get(i).getXValue()); 
         double yb = getYAxis().getDisplayPosition(-10); 
         double xb2 = getXAxis().getDisplayPosition(bData.get(i+1).getXValue()); 
         double yb2 = getYAxis().getDisplayPosition(-10); 

         Polygon bottomPolygon = new Polygon(); 

         bottomPolygon.getPoints().addAll(xb,yb, 
           xb, getYAxis().getDisplayPosition(bData.get(i).getYValue()), 
           xb2,getYAxis().getDisplayPosition(bData.get((i+1)).getYValue()), 
           xb2,yb2 
         ); 
         getPlotChildren().add(bottomPolygon); 
         bottomPolygon.toFront(); 
         bottomPolygon.setFill(Color.GREEN); 

        } 


       } 
      }; 

    lineChart.setCreateSymbols(false); 


    CategoryAxis xAxis2 = new CategoryAxis(); 
    xAxis2.setLabel("X"); 

    BarChart<String, Number> barChart = new BarChart<>(xAxis2, yAxis); 
    barChart.setLegendVisible(false); 
    barChart.setAnimated(false); 
    //barChart.setAlternativeRowFillVisible(false); 
    //barChart.setAlternativeColumnFillVisible(false); 
    //barChart.setHorizontalGridLinesVisible(false); 
    //barChart.setVerticalGridLinesVisible(false); 
    barChart.getXAxis().setVisible(false); 
    barChart.getYAxis().setVisible(false); 

    XYChart.Series<String, Number> barSeries = new XYChart.Series<>(); 
    barSeries.getData().add(new XYChart.Data<>("1", 2.4)); 
    barSeries.getData().add(new XYChart.Data<>("2", 2.7)); 
    barSeries.getData().add(new XYChart.Data<>("3", 0.8)); 
    barSeries.getData().add(new XYChart.Data<>("4", -3.2)); 
    barSeries.getData().add(new XYChart.Data<>("5", -0.1)); 

    barChart.getData().addAll(barSeries); 
    barChart.getStylesheets().add("sample/BarChart.css"); 
    barChart.setPadding(new Insets(0,0,35,3)); 

    StackPane pane = new StackPane(); 
    pane.getChildren().addAll(lineChart, barChart); 

回答

0

您可以在5.5,則刻度線將幾乎重疊使0.5您NumberAxis開始和結束。然後你告訴NumberAxisCategoryAxis不要顯示它的刻度標記,它會適合。

如果你不想改變NumberAxis的範圍,您沒有問題沒有對準BarChart值的數字,你可以簡單地告訴CategoryAxis不顯示它的刻度線。

該功能是setTickLabelsVisible(boolean),可能你也應該使用setTickMarkVisible(boolean)

我沒有測試這個。