JFreeChart | 如何在每个柱形图的顶部添加百分比并格式化域轴(X轴)刻度标签?

3

我正在使用JFreeChart,以下是我开发的图表截图和相关代码。

JFreeChart

    private void getBarChart(List<Data> data) {
JFreeChart barChart = ChartFactory.createBarChart("", "", "", createDataset(data), PlotOrientation.VERTICAL, false, true, false);
        CategoryPlot plot = barChart.getCategoryPlot();
        plot.getRenderer().setSeriesPaint(0, new Color(7, 43, 97));

        barChart.getCategoryPlot().getRangeAxis().setLowerBound(0);
        barChart.getCategoryPlot().getRangeAxis().setUpperBound(1);
        NumberAxis xAxis2 = (NumberAxis) barChart.getCategoryPlot().getRangeAxis();
        xAxis2.setNumberFormatOverride(NumberFormat.getPercentInstance());

        plot.getRenderer().setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator());
        plot.getRenderer().setSeriesItemLabelsVisible(1, true);
        plot.getRenderer().setBaseItemLabelsVisible(true);
        plot.getRenderer().setBaseSeriesVisible(true);
        barChart.getCategoryPlot().setRenderer(plot.getRenderer());


        BarRenderer.setDefaultBarPainter(new StandardBarPainter());
        ((BarRenderer) plot.getRenderer()).setBarPainter(new StandardBarPainter());

        BufferedImage image = new BufferedImage(650, 250, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = image.createGraphics();
        g2.setRenderingHint(JFreeChart.KEY_SUPPRESS_SHADOW_GENERATION, true);
        Rectangle r = new Rectangle(0, 0, 650, 250);
        barChart.draw(g2, r);
        BufferedImage chartImage = barChart.createBufferedImage(600, 400, null);
}

期望的图表应该类似于以下内容。 enter image description here 问题1.) 如何按照期望的图表格式化x轴标签?(在barChart.getCategoryPlot().getDomainAxis()中使用CategoryLables或TickLabels)
问题2.) 在每个条形图的顶部显示的值(SeriesItemLabels)需要使用类似于y轴的百分比符号(%)进行格式化。(我认为,就像我在xAxis2.setNumberFormatOverride中所做的那样,这些值将自动乘以100%。目前它仅显示小数值)。如何实现这一点?
请帮帮我。谢谢。
2个回答

4

以下是对于 BarChartDemo1.java 的工作进行更新,适用于 JFreechart 1.5,下面展示了一些替代方案。

  1. As shown here and here, you can invoke setCategoryLabelPositions() on the domain axis and use CategoryLabelPositions.createUpRotationLabelPositions to fine-tune the angle. The example below rotates counter-clockwise π/4 radians or 45°.

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0));
    
  2. A shown here, you can construct a custom StandardCategoryItemLabelGenerator, but you may want to use ArgumentIndex {3}, which is the value as a percentage of the column total, as well as a suitable NumberFormat.

    renderer.setDefaultItemLabelGenerator(
        new StandardCategoryItemLabelGenerator(
            "{3}", NumberFormat.getPercentInstance()));
    renderer.setDefaultItemLabelsVisible(true);
    

bar chart


3

1) 以下代码可以让坐标轴标签以上升的斜率显示:

CategoryAxis domainAxis = plot.getDomainAxis();  
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);

45代表角度,用度数表示,UP表示从左下到右上的方向。你也可以定义任意角度(例如22.5°)。

CategoryAxis domainAxis = plot.getDomainAxis();  
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.toRadians(22.5))); 

需要注意的是,createUpRotationLabelPositions 函数期望以弧度为单位的角度。

2) 下面这行代码将格式化系列 0 的柱形标签为百分比。

DecimalFormat labelFormat = new DecimalFormat("##0.0 %");
labelFormat.setMultiplier(100);
plot.getRenderer().setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator("{2}", labelFormat));
plot.getRenderer().setSeriesItemLabelsVisible(0, true);

使用 {0} = 系列, {1} = 类别, {2} = 值

作为替代方案,您可以定义自己的标签生成器,例如:

class CstmStandardCategoryItemLabelGenerator extends StandardCategoryItemLabelGenerator {

    @Override
    public String generateLabel(CategoryDataset dataset, int row, int column) {
        return String.format("%.1f %%", dataset.getValue(row, column).doubleValue() * 100.0);
    }
}

这可以简单地如下所示使用:

plot.getRenderer().setSeriesItemLabelGenerator(0, new CstmStandardCategoryItemLabelGenerator());

这会导致:

enter image description here


这正是我在寻找的。谢谢,伙计! - Amila Iddamalgoda

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接