2013-03-14 64 views
2

我有一個圖表,數據堆疊在( - ,+)Y軸上。JavaFx StackedBar圖表問題

  • 隨着JfreeChart所有這一切變得嚴謹和數據完美地顯示在每個酒吧沒有損失。 enter image description here

  • 但是,當使用javafx StackedBar圖表時,當bar系列有(正數爲&負數)數據堆棧欄損壞和丟失數據。 enter image description here

      if (plusValue > 0) { 
           series.getData().add(new XYChart.Data<String, Number>(xPoint, plusValue)); 
          } 
          if (minusValue > 0) { 
           series.getData().add(new XYChart.Data<String, Number>(xPoint, -minusValue)); 
          } 
    

更新:

- Oracle StackedBarChart Sample - 「我改系列值」

import java.util.Arrays; 
import javafx.application.Application; 
import javafx.collections.FXCollections; 
import javafx.scene.Scene; 
import javafx.scene.chart.CategoryAxis; 
import javafx.scene.chart.NumberAxis; 
import javafx.scene.chart.StackedBarChart; 
import javafx.scene.chart.XYChart; 
import javafx.stage.Stage; 

public class StackedBarChartSample extends Application { 

final static String austria = "Austria"; 
final static String brazil = "Brazil"; 
final static String france = "France"; 
final static String italy = "Italy"; 
final static String usa = "USA"; 
final CategoryAxis xAxis = new CategoryAxis(); 
final NumberAxis yAxis = new NumberAxis(); 
final StackedBarChart<String, Number> sbc = 
     new StackedBarChart<String, Number>(xAxis, yAxis); 
final XYChart.Series<String, Number> series1 = 
     new XYChart.Series<String, Number>(); 
final XYChart.Series<String, Number> series2 = 
     new XYChart.Series<String, Number>(); 
final XYChart.Series<String, Number> series3 = 
     new XYChart.Series<String, Number>(); 

@Override 
public void start(Stage stage) { 
    stage.setTitle("Bar Chart Sample"); 
    sbc.setTitle("Country Summary"); 
    xAxis.setLabel("Country"); 
    xAxis.setCategories(FXCollections.<String>observableArrayList(
      Arrays.asList(austria, brazil, france, italy, usa))); 
    yAxis.setLabel("Value"); 
    series1.setName("2003"); 
    // plus value 
    series1.getData().add(new XYChart.Data<String, Number>(austria, 200)); 
    series1.getData().add(new XYChart.Data<String, Number>(brazil, 300)); 
    series1.getData().add(new XYChart.Data<String, Number>(france, 400)); 
    series1.getData().add(new XYChart.Data<String, Number>(italy, 500)); 
    series1.getData().add(new XYChart.Data<String, Number>(usa, 600)); 
    // minus value 
    series1.getData().add(new XYChart.Data<String, Number>(austria, -200)); 
    series1.getData().add(new XYChart.Data<String, Number>(brazil, -300)); 
    series1.getData().add(new XYChart.Data<String, Number>(france, -400)); 
    series1.getData().add(new XYChart.Data<String, Number>(italy, -500)); 
    series1.getData().add(new XYChart.Data<String, Number>(usa, -600)); 
    series2.setName("2004"); 
    series2.getData().add(new XYChart.Data<String, Number>(austria, 200)); 
    series2.getData().add(new XYChart.Data<String, Number>(brazil, 300)); 
    series2.getData().add(new XYChart.Data<String, Number>(france, 400)); 
    series2.getData().add(new XYChart.Data<String, Number>(italy, 500)); 
    series2.getData().add(new XYChart.Data<String, Number>(usa, 600)); 
    // minus value 
    series2.getData().add(new XYChart.Data<String, Number>(austria, -200)); 
    series2.getData().add(new XYChart.Data<String, Number>(brazil, -300)); 
    series2.getData().add(new XYChart.Data<String, Number>(france, -400)); 
    series2.getData().add(new XYChart.Data<String, Number>(italy, -500)); 
    series2.getData().add(new XYChart.Data<String, Number>(usa, -600)); 
    series3.setName("2005"); 
    series3.getData().add(new XYChart.Data<String, Number>(austria, 200)); 
    series3.getData().add(new XYChart.Data<String, Number>(brazil, 300)); 
    series3.getData().add(new XYChart.Data<String, Number>(france, 400)); 
    series3.getData().add(new XYChart.Data<String, Number>(italy, 500)); 
    series3.getData().add(new XYChart.Data<String, Number>(usa, 600)); 
    // minus value 
    series3.getData().add(new XYChart.Data<String, Number>(austria, -200)); 
    series3.getData().add(new XYChart.Data<String, Number>(brazil, -300)); 
    series3.getData().add(new XYChart.Data<String, Number>(france, -400)); 
    series3.getData().add(new XYChart.Data<String, Number>(italy, -500)); 
    series3.getData().add(new XYChart.Data<String, Number>(usa, -600)); 
    Scene scene = new Scene(sbc, 800, 600); 
    sbc.getData().addAll(series1, series2, series3); 
    stage.setScene(scene); 
    stage.show(); 
} 

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

}

  • 沒有減值。 Without minus value
  • 負值數據損壞。 With minus value data corrupted
+0

你使用哪種版本的JFX的? –

+0

@Alexander Kirov java版本「1.7.0_11」 –

+0

你能否在jira中的JavaFX控件組件上提出問題? –

回答

1

我發現了StackedBarChart代碼的bug:

private Map<Series, Map<String,Data<X, Y>>> seriesCategoryMap = new HashMap<Series, Map<String, Data<X, Y>>>(); 
  • 這行代碼意味着每個系列都有map<String categoryPoint ,data bar>,所以該系列只有one bar for that categoryPoint,這條將推杆進入地圖作爲地圖將清除以前的酒吧數據的最後一個值。

    categoryMap.put(categoryPoint,bar); 
    
  • 但是,我們需要在seriesCategoryMap每個系列有categoryMap但是,地圖必須有for each categoryPoint to be List<Data>

  • 總結,對於每個系列我們有categoryMap和每個categoryPoint我們有List<Data<X,Y>> bar List to have positive and negative bars

  • 使代碼行成爲。

    private Map<Series, Map<String, List<Data<X, Y>>>> seriesCategoryMap = new HashMap<Series, Map<String, List<Data<X, Y>>>>(); 
    
  • 之後,我們必須更新代碼依賴於該數據結構。

1-更新 'dataItemAdded & seriesAdded' 的方法。

 // list to hold more that one bar "positive and negative" 
    List<Data<X, Y>> itemList = categoryMap.get(category) != null ? categoryMap.get(category) : new ArrayList<Data<X, Y>>(); 
    itemList.add(item); 
    categoryMap.put(category, itemList); 

2- getDataItem方法。

private List<Data<X, Y>> getDataItem(Series<X, Y> series, int seriesIndex, int itemIndex, String category) { 
    Map<String, List<Data<X, Y>>> catmap = seriesCategoryMap.get(series); 
    return catmap != null ? catmap.get(category) != null ? catmap.get(category) : new ArrayList<Data<X, Y>>() : new ArrayList<Data<X, Y>>(); 
} 

3- updateAxisRange & layoutPlotChildren更新兩個方法在負值和正值軸管理欄長度。

+0

哈立德,你在吉拉提交了一個問題嗎? –

+0

@AlexanderKirov是的,http://javafx-jira.kenai.com/browse/RT-28995 –

+0

@AlexanderKirov上述錯誤報告是否正確填寫?如果沒有,您可以通過上面的行填寫錯誤報告。在下一個版本中修復,然後告訴我刪除我的報告。 –