BarChart bar value labels are hidden by the margin

2019-07-23 06:35发布

问题:

I am using Jaspersoft Studio to create the report file with charts.

When I use the bar chart and try to display the bar values labels these are covered by the margins. In my opinion this looks like a bug, is there any way to show the labels properly?

To better illustrate the problem you can see the issue on the picture and also find in red the expected values.

EDIT

Here is my progress customizing the bar chat. So far I could make several changes but I am stuck with the most important one.

I tried using huge values for the margins without any visual effect.

    rangeAxis.setUpperMargin(200);
    rangeAxis.setLowerMargin(200);

I also played with the bounds. In that case I can see the entire labels but the problem is that the values won't stop at 100 (so that is not a valid solution).

I discovered the reason why the margins are completely ignored:

public void setUpperMargin(double margin) Sets the upper margin for the axis (as a percentage of the axis range) and sends an AxisChangeEvent to all registered listeners. This margin is added only when the axis range is auto-calculated - if you set the axis range manually, the margin is ignored.

But if I remove the margins then the bounds are autocalculated from 0-125 what is not the scope.

As usual, any suggestion will be welcome.

For a better understanding you can download the sample code from my GitHub repository.

GitHub: https://github.com/MichaelKnight/jaspertest.git

回答1:

The chart in jasper report is generated by the jfreechart library. In your case you can:

1) Set the range axis upper margin to give more space to value label (see jfreechart BarChartDemo5).

2) Draw the label inside the bar (see jfreechart BarChartDemo7) After edit if you need fixed upper value of axis you need to use this, hence the label has no space left if upper limit equals max bar value

To customize your bar chart in jasper report create a customizer class (MyChartCustomizer) extending the JRChartCustomizer. This will expose:

public void customize(JFreeChart chart, ChartComponent chartComponent)
{
  //Simple example of increasing upper margin on range axis, to print label
  //inside of bar see jfreechart BarChartDemo7
  CategoryPlot plot = (CategoryPlot) chart.getPlot();
  NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
  rangeAxis.setUpperMargin(0.20); //Increase or decrease to change upper margin in percentages (0 --> 1.0)
  rangeAxis.setAutoRange(true); //make sure that fixed range is not set
}

add the class to classpath and in jrxml set the customizerClass attribute

<barChart>
    <chart customizerClass="my.package.MyChartCustomizer">
   ....
    </chart>
   ...
</barChart>