Problem in extending DateAxis

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
shehriih
Posts: 5
Joined: Wed Jan 09, 2008 9:26 am

Problem in extending DateAxis

Post by shehriih » Wed Jan 09, 2008 9:32 am

Hi all i am trying to extends DateAxis to cutomize draw method

Code: Select all

public class MyDateAxis extends DateAxis
and when creating JfreeChart

Code: Select all

JFreeChart jfreechart = ChartFactory.createGanttChart("Gantt Chart Demo", "Task", "Date", intervalcategorydataset, true, true, false);

CategoryPlot categoryplot = (CategoryPlot)jfreechart.getPlot();

MyDateAxis da = (MyDateAxis )categoryplot.getRangeAxis(); // causes ClassCastException 
// org.jfree.chart.axis.DateAxis cannot be cast to MyDateAxis 

please help on that !

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

Post by paradoxoff » Wed Jan 09, 2008 11:10 am

Hi,
I assume it is quite easy: since you can´t specify the type of the RangeAxis in the factory method, the factory has to guess the type of the range axis and is creating a NumberAxis by default. This, of course, can´t be cast to a DateAxis or any subclass of it! Instead of using the getter, simply use a setter:

Code: Select all

categoryplot.setRangeAxis(new (MyDateAxis))
That is one reason why I avoid the factory methods, because in that case you can never be sure what is really created.
Regards, paradoxoff

shehriih
Posts: 5
Joined: Wed Jan 09, 2008 9:26 am

Post by shehriih » Wed Jan 09, 2008 12:04 pm

sorry,

but it works if you cast it to DateAxis

Code: Select all

DateAxis da = (DateAxis)categoryplot.getRangeAxis(); 

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

Post by david.gilbert » Wed Jan 09, 2008 12:09 pm

Of course it does, because it IS a DateAxis. But it will never be a MyDateAxis until you call setRangeAxis() and make it so.
David Gilbert
JFreeChart Project Leader

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

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

Post by paradoxoff » Wed Jan 09, 2008 2:44 pm

Yep, sorry, of course David is right. I should think better before I post.

shehriih
Posts: 5
Joined: Wed Jan 09, 2008 9:26 am

Post by shehriih » Sat Jan 12, 2008 6:46 pm

thanks all :)

of course , you can not assign DateAxis to MyDateAxis


i have just checked the code

sorry..

Locked