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

时间:2018-12-28 19:04:32

标签: java jfreechart

我正在使用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 个答案:

答案 0 :(得分:3)

BarChartDemo1.java开始,为JFreechart 1.5更新,以下示例如下。

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

    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setCategoryLabelPositions(
        CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 4.0));
    
  2. 如图所示的here,您可以构建自定义StandardCategoryItemLabelGenerator,但是您可能想使用 ArgumentIndex {3},其值为列总数的百分比以及合适的NumberFormat

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

bar chart

答案 1 :(得分:2)

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