Issue Dual axis in jfreechart v1.0.18 and v1.0.19

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
kspat
Posts: 4
Joined: Thu Jan 08, 2015 12:10 pm
antibot: No, of course not.

Issue Dual axis in jfreechart v1.0.18 and v1.0.19

Post by kspat » Thu Jan 08, 2015 12:25 pm

Hi,

Hi I upgraded jfreechart from v1.0.14 to v1.0.19 and seeing issue with the dual axis graph. I had scales disaplyed on left and right side of the graph, after the upgarde both scales are being disaplyed on the left side. This is not seen in v1.0.17. PLease let me know the workaround.

Regards,
Karthik S Patawardhan

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Issue Dual axis in jfreechart v1.0.18 and v1.0.19

Post by david.gilbert » Thu Jan 08, 2015 6:26 pm

Do you have a small self-contained program that reproduces the issue? The dual axis examples that ship with the JFreeChart Developer Guide seem to be working OK.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

kspat
Posts: 4
Joined: Thu Jan 08, 2015 12:10 pm
antibot: No, of course not.

Re: Issue Dual axis in jfreechart v1.0.18 and v1.0.19

Post by kspat » Fri Jan 09, 2015 12:19 pm

Hello, Thanks a lot for the super quick reply :)

Please refer the following sample code which will plot the graph using XYPlot and I have added a line which actually adds extra scale on the right side of the graph. Similar code is being used in our application as well where we are using XYPlot and use setRangeAxis() & setDomainAxis() methods to get the scale on the right side of the graph. Please refer the tag "Line added by "kspat" which will plot extra scale on the right side of the graph. Issue only with 1.0.18 and above, not seen in v1.0.17".
I cross verified this code with v1.0.17 where its working fine and not working as expected in v1.0.19. Need your help in this case on how can I find a work around to resolve this issue.

Also need your suggestion on the version of jfreechart which can be used : v1.0.17 or v1.0.19. Please suggest. Thanks in advance. :)

Code: Select all

import java.awt.Color; 
import java.awt.Dimension; 
import javax.swing.JPanel; 
import org.jfree.chart.*;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis; 
import org.jfree.chart.plot.PlotOrientation; 
import org.jfree.chart.plot.XYPlot; 
import org.jfree.chart.renderer.xy.XYItemRenderer; 
import org.jfree.data.xy.DefaultXYZDataset; 
import org.jfree.data.xy.XYZDataset; 
import org.jfree.ui.ApplicationFrame; 
import org.jfree.ui.RefineryUtilities;
  
public class BubbleChart_AWT extends ApplicationFrame
{
   public BubbleChart_AWT( String s )
   {
      super( s );                 
      JPanel jpanel = createDemoPanel( );                 
      jpanel.setPreferredSize(new Dimension( 560 , 370 ) );                 
      setContentPane( jpanel ); 
   }

   private static JFreeChart createChart( XYZDataset xyzdataset )
   {
      JFreeChart jfreechart = ChartFactory.createBubbleChart(
         "AGE vs WEIGHT vs WORK",                    
         "Weight",                    
         "AGE",                    
         xyzdataset,                    
         PlotOrientation.HORIZONTAL,                    
         true, true, false);
         
      XYPlot xyplot = ( XYPlot )jfreechart.getPlot( );                 
      xyplot.setForegroundAlpha( 0.65F );                 
      XYItemRenderer xyitemrenderer = xyplot.getRenderer( );
      xyitemrenderer.setSeriesPaint( 0 , Color.blue );                 
      NumberAxis numberaxis = ( NumberAxis )xyplot.getDomainAxis( );                 
      numberaxis.setLowerMargin( 0.2 );                 
      numberaxis.setUpperMargin( 0.5 );                 
      NumberAxis numberaxis1 = ( NumberAxis )xyplot.getRangeAxis( );                 
      numberaxis1.setLowerMargin( 0.8 );                 
      numberaxis1.setUpperMargin( 0.9 );
      
      // Line added by "kspat" which will plot extra scale on the right side of the graph. Issue only with 1.0.18 and above, not seen in v1.0.17
      xyplot.setDomainAxis(1, numberaxis);
                     
      return jfreechart;
   }

