JFreeChart在子图上对齐BarChart宽度

时间:2013-03-02 10:28:17

标签: java swing charts jfreechart

我正在构建通过CombinedRangeCategoryPlot组合的多个堆积条形图(子图)。

由于子图数据集没有相同数量的项,并且由于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;

//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;

2 个答案:

答案 0 :(得分:1)

经过几个小时的搜索后,找到了解决方案:使用plot.setWeight()

出于某种原因,在将图添加到CombinedRangeCategoryPlot时,权重会重置为值1,因此必须在之后设置。

希望这有帮助。

答案 1 :(得分:1)

  

出于某种原因,在添加绘图时,权重会重置为值1

作为解释,