自由图表 |如何在每个栏的顶部添加百分比并设置域轴(X 轴)刻度标签的格式?

JFreeChart | How to add percentage to top of each bar and format domain axis (X axis) ticklabels?

我正在使用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);
}

预期的图表应该如下所示。

问题 1.) 如何按照预期的图形格式化 x 轴标签?(CategoryLabels 或 TickLabels in barChart.getCategoryPlot().getDomainAxis())

问题 2.) 每个条形 (SeriesItemLabels) 顶部显示的值需要使用类似于 y 轴的百分比标记 (%) 进行格式化。 (我还认为,就像我在 xAxis2.setNumberFormatOverride 中所做的那样,这些值将自动乘以 100%。现在它只显示十进制值)。如何实现?

请帮帮我。谢谢

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());

这导致:

BarChartDemo1.java, updated for JFreechart 1.5 开始,以下备选方案如下所示。

  1. 如图here and ,您可以在域轴上调用setCategoryLabelPositions()并使用CategoryLabelPositions.createUpRotationLabelPositions微调角度。下面的示例逆时针旋转 π/4 弧度或 45°。

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0));
    
  2. 显示的是 here, you can construct a custom StandardCategoryItemLabelGenerator, but you may want to use ArgumentIndex {3},这是列总计的百分比值,以及合适的 NumberFormat.

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