TickUnitSource: endless instances to avoid overlapping?

A free public discussion forum for the JFreeChart class library.

TickUnitSource: endless instances to avoid overlapping?

Postby mhilpert » Mon Sep 29, 2003 12:31 pm

Upgrade from 0.9.12 to 0.9.13, the following compilation error occured:

Code: Select all
TickUnits tickUnits = getStandardTickUnits();


Type mismatch: cannot convert from TickUnitSource to TickUnits

How can we replace this code so that the old behaviour doesn't break?

The problem is, that in my custom DateAxis, the following code doesn't work anymore, even if i just change the result of getStandardTickUnits() to TickUnitSource:

Code: Select all
        TickUnitSource tickUnits = getStandardTickUnits();
       
        DateFormat formatter = getDateFormatOverride();
        if (formatter != null) { //custom date format set?
            tickUnits.add(new DateTickUnit(DateTickUnit.MONTH, 3, formatter));
            tickUnits.add(new DateTickUnit(DateTickUnit.MONTH, 4, formatter));
            tickUnits.add(new DateTickUnit(DateTickUnit.MONTH, 5, formatter));
        } else {
            tickUnits.add(new DateTickUnit(DateTickUnit.MONTH, 3, new SimpleDateFormat(DEFAULT_DATEFORMAT)));
            tickUnits.add(new DateTickUnit(DateTickUnit.MONTH, 4, new SimpleDateFormat(DEFAULT_DATEFORMAT)));
            tickUnits.add(new DateTickUnit(DateTickUnit.MONTH, 5, new SimpleDateFormat(DEFAULT_DATEFORMAT)));
        }


All the add() methods fail with

The method add(DateTickUnit) is undefined for the type TickUnitSource

Is the correct way to cast TickUnitSource to TickUnits?

