Time Series Chart - Date Axis Question

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Psyala
Posts: 3
Joined: Tue Mar 10, 2020 12:35 pm
antibot: No, of course not.

Time Series Chart - Date Axis Question

Post by Psyala » Fri Mar 13, 2020 11:59 am

Hi there!

I have created a TimeSeriesChart which display 2 days worth of data at 30 minute granularity.

I would like the X-Axis to tick interval every 1 hour (which works).

However, because the data spans multiple days you can potentially see repeating times.
Would it be possible that when the time is "00:00" - also display the date ie "13-MAR 00:00".

My current code:

Code: Select all

        //Chart Settings
        jFreeChart = ChartFactory.createTimeSeriesChart("", "", "Value", dataSet, false, true, false);
        jFreeChart.setBackgroundPaint(Color.WHITe);

        //Plot Settings
        plot = jFreeChart.getXYPlot();
        plot.setOrientation(PlotOrientation.VERTICAL);
        plot.setBackgroundPaint(Color.BLUE);
        plot.setDomainGridlinePaint(Color.BLACK);
        plot.setRangeGridlinePaint(Color.BLACK);
        plot.setAxisOffset(new RectangleInsets(5, 0, 0, 5));
        renderer = new XYLineAndShapeRenderer(true, true);
        plot.setRenderer(renderer);

        //Date Axis Settings
        domainAxis = (DateAxis) plot.getDomainAxis();
        plot.setDomainAxis(domainAxis);
        domainAxis.setLowerMargin(0.01);
        domainAxis.setUpperMargin(0.01);
        domainAxis.setTickUnit(new DateTickUnit(DateTickUnitType.HOUR, 1, new SimpleDateFormat("HH:00")), true, true);
        domainAxis.setTickMarkPosition(DateTickMarkPosition.START);
Example of desired output:
https://imgur.com/a/QHnPR2z

Thanks in advance!

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Time Series Chart - Date Axis Question

Post by John Matthews » Wed Mar 18, 2020 12:15 am

One alternative: add a second domain axis with a different date format.

Psyala
Posts: 3
Joined: Tue Mar 10, 2020 12:35 pm
antibot: No, of course not.

Re: Time Series Chart - Date Axis Question

Post by Psyala » Fri Apr 03, 2020 3:36 pm

John Matthews wrote:
Wed Mar 18, 2020 12:15 am
One alternative: add a second domain axis with a different date format.
Hi John, would you advise on what the best way to approach that would be?

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: Time Series Chart - Date Axis Question

Post by John Matthews » Sat Apr 04, 2020 8:23 pm

Another alternative: add a suitable pattern to your SimpleDateFormat, perhaps D or E.

Psyala
Posts: 3
Joined: Tue Mar 10, 2020 12:35 pm
antibot: No, of course not.

Re: Time Series Chart - Date Axis Question

Post by Psyala » Mon Apr 06, 2020 9:04 am

I've managed to add another axis, which achievess what I desire with the following code:

Code: Select all

        //Date Axis Settings
        dateAxis = new DateAxis("");
        plot.setDomainAxis(1, dateAxis);
        plot.setDomainAxisLocation(1, AxisLocation.BOTTOM_OR_LEFT);
        plot.mapDatasetToDomainAxes(0, Arrays.asList(0, 1));
        dateAxis.setLowerMargin(0.01);
        dateAxis.setUpperMargin(0.01);
        dateAxis.setTickUnit(new DateTickUnit(DateTickUnitType.DAY, 1, new SimpleDateFormat("dd-MMM")), true, true);
        dateAxis.setTickMarkPosition(DateTickMarkPosition.START);
        dateAxis.setTickLabelFont(numberAxis.getTickLabelFont());
        dateAxis.setTickMarkStroke(new BasicStroke(1));
        dateAxis.setAxisLineVisible(false);

prit_sachin
Posts: 1
Joined: Tue Nov 03, 2020 5:12 am
antibot: No, of course not.

Re: Time Series Chart - Date Axis Question

Post by prit_sachin » Tue Nov 03, 2020 5:15 am

Psyala: Can I have your sample code, would like to see how dataset is formed, thanks in adavance

alarawany
Posts: 4
Joined: Sun May 20, 2018 10:54 pm
antibot: No, of course not.

Re: Time Series Chart - Date Axis Question

Post by alarawany » Thu Nov 11, 2021 7:20 am

add pattern that fits
Graduated from Soran University with First Class Degree with Honours in Computer Science

Mikexo
Posts: 1
Joined: Wed Dec 08, 2021 4:52 pm
antibot: No, of course not.

Re: Time Series Chart - Date Axis Question

Post by Mikexo » Tue Jan 04, 2022 5:49 pm

prit_sachin wrote:
Tue Nov 03, 2020 5:15 am
Psyala: Can I have your sample code, would like to see how dataset is formed, thanks in adavance
Agree, would be curious to see it too. Have free time at my home in Portugal this week so would be nice to look into a sample code. Please post if you can
Hi

Locked