Search code examples
javajfreechart

How to remove gaps between a time period of JFreeChart


I was wondering how to remove times e.g. (5pm to 9am) from a time series in JFreeChart. I have tried this:

SegmentedTimeline baseTimeLine = new SegmentedTimeline(
    SegmentedTimeline.DAY_SEGMENT_SIZE,24,1);

However, I don't think this is what is needed to remove time periods.


Solution

  • SegmentedTimeline.newFifteenMinuteTimeline(), seen here, is a good example from which to start. In this example, newWorkdayTimeline() creates a new SegmentedTimeline that includes 8 hours and excludes 16 hours. It then starts on Monday after the prescribed number of hours have passed. It then chains a newMondayThroughFridayTimeline() to get weekdays, 9-5.

    public static SegmentedTimeline newWorkdayTimeline() {
        SegmentedTimeline timeline = new SegmentedTimeline(
            SegmentedTimeline.HOUR_SEGMENT_SIZE, 8, 16);
        timeline.setStartTime(SegmentedTimeline.firstMondayAfter1900()
            + 8 * timeline.getSegmentSize());
        timeline.setBaseTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());
        return timeline;
    }
    

    Starting form this example, I plotted a week of random hourly data. Zoom in on the domain axis to see the effect. I've included a continuous dataset to make it easier to see the segment borders.

    private static final int N = 168; // a week
    …
    private static JFreeChart buildChart(
        …
        XYPlot plot = chart.getXYPlot();
        ((DateAxis) plot.getDomainAxis()).setTimeline(newWorkdayTimeline());
        …
        return chart;
    }
    

    image