Dheeraj Joshi
Dheeraj Joshi

Reputation: 3147

Add Margin and Chart header alignment in Jfree charts

Greeting,

I was wondering is it possible to add some margin to Jfree charts and align the chart title to left of the chart rather than default center of the chart.

Expected Chart

Required Chart
Need to add a 28 px margin to all directions. TOP , BOTTOM , LEFT and RIGHT And to make the chart title align to the left rather than center.

Chart which i am able to generate

Generate Chart

Upvotes: 0

Views: 2176

Answers (2)

huu duy
huu duy

Reputation: 2059

Set value to the lowerMargin, upperMargin property of X Axis. This is kotlin code:

    val dateAxis = DateAxis("Time")
    dateAxis.lowerMargin = 0.05
    dateAxis.lowerMargin = 0.05

And this is the result: [enter image description here]

Upvotes: 0

Dheeraj Joshi
Dheeraj Joshi

Reputation: 3147

To Add margin to Chart we can add Padding

public void addMargin(JFreeChart jChart){
      RectangleInsets chartRectangle = new RectangleInsets(28F,30F,30F,30F);
      //RectangleInsets chartRectangle = new RectangleInsets(TOP,LEFT,BOTTOM,RIGHT);
      jChart.setPadding(chartRectangle);
}

To align the header to Left set horizontal alignment to Chart title

public void alignChartTitle(JFreeChart jChart){
      jChart.getTitle().setHorizontalAlignment(HorizontalAlignment.LEFT);
}

Upvotes: 4

Related Questions