:(
Last edited by mhilpert on Mon Feb 25, 2008 4:49 pm, edited 1 time in total.
using JFreeChart 1.0.15 with JDK 1.7.0_25
mhilpert
 
Posts: 493
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Postby david.gilbert » Mon Sep 29, 2003 9:07 pm

I've introduced the TickUnitSource interface mainly in response to this problem:

http://www.jfree.org/phpBB2/viewtopic.php?t=5583

Now, in addition to the TickUnits class there is a StandardTickUnitSource class that also implements the TickUnitSource interface. This new class doesn't use an 'add' method, so you can no longer assume that the result from the getStandardTickUnits() method will support this.

I don't know the context of the code you posted, but instead of getStandardTickUnits() you might look at the createStandardTickUnits() method in the DateAxis class...you will be able to cast the result from this function to a TickUnits object.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11316
Joined: Fri Mar 14, 2003 10:29 am

Manual update

Postby tplatt » Wed Nov 05, 2003 11:22 am

Just had the same problem as section 8.4.9 in the manual is out of date

Code: Select all
XYPlot myPlot = myChart.getXYPlot();
NumberAxis axis = (NumberAxis) myPlot.getRangeAxis();
TickUnits units = NumberAxis.createIntegerTickUnits();
axis.setStandardTickUnits(units);


Should be

Code: Select all
XYPlot myPlot = myChart.getXYPlot();
NumberAxis axis = (NumberAxis) myPlot.getRangeAxis();
TickUnits units = (TickUnits) NumberAxis.createIntegerTickUnits();
axis.setStandardTickUnits(units);


Cheers

Theo
tplatt
 
Posts: 8
Joined: Fri Sep 12, 2003 2:44 pm
Location: Jersey, Channel Islands

Postby david.gilbert » Thu Nov 06, 2003 7:01 pm

Thanks for pointing that out, I will update it for the next release.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11316
Joined: Fri Mar 14, 2003 10:29 am

Postby mhilpert » Mon Feb 25, 2008 4:48 pm

I've written my own TickUnitSource but keep on increasing number of DateTickUnits as the wider the time span on the x axis is, the more DateTickUnits are required - otherwise, the labels fall back into "overlapping mode" ... :cry:

Code: Select all
    /**
     * Create tick units for a time period (e.g. Month.class) and date format (e.g. "MMM yyyy").
     *
     * @param timePeriod A JFreeChart RegularTimePeriod class (e.g. org.jfree.data.time.Month).
     * @param dateFormat Optional format string to display tick labels (e.g. "MMM yyyy").
     * @param timeZone Optional time zone (default: TimeZone.getDefault()).
     * @return Tick unit source for given <i>timePeriod</i>.
     */
    @SuppressWarnings("unchecked")
    static public final TickUnitSource createStandardTickUnits(final Class<?> timePeriod, String dateFormat, TimeZone timeZone) {
        final TickUnits result = new TickUnits();
       
        if (timePeriod != null) {
            String f = null; //default date format
            if (timePeriod == Hour.class) {
                f = "HH d-MM";
            } else if (timePeriod == Day.class) {
                f = "d-MM-yy";
            } else if (timePeriod == Week.class) {
                f = "w-yy";
            } else if (timePeriod == Month.class) {
                f = "MM-yy";
            } else if (timePeriod == Quarter.class) {
                f = "MM-yy";
            } else if (timePeriod == Year.class) {
                f = "yyyy";
            }
           
            if (dateFormat == null) {
                dateFormat = f;
            }
           
            if (timeZone == null) {
                timeZone = TimeZone.getDefault(); //default
            }
           
            final DateFormat df = new SimpleDateFormat(dateFormat);
            df.setTimeZone(timeZone);
           
            if (timePeriod == Hour.class) {
                for (int i = 1; i <= 24; i++) {
                    result.add(new DateTickUnit(DateTickUnit.HOUR, i, DateTickUnit.HOUR, 1, df));
                }
            } else if (timePeriod == Day.class) {
                for (int i = 1; i <= 31; i++) {
                    result.add(new DateTickUnit(DateTickUnit.DAY, i, DateTickUnit.DAY, 1, df));
                }
            } else if (timePeriod == Week.class) {
                for (int i = 1; i <= 56; i++) {
                    result.add(new DateTickUnit(DateTickUnit.MONTH, i, DateTickUnit.MONTH, 1, df));
                }
            } else if (timePeriod == Month.class) {
                for (int i = 1; i <= 120; i++) { //span 10 years to avoid overlapping
                    result.add(new DateTickUnit(DateTickUnit.MONTH, i, DateTickUnit.MONTH, 1, df));
                }
            } else if (timePeriod == Quarter.class) {
                for (int i = 1; i <= 4; i++) {
                    result.add(new DateTickUnit(DateTickUnit.YEAR, i, DateTickUnit.MONTH, 4, df));
                }
            } else if (timePeriod == Year.class) {
                result.add(new DateTickUnit(DateTickUnit.YEAR,   1, DateTickUnit.YEAR,  1, df));
                result.add(new DateTickUnit(DateTickUnit.YEAR,   2, DateTickUnit.YEAR,  1, df));
                result.add(new DateTickUnit(DateTickUnit.YEAR,   5, DateTickUnit.YEAR,  1, df));
                result.add(new DateTickUnit(DateTickUnit.YEAR,  10, DateTickUnit.YEAR,  1, df));
                result.add(new DateTickUnit(DateTickUnit.YEAR,  25, DateTickUnit.YEAR,  5, df));
                result.add(new DateTickUnit(DateTickUnit.YEAR,  50, DateTickUnit.YEAR, 10, df));
                result.add(new DateTickUnit(DateTickUnit.YEAR, 100, DateTickUnit.YEAR, 20, df));
            }
        }//else: input unavailable
       
        return result;
    }//createStandardTickUnits()


I wonder if there is no smarter solution to avoid label overlapping. Adding "endless" amounts of DateTickUnits can't be the solution ...
using JFreeChart 1.0.15 with JDK 1.7.0_25
mhilpert
 
Posts: 493
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany

Postby david.gilbert » Thu Feb 28, 2008 4:52 am

mhilpert wrote:I wonder if there is no smarter solution to avoid label overlapping. Adding "endless" amounts of DateTickUnits can't be the solution ...


There probably is a smarter solution, although I didn't think of one yet. The main problem is determining what the date format for the tick labels should be...when the axis is just displaying years, you probably want "YYYY" for the date format...but when you get down to seconds or milliseconds, you probably want the date labels to show the hours, minutes and seconds. If there is a way to auto-generate an appropriate formatter for a given tick size, then the solution is half-way done...
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11316
Joined: Fri Mar 14, 2003 10:29 am

Postby mhilpert » Thu Feb 28, 2008 9:10 pm

What's the problem you already need to state one fixed class like "Month.class" and the formatter is of course also fixed for one axis. So you can compute the maximum space for a label, e.g. a Date with "yyyy-MM-dd".
using JFreeChart 1.0.15 with JDK 1.7.0_25
mhilpert
 
Posts: 493
Joined: Wed Apr 02, 2003 1:57 pm
Location: Germany


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 16 guests