2017-04-01 73 views
0

我想要在條形圖中繪製一條線。該線反映了與其他值進行比較的某個值。我將solution for a LineChart應用於我的情況(請參閱代碼)。但是,沒有畫線。JavaFX無法在BarChart中繪製線條

public XYChart buildBarChart() 
    { 
     Line valueMarker = new Line(); 
     CategoryAxis xAxis = new CategoryAxis(); 
     NumberAxis yAxis = new NumberAxis(); 
     BarChart<String,Number> barChart = new BarChart<String,Number> (xAxis,yAxis); 
     barChart.setBarGap (-10); 
     barChart.setCategoryGap (0); 
     barChart.getYAxis().setTickLabelsVisible(false); 
     barChart.getYAxis().setOpacity(0);  
     yAxis.setLabel ("Score"); 

     // update marker 
     Node chartArea = barChart.lookup (".chart-plot-background"); 
     Bounds chartAreaBounds = chartArea.localToScene (chartArea.getBoundsInLocal()); 

     // remember scene position of chart area 
     yShift = chartAreaBounds.getMinY(); 

     // set x parameters of the valueMarker to chart area bounds 
     valueMarker.setStartX (chartAreaBounds.getMinX()); 
     valueMarker.setEndX (chartAreaBounds.getMaxX()); 

     // find pixel position of that value 
     double displayPosition = yAxis.getDisplayPosition (valNederland); 

     // update marker 
     valueMarker.setStartY (yShift + displayPosition); 
     valueMarker.setEndY (yShift + displayPosition); 

     BarChart.Series<String,Number> series1 = new BarChart.Series<String,Number>(); 
     BarChart.Series<String,Number> series2 = new BarChart.Series<String,Number>(); 
     BarChart.Series<String,Number> series3 = new BarChart.Series<String,Number>(); 
     BarChart.Series<String,Number> series4 = new BarChart.Series<String,Number>(); 

     series1.getData().add (new XYChart.Data<String,Number> (sRegio, valRegio_2015)); 
     series2.getData().add (new XYChart.Data<String,Number> (sOmgeving, valOmgeving)); 
     series3.getData().add (new XYChart.Data<String,Number> (sRest, valRest)); 
     series4.getData().add (new XYChart.Data<String,Number> (sNl, valNederland)); 
     barChart.getData().addAll (series1, series2, series3, series4); 

     return barChart; 
    } /*** buildBarChart ***/ 

這種情況的關鍵是要有ChartArea的座標。當我在調試器中檢查ChartArea的屬性時,我發現_geomBounds的所有值都是'unlogic',即maxX/Y = -1和minX/Y = -1。在chartASreaBounds分配一個值後,我只檢查一行。

對我來說,似乎barChart查找失敗,因爲我以前有類似的問題。有沒有人有建議如何糾正這種情況?

編輯

的條形圖是在構造函數創建如下:

public Charter (int nTimes, int nCategories, float [] years, String [] titles, String label, Indicator ind, float [] past) 
    { 
     this(); 

     // all kind of code to do somethong with the parameters omitted 
     // ... 

     // Setting up the BarChart. Note that past == null in my experiments 
     HBox hBox = new HBox(); 
     XYChart bar = buildBarChart(); 
     bar.setPrefHeight (height); 
     bar.setPrefWidth (width); 

     VBox buttons = buildButtonView(); 

     if (past != null) 
     { 
     XYChart line = buildLineChart(); 
     line.setPrefHeight (height); 
     line.setPrefWidth (width); 
     hBox.getChildren().addAll (bar, line); 
     } else 
     { 
     hBox.getChildren().addAll (bar); 
     } // if 
     Label dsecription = new Label (label); 
     this.getProperties().put ("--Indicator", ind.getName()); 
     this.getChildren().addAll (dsecription, hBox); 
    } /*** Charter ***/ 

後憲章已經創造了它被添加到垂直框中,並與其在屏幕上顯示的垂直框已經添加到現場。

+0

何時調用該方法?也許在BarChart被淘汰之前? – findusl

+0

可能是這種情況,我不知道BarChart何時佈局。我在我的問題中添加了一個編輯 – Arnold

回答

0

它看起來像是要在屏幕添加到場景之前計算屏幕上條形圖的位置。因爲在那時它還沒有在屏幕上位置是-1或沒有定義。

我傾向於在fxml中製作佈局,因此不會出現這樣的問題。如果你需要在代碼中完成它,我認爲在繪製線條之前先將它添加到hBox的子項中可以解決問題。

+0

謝謝,我會嘗試。我認爲我應該首先將hBox添加到一個場景中,以便完成這項工作。 – Arnold