DateAxis of timeseries containing days with 25 hours

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
JanKrause
Posts: 5
Joined: Thu Oct 11, 2007 7:40 am

DateAxis of timeseries containing days with 25 hours

Post by JanKrause » Thu Oct 11, 2007 8:13 am

Hello everybody outhere using jFreeChart,

I would like to plot a timeseries in a simple linechart. Considering summer- or wintertime (e.g. my timezone Europe/Berlin) a day could have 23, 24 or 25 hours. In the case of 25 hours I exspect the values maped to a dateaxis like

Code: Select all

--------------------------------------------------------- (>> my simple graph)
==========================================================================
00:00   01:00  02:00  02:00  03:00 ....  22:00  23:00  00:00
but I get

Code: Select all


--------------------------------------------------------- (>> my simple graph)
==========================================================================
00:00   01:00  02:00  03:00 .... 22:00  23:00  00:00  01:00
So the chart contains 25 hours correctly, but the labeling of the dateaxis is not correct (e.g. for the 28th October 2007). The last hour is displayed as the first hour of the following day.

Does anybody have an idea how to solve this problem?

Thanks in advance

Jan

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

Post by david.gilbert » Thu Oct 11, 2007 9:54 am

I'll take a look. Which DateAxis constructor do you use? Do you do anything special with the TickUnitSource?
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

JanKrause
Posts: 5
Joined: Thu Oct 11, 2007 7:40 am

Post by JanKrause » Thu Oct 11, 2007 10:51 am

Hello David,

thanks for your fast reply.
Which DateAxis constructor do you use?
I use the String-constructor of DateAxis with my axislabel like

Code: Select all

String xAxisLabel = "MyLabel";
DateAxis xAxis = new DateAxis(xAxisLabel);
I didn't help to use the constructor with the TimeZone:

Code: Select all

String xAxisLabel = "MyLabel";
DateAxis xAxis = new DateAxis(xAxisLabel, TimeZone.getTimeZone("Europe/Berlin"));
Do you do anything special with the TickUnitSource?
No.

Greetings

Jan

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

Post by david.gilbert » Thu Oct 11, 2007 12:37 pm

I used this test program with JFreeChart 1.0.6 and got what I think is the correct result ("2:00" appears twice on the axis, if you stretch the chart to make it wide enough). This is after I set my system locale to Europe/Berlin. Does it work for you?

Code: Select all

/* -----------------
 * DateAxisTest.java
 * -----------------
 * (C) Copyright 2007, by Object Refinery Limited.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 *
 */

import java.awt.Color;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Hour;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.RefineryUtilities;

public class DateAxisTest extends ApplicationFrame {

    public DateAxisTest(String title) {
        super(title);
        ChartPanel chartPanel = (ChartPanel) createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        chartPanel.setMouseZoomable(true, false);
        setContentPane(chartPanel);
    }

    private static JFreeChart createChart(XYDataset dataset) {

        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Test",  // title
            "Date",             // x-axis label
            "Price Per Unit",   // y-axis label
            dataset,            // data
            true,               // create legend?
            true,               // generate tooltips?
            false               // generate URLs?
        );

        chart.setBackgroundPaint(Color.white);

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setBaseShapesFilled(true);        
        return chart;

    }
    
    /**
     * Creates a dataset, consisting of two series of monthly data.
     *
     * @return The dataset.
     */
    private static XYDataset createDataset() {

        TimeSeries s1 = new TimeSeries("Hourly Data", Hour.class);
        s1.add(new Hour(23, 27, 10, 2007), 181.8);
        s1.add(new Hour(0, 28, 10, 2007), 181.8);
        s1.add(new Hour(1, 28, 10, 2007), 181.8);
        s1.add(new Hour(2, 28, 10, 2007), 181.8);
        s1.add(new Hour(3, 28, 10, 2007), 181.8);
        s1.add(new Hour(4, 28, 10, 2007), 181.8);
        s1.add(new Hour(5, 28, 10, 2007), 181.8);
        s1.add(new Hour(6, 28, 10, 2007), 181.8);
        s1.add(new Hour(7, 28, 10, 2007), 181.8);
        s1.add(new Hour(8, 28, 10, 2007), 181.8);
        s1.add(new Hour(9, 28, 10, 2007), 181.8);
        s1.add(new Hour(10, 28, 10, 2007), 181.8);
        s1.add(new Hour(11, 28, 10, 2007), 181.8);
        s1.add(new Hour(12, 28, 10, 2007), 181.8);
        s1.add(new Hour(13, 28, 10, 2007), 181.8);
        s1.add(new Hour(14, 28, 10, 2007), 181.8);
        s1.add(new Hour(15, 28, 10, 2007), 181.8);
        s1.add(new Hour(16, 28, 10, 2007), 181.8);
        s1.add(new Hour(17, 28, 10, 2007), 181.8);
        s1.add(new Hour(18, 28, 10, 2007), 181.8);
        s1.add(new Hour(19, 28, 10, 2007), 181.8);
        s1.add(new Hour(20, 28, 10, 2007), 181.8);
        s1.add(new Hour(21, 28, 10, 2007), 181.8);
        s1.add(new Hour(22, 28, 10, 2007), 181.8);
        s1.add(new Hour(23, 28, 10, 2007), 181.8);
        s1.add(new Hour(0, 29, 10, 2007), 181.8);
        
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        
        return dataset;

    }

    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        ChartPanel panel = new ChartPanel(chart);
        panel.setMaximumDrawHeight(8000);
        panel.setMaximumDrawWidth(8000);
        return panel;
    }
    
    public static void main(String[] args) {

        DateAxisTest demo = new DateAxisTest(
                "DateAxis Test");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);

    }

}
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

JanKrause
Posts: 5
Joined: Thu Oct 11, 2007 7:40 am

Post by JanKrause » Mon Oct 15, 2007 8:47 am

Hello David,

thanks for your piece of code. It works good for me, but now I have another problem. The 28th October 2007 has 25hours. So I have 02:00 - 03:00 (A) and 02:00 - 03:00 (B). I have values for both hours and I would like to display them in the chart. But if I add both I got an exception telling me that a value for 02:00 is already included in the series. How can I add both values to my TimeSeries?

Greetings

Jan

jwenting
Posts: 157
Joined: Sat Jul 15, 2006 7:46 am

Post by jwenting » Mon Oct 15, 2007 10:14 am

would by any chance the 28th mark the end of DST in the timezone you're using?

JanKrause
Posts: 5
Joined: Thu Oct 11, 2007 7:40 am

Post by JanKrause » Mon Oct 15, 2007 2:53 pm

Definitly yes. The DST in the timezone Europe/Berlin ends this year on the 28th October.

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

Post by david.gilbert » Mon Oct 15, 2007 5:29 pm

One way would be to use the FixedMillisecond time period class (which is just a thin wrapper around the java.util.Date encoding).
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

JanKrause
Posts: 5
Joined: Thu Oct 11, 2007 7:40 am

Post by JanKrause » Tue Oct 16, 2007 9:40 am

Hi everybody,

thanks, David. That hint works quite good.

Greetings

Jan

iogan18tm
Posts: 24
Joined: Tue May 04, 2004 5:14 pm

Post by iogan18tm » Sat Oct 27, 2007 11:39 am

[quote="JanKrause"]Hi everybody,

thanks, David. That hint works quite good.

Greetings

Jan[/quote]
Could you please post a code for that solution you found?

Post Reply