2013-03-02 80 views
1

我正在構建通過CombinedRangeCategoryPlot組合的多個堆積條形圖(子圖)。JFreeChart對齊BarChart寬度跨子圖

由於subplots數據集沒有相同數量的項目,並且由於JFreeChart決定爲每個子圖分配相同的空間,所以我有不同寬度的小節。

有沒有什麼辦法可以調整它們的寬度(即使這意味着子圖具有不同的寬度)?

請看下面的結果和我到目前爲止的代碼。

非常感謝, 托馬斯

enter image description here

//Builds commong range axis 
NumberAxis rangeAxis = new NumberAxis("%"); 
rangeAxis.setRange(0, 1.0); 
rangeAxis.setNumberFormatOverride(NumberFormat.getPercentInstance()); 

//Builds common data set 
CombinedRangeCategoryPlot combinedPlots = new CombinedRangeCategoryPlot(rangeAxis); 
for (int groupIndex=0; groupIndex<LeakGroups.values().length; ++groupIndex){ 
    //Builds category axis 
    CategoryAxis categoryAxis = new CategoryAxis(GuiConstants.LEAK_GROUPS_LABELS[groupIndex]); 
    //Sets margins between bars 
    categoryAxis.setCategoryMargin(0.5f); 

    //Builds bar renderer 
    StackedBarRenderer barRenderer = new StackedBarRenderer(); 
    barRenderer.setRenderAsPercentages(true); 


    //Builds dot/level renderer 
    LineAndShapeRenderer dotRenderer = new LineAndShapeRenderer(); 
    //dotRenderer.setSeriesLinesVisible(0, false); 
    //dotRenderer.setSeriesShapesVisible(0, false); 
    //dotRenderer.setSeriesLinesVisible(1, false); 
    //Defines level shape height (depends on chart size): nominal values are for a height of 1000px 
    int shapeHeightPx = (int) Math.round(20 * (this.getHeight()/1000.0)); 
    dotRenderer.setSeriesShape(1, new Rectangle(-1, -shapeHeightPx/2, 2, shapeHeightPx)); 

    //Builds plot 
    CategoryPlot plot = new CategoryPlot(); 
    plot.setDomainAxis(categoryAxis); 
    plot.setDataset(0, data[groupIndex].bars); 
    plot.setRenderer(0, barRenderer); 
    plot.setDataset(1, data[groupIndex].dots); 
    plot.setRenderer(1, dotRenderer); 

    //Adds to combined 
    combinedPlots.add(plot); 
} 
combinedPlots.setOrientation(PlotOrientation.HORIZONTAL); 
//Puts range axis at the bottom 
combinedPlots.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 
//Changes plot render sequence so that bars are in the background and shapes in front 
combinedPlots.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); 
//Shows gridlines for categories and not for values 
combinedPlots.setDomainGridlinesVisible(true); 
combinedPlots.setRangeGridlinesVisible(false); 

//Creates chart 
JFreeChart chart = new JFreeChart("Leaks", combinedPlots); 

//Sets a margin right to allow space for last catergory label ("100%") 
chart.setPadding(new RectangleInsets(0, 0, 0, 20)); 

return chart; 

回答

1

搜索了幾個小時後,找到了解決辦法:使用plot.setWeight()

由於某些原因,當將圖添加到CombinedRangeCategoryPlot時,重量會重置爲值1,因此必須在之後進行設置。

希望這會有所幫助。