   public static XYZDataset createDataset( )
   {
      DefaultXYZDataset defaultxyzdataset = new DefaultXYZDataset(); 
                     
      double ad[ ] = { 30 , 40 , 50 , 60 , 70 , 80 };                 
      double ad1[ ] = { 10 , 20 , 30 , 40 , 50 , 60 };                 
      double ad2[ ] = { 4 , 5 , 10 , 8 , 9 , 6 };                 
      double ad3[][] = { ad , ad1 , ad2 };                 
      defaultxyzdataset.addSeries( "Series 1" , ad3 );
                   
      return defaultxyzdataset; 
   }

   public static JPanel createDemoPanel( )
   {
      JFreeChart jfreechart = createChart( createDataset( ) );                 
      ChartPanel chartpanel = new ChartPanel( jfreechart );
                       
      chartpanel.setDomainZoomable( true );                 
      chartpanel.setRangeZoomable( true );
                 
      return chartpanel;
   }

   public static void main( String args[ ] )
   {
      BubbleChart_AWT bubblechart = new BubbleChart_AWT( "Bubble Chart_frame" );   
                    
      bubblechart.pack( );                 
      RefineryUtilities.centerFrameOnScreen( bubblechart );                 
      bubblechart.setVisible( true ); 
   }
}

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Issue Dual axis in jfreechart v1.0.18 and v1.0.19

Post by david.gilbert » Sat Jan 10, 2015 9:47 am

The main problem in your code is that you add the same axis object to the plot two times. You should create a new NumberAxis instance for the axis on the right side of the plot. The trick then is to make sure the auto axis range on this new axis matches the existing axis. Here is the code I used (modifications to your demo - this is the last part of the createChart() method):

Code: Select all

      // create a separate axis object for the axis at the right side
      NumberAxis axis2 = new NumberAxis("Axis 2"); 
      
      // make sure it has the same attributes as the axis at the left side
      axis2.setAutoRangeIncludesZero(false);
      axis2.setLowerMargin( 0.2 );                 
      axis2.setUpperMargin( 0.5 ); 
      
      // add it to the plot and set the location to the right side
      xyplot.setDomainAxis(1, axis2);
      xyplot.setDomainAxisLocation(1, AxisLocation.TOP_OR_RIGHT);
      
      // map the dataset to both the existing axis and the new one just added
      xyplot.mapDatasetToDomainAxes(0, Arrays.asList(0, 1));
      
      // the new axis doesn't have the theme settings, so reapply the theme to
      // the whole chart
      ChartUtilities.applyCurrentTheme(jfreechart);
      return jfreechart;
The important part is: xyplot.mapDatasetToDomainAxes(0, Arrays.asList(0, 1));

This tells JFreeChart that dataset 0 is related to domain axes 0 and 1. When it comes to plotting the data, the first axis will be used (axis 0 here), the second axis will have no impact on the data plotting. But when each axis is calculating its autorange, it will look in this map to find the datasets that should contribute to the axis range, so axis 1 will compute its range on the basis of the data in dataset 0. To ensure you end up with the same axis range on both axes, you need to make sure the margins and the autoRangeIncludesZero flag on the new axis match the existing axis.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

kspat
Posts: 4
Joined: Thu Jan 08, 2015 12:10 pm
antibot: No, of course not.

Re: Issue Dual axis in jfreechart v1.0.18 and v1.0.19

Post by kspat » Tue Jan 13, 2015 1:11 pm

Thanks a lot for your inputs. I will try it on our application and confirm if its working.

Still wondering how the same code works with v1.0.17 and not on v1.0.18 and later.

Also please let me know which is the best option to upgrade from v1.0.14 (we are looking for stable release): v1.0.17 or v1.0.19.

kspat
Posts: 4
Joined: Thu Jan 08, 2015 12:10 pm
antibot: No, of course not.

Re: Issue Dual axis in jfreechart v1.0.18 and v1.0.19

Post by kspat » Wed Jan 14, 2015 1:24 pm

Hello,

Thanks a lot for the solution provided. Its working fine now in our application. Thanks for the quick response. :)

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Issue Dual axis in jfreechart v1.0.18 and v1.0.19

Post by david.gilbert » Tue Jan 20, 2015 7:38 pm

kspat wrote:Also please let me know which is the best option to upgrade from v1.0.14 (we are looking for stable release): v1.0.17 or v1.0.19.
I would use 1.0.19, the API should be almost the same but there are some bug fixes (plus JavaFX support if you need it and are prepared to recompile with Java 1.8 ).
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

Locked