setTickMarkPosition - distorted axis labels

A free public discussion forum for the JFreeChart class library.

setTickMarkPosition - distorted axis labels

Postby bezalel_d » Mon Sep 25, 2006 10:23 am

When using DateTickMarkPosition.End in the setTickMarkPosition method for a TimeSeries chart, the axis labels miss out tickmarks for incomplete years at the beginning of the chart.

In the code below, despite the data ranging from Apr 2003, no axis label is displayed until Dec 2004. I would have expected a label for Dec 2003.

This can be reproduced by running the code below:

Code: Select all


import java.awt.Color;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class TestMonthlyChart extends ApplicationFrame {

    /**
     * Constructs the demo application.
     *
     * @param title  the frame title.
     */
    public TestMonthlyChart(String title) {

        super(title);
        IntervalXYDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 300));
        setContentPane(chartPanel);

    }
   
    private static JFreeChart createChart(IntervalXYDataset dataset) {
        JFreeChart chart = ChartFactory.createXYBarChart(
            "Random Weekly Data",
            "Year",
            true,
            "Number of People",
            dataset,
            PlotOrientation.VERTICAL,
            true,
            true,
            false
        );

        // then customise it a little...
        chart.setBackgroundPaint(Color.white);
       
        XYPlot plot = chart.getXYPlot();     
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);
        XYItemRenderer renderer = plot.getRenderer();
        renderer.setToolTipGenerator(new StandardXYToolTipGenerator("{1}. {2}", DateFormat.getInstance(), NumberFormat.getInstance()));
       
        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
        axis.setTickMarkPosition(DateTickMarkPosition.END);         
        return chart;
    }
   

    private static IntervalXYDataset createDataset() {

        TimeSeries t1 = new TimeSeries("Random Monthly Data", "Month", "Count", Month.class);
        try {
            t1.add(new Month(4, 2003), new Integer(2));
            t1.add(new Month(5, 2003), new Integer(3));
            t1.add(new Month(6, 2003), new Integer(31));
            t1.add(new Month(7, 2003), new Integer(2));
            t1.add(new Month(8, 2003), new Integer(5));
            t1.add(new Month(9, 2003), new Integer(21));
            t1.add(new Month(10, 2003), new Integer(21));
            t1.add(new Month(11, 2003), new Integer(21));
            t1.add(new Month(12, 2003), new Integer(21));
            t1.add(new Month(1, 2004), new Integer(9));
            t1.add(new Month(2, 2004), new Integer(1));
            t1.add(new Month(3, 2004), new Integer(7));
            t1.add(new Month(4, 2004), new Integer(2));
            t1.add(new Month(5, 2004), new Integer(3));
            t1.add(new Month(6, 2004), new Integer(31));
            t1.add(new Month(7, 2004), new Integer(2));
            t1.add(new Month(8, 2004), new Integer(5));
            t1.add(new Month(9, 2004), new Integer(21));
            t1.add(new Month(10, 2004), new Integer(21));
            t1.add(new Month(11, 2004), new Integer(21));
            t1.add(new Month(12, 2004), new Integer(21));
            t1.add(new Month(1, 2005), new Integer(9));
            t1.add(new Month(2, 2005), new Integer(1));
            t1.add(new Month(3, 2005), new Integer(7));
            t1.add(new Month(4, 2005), new Integer(2));
            t1.add(new Month(5, 2005), new Integer(3));
            t1.add(new Month(6, 2005), new Integer(31));
            t1.add(new Month(7, 2005), new Integer(2));
            t1.add(new Month(8, 2005), new Integer(5));
            t1.add(new Month(9, 2005), new Integer(21));
            t1.add(new Month(10, 2005), new Integer(21));
            t1.add(new Month(11, 2005), new Integer(21));
            t1.add(new Month(12, 2005), new Integer(21));
        }
        catch (Exception e) {
            System.err.println(e.getMessage());
        }
        TimeSeriesCollection tsc = new TimeSeriesCollection(t1, TimeZone.getTimeZone("America/New_York"));
        tsc.setDomainIsPointsInTime(false);
        return tsc;

    }

    /**
     * Creates a panel for the demo.
     * 
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        return new ChartPanel(createChart(createDataset()));
    }
   
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {

        TestMonthlyChart demo = new TestMonthlyChart("Random Monthly Data");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}




I would expect Dec-2003 to be shown on the chart axis despit Jan, Feb and Mar not being present in the data.

Any help appreciated here.
bezalel_d
 
Posts: 3
Joined: Mon Sep 25, 2006 10:13 am

Postby david.gilbert » Mon Sep 25, 2006 11:46 am

This is a bug. Thanks for the report, I've added it to the bug database:

http://sourceforge.net/tracker/index.ph ... tid=115494

I'm taking a closer look at it now...
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: 11367
Joined: Fri Mar 14, 2003 10:29 am

Postby bezalel_d » Mon Sep 25, 2006 12:30 pm

Thanks David. I've also noticed that when showing fewer month periods, the labels displayed are for May, Sep, Jan, May, Sep, Jan etc - even though setTickMarkPosition is set to DateTickMarkPosition.END.

Again I would expect this setting to bias year ends in this case so we would might get Feb, Aug, Dec, Feb, Aug, Dec etc - the 4 monthly chunks always ending in Dec rather than Jan. I suspect that this is currently arbitrary.

The above can be reproduced in the same code but by removing periods prior to Dec 2003.

Many thanks
bezalel_d
 
Posts: 3
Joined: Mon Sep 25, 2006 10:13 am

Postby david.gilbert » Mon Sep 25, 2006 2:26 pm

The first problem is now fixed in CVS. The other problem you have mentioned (when the tick spacing is a multiple of some time period) is more difficult to deal with - for now, the spacing is relative to the period that the start of the axis falls in. I'll have to think some more about the solution to that one...
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: 11367
Joined: Fri Mar 14, 2003 10:29 am

Postby HTPC2Good4U » Mon Oct 02, 2006 8:15 pm

For the other problem mentioned by this user, the solution that I found to my own problem might be able to be used:

http://www.jfree.org/phpBB2/viewtopic.php?p=54512#54512
HTPC2Good4U
 
Posts: 8
Joined: Mon Sep 18, 2006 8:38 pm


Return to JFreeChart - General

Who is online

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