Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFreeChart Margin

I am using JasperReports to create a line chart for my webapps.

I have successfully passed the dataset to the compiled report (created in iReport) and can see the data correctly.

However, I want to do some customization on the margin.

  1. The value shown on the line chart is trimming for the highest value as there is no margin.
  2. The X-Axis label is coming after few empty space from Y-Axis 0 value. I want to remove that margin and start the X-Axis from very close to the meeting point of X & Y.

Please see the picture:

enter image description here

I am using customized class which is defined in my webspps. I am able to change the font size and rotation of the label but don't know how to adjust margin.

public class LineChartCustomizer implements JRChartCustomizer {
    @Override
    public void customize(JFreeChart jFreeChart, JRChart jrChart) {
        CategoryPlot plot = jFreeChart.getCategoryPlot();

        DecimalFormat dfKey = new DecimalFormat("###,###");

        StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator("{2}", dfKey);  

        LineAndShapeRenderer renderer = new LineAndShapeRenderer();

        renderer.setBaseItemLabelsVisible(true);

        renderer.setBaseItemLabelGenerator(labelGenerator);

        renderer.setBaseItemLabelFont(new java.awt.Font("SansSerif", java.awt.Font.PLAIN, 4));        

        renderer.setSeriesShape(0, ShapeUtilities.createDiamond(1F));

        plot.setRenderer(renderer);
    }
}
like image 382
Chirag Jhaveri Avatar asked Oct 04 '22 03:10

Chirag Jhaveri


1 Answers

I think* you're looking for ValueAxis#setUpperMargin(double) and CategoryAxis#setLowerMargin(double). You can get the CategoryAxis and ValueAxis from plot.getDomainAxis() and plot.getRangeAxis(). Note that the margins are a percentage of the axis length and not pixel values.

* I'm not familiar with JasperReports, but it seems a little strange that you have a CategoryPlot in hand as opposed to an XYPlot. I would have expected the chart in your picture to have used an xy time series. I have only ever tested this with an XYPlot, so I'm not entirely sure how it will behave with a CategoryPlot.

like image 167
DannyMo Avatar answered Oct 05 '22 18:10

DannyMo