如何使用JFreeChart创建堆叠间隔图?

时间:2013-02-26 21:06:20

标签: java swing jfreechart

目标是建立一个区间条形图,为每个类别指出一系列可接受的值(介于0 - 100%之间)。

对于每个类别,我想指出2个区间(即中心区间为绿色,周围区间为灰色),我认为使用IntervalBarRenderer是不可行的,因此我使用了StackedBarRenderer (每个条的最左侧和右侧都有透明层)。

这是正确的做法吗?

此外,如果这是正确的方法,似乎透明的条形间隔(在每个条形图的最左侧和右侧),不显示它们的轮廓:它们是否有任何方式来显示透明的轮廓油漆

请参阅下面的主要代码和当前结果的屏幕截图。

非常感谢任何帮助或暗示!

托马斯 Current result - not satisfactory

private JFreeChart createChart(LeaksChartSeriesVO data){
    JFreeChart chart = ChartFactory.createStackedBarChart(
            "Leak meters", //Title
            "Leaks", //Domain axis (X) label
            "%", //Range axis (Y) label
            data.bars,
            PlotOrientation.HORIZONTAL, 
            true, //Legend?
            true, //Tooltip?
            false); //Urls?
    CategoryPlot plot = (CategoryPlot) chart.getPlot();

    //Sets X axis sub-legends
    SubCategoryAxis subCat = new SubCategoryAxis("Leak meters");

    //Adds second dataset
    plot.setDataset(1, data.dots);

    //Defines level renderer
    LevelRenderer renderer1 = new LevelRenderer();
    renderer1.setSeriesPaint(0, Color.black);
    plot.setRenderer(1, renderer1);

    //Sets Y axis as %
    ((StackedBarRenderer) plot.getRenderer()).setRenderAsPercentages(true);

    //Sets colors
    ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(0, new Color(0, 0, 0, 0)); //Transparent for start
    ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(1, Color.gray); //Grey low
    ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(2, Color.green); //Green
    ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(3, Color.gray); //Grey high
    ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(4, new Color(0, 0, 0, 0)); //Transparent for end
    ((StackedBarRenderer) plot.getRenderer()).setDrawBarOutline(true);
    ((StackedBarRenderer) plot.getRenderer()).setBaseOutlinePaint(Color.black);

    //Setup which items not to see in legend
    ((StackedBarRenderer) plot.getRenderer()).setSeriesVisibleInLegend(0, false);
    ((StackedBarRenderer) plot.getRenderer()).setSeriesVisibleInLegend(3, false);
    ((StackedBarRenderer) plot.getRenderer()).setSeriesVisibleInLegend(4, false);

    //Sets renderer & axis
    plot.setDomainAxis(subCat);

    //Changes plot render sequence so that bars are in the background and shapes in front
    chart.getCategoryPlot().setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

    //Sets margins between bars
    chart.getCategoryPlot().getDomainAxis().setCategoryMargin(0.5f);

    return chart;
}

private JFreeChart createChart(LeaksChartSeriesVO data){ JFreeChart chart = ChartFactory.createStackedBarChart( "Leak meters", //Title "Leaks", //Domain axis (X) label "%", //Range axis (Y) label data.bars, PlotOrientation.HORIZONTAL, true, //Legend? true, //Tooltip? false); //Urls? CategoryPlot plot = (CategoryPlot) chart.getPlot(); //Sets X axis sub-legends SubCategoryAxis subCat = new SubCategoryAxis("Leak meters"); //Adds second dataset plot.setDataset(1, data.dots); //Defines level renderer LevelRenderer renderer1 = new LevelRenderer(); renderer1.setSeriesPaint(0, Color.black); plot.setRenderer(1, renderer1); //Sets Y axis as % ((StackedBarRenderer) plot.getRenderer()).setRenderAsPercentages(true); //Sets colors ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(0, new Color(0, 0, 0, 0)); //Transparent for start ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(1, Color.gray); //Grey low ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(2, Color.green); //Green ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(3, Color.gray); //Grey high ((StackedBarRenderer) plot.getRenderer()).setSeriesPaint(4, new Color(0, 0, 0, 0)); //Transparent for end ((StackedBarRenderer) plot.getRenderer()).setDrawBarOutline(true); ((StackedBarRenderer) plot.getRenderer()).setBaseOutlinePaint(Color.black); //Setup which items not to see in legend ((StackedBarRenderer) plot.getRenderer()).setSeriesVisibleInLegend(0, false); ((StackedBarRenderer) plot.getRenderer()).setSeriesVisibleInLegend(3, false); ((StackedBarRenderer) plot.getRenderer()).setSeriesVisibleInLegend(4, false); //Sets renderer & axis plot.setDomainAxis(subCat); //Changes plot render sequence so that bars are in the background and shapes in front chart.getCategoryPlot().setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD); //Sets margins between bars chart.getCategoryPlot().getDomainAxis().setCategoryMargin(0.5f); return chart; }

1 个答案:

答案 0 :(得分:1)

这是用于绘制条形的GradientBarPainter的默认行为:当条形图是透明的时,它不会绘制轮廓。

您可以使用将绘制轮廓的StandardBarPainter

只需添加:

StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
renderer.setBarPainter(new StandardBarPainter());

另一个解决方案是保留GradientBarPainter,但为条形图使用几乎透明的颜色:

setSeriesPaint(0, new Color(0, 0, 0, 1));