Setting the location of range axis for horizontal bar chart

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
cpb
Posts: 10
Joined: Wed Jul 29, 2015 7:12 am
antibot: No, of course not.

Setting the location of range axis for horizontal bar chart

Post by cpb » Wed Jul 29, 2015 7:33 am

Hi,

Although my query stems from use of Jasper Studio I'm pretty sure my query belongs here.

In Jasper Studio I have created a horizontal bar chart. By default the range axis is located at the top of the chart. I'd like to move this to the bottom of the chart. Control of the axes locations isn't hooked into the studio interface so I've created something called a chart customiser to override the default behaviour. Initially I used setItemMargin to prove the concept then tried modifying the customiser to override the range axis location. I've been able to override the IntemMargin and CategoryLabelPositions but can't re-locate the range axis.

Given the customize() code below is anyone able to advise on the use of setRangeAxisLocation? Should I be using it at all? Regardless, what is the correct approach when setting the range axis location?

Code: Select all

import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
import net.sf.jasperreports.engine.JRChart;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;

public class BarChartModifier3 extends JRAbstractChartCustomizer {

   public void customize(JFreeChart chart, JRChart jasperChart) {

       CategoryPlot categoryPlot = chart.getCategoryPlot();
       BarRenderer renderer = (BarRenderer) categoryPlot.getRenderer();

       // Spaces between bars
       renderer.setItemMargin(0.0);

       // set axis location
       categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
       // categoryPlot.setRangeAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
   }
}

cpb
Posts: 10
Joined: Wed Jul 29, 2015 7:12 am
antibot: No, of course not.

Re: Setting the location of range axis for horizontal bar ch

Post by cpb » Wed Jul 29, 2015 8:09 am

Problem solved!

After a little more tinkering I got the following to work. Can anyone explain the use of AxisLocation?

Code: Select all

import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
import net.sf.jasperreports.engine.JRChart;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;

public class BarChartModifier3 extends JRAbstractChartCustomizer {

   public void customize(JFreeChart chart, JRChart jasperChart) {

       CategoryPlot categoryPlot = chart.getCategoryPlot();
       BarRenderer renderer = (BarRenderer) categoryPlot.getRenderer();

       // Spaces between bars
       renderer.setItemMargin(0.0);
              
       // set axis location
       categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
   }
}

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Setting the location of range axis for horizontal bar ch

Post by paradoxoff » Wed Jul 29, 2015 12:20 pm

The AxisLocation is a rule to calculate the edge (top, right, bottom or left) of an axis, based on the type of the axis (x or y or in JFreeChart slang domain and range axis) and based on the plot orientation (horizontal or vertical).
To understand which combination of PlotOrientation and AxcisLocation leads to which edge, have a look at the static methods of the Plot class resolveDomainAxisLocation(AxisLocation location, PlotOrientation orientation) and resolveRangeAxisLocation(AxisLocation location, PlotOrientation orientation).

Locked