1

Rendering a chart over several days, with a dataset that has 24 hour data, but it's only useful during M-F, 7AM to 5PM. If I setup a time series with the code below, I get a chart that contains all 24 hours, 7 days a week. Makes sense, but not for my use case.

Is there a way to define what interval the time series displays? Or do I need to use a different chart type and attempt to fit my data into regular periods? I hope not the latter, while the data I receive is usually in 30 second intervals, there can easily be gaps.

It's pretty impossible to post an SSCE of a working UI with a chart dynamically asking for data from a server, but some highlights are below to get an idea of the chart types I'm using.

Some of the plot.add, CombinedDomainXY, index 0 code may seem strange. I have three subplots with the shared time values, I've pared it down to one here to keep it short. I assume there is a way to do what I need for one plot it would work for a chart with multiple subplots.

public ChartPanel extends JPanel
{
    private final MyDataset _myDataset = new MyDataset();
    private final XYPlot _myPlot = new XYPlot();
    _chartPanel = new ChartPanel( createChart() );
    private JFreeChart createChart()
    {
            CombinedDomainXYPlot plot = new CombinedDomainXYPlot(
                    timeAxis );
            plot.setGap( 10.0 );
            plot.setDomainPannable( true );

            plot.setDataset( index, dataset );
            NumberAxis axis = new NumberAxis();

            axis.setAutoRangeIncludesZero( false );
            plot.setRangeAxis( 0, axis );
            plot.setRangeAxisLocation( 0, axisLocation );
            plot.setRenderer( 0, new StandardXYItemRenderer() );
            plot.mapDatasetToRangeAxis( 0, index );

            // add the subplots...
            plot.add( _myPlot, 1 );
    }
}
public class MyDataset implements XYDataset
{
    @Override
    public double getYValue( int series, int item )
    {
        return getMyData(item);
    }
    @Override
    public double getXValue( int series, int item )
    {
        return _bars.get( item ).DateTime.toInstant().toEpochMilli();
    }
    //other basic overloaded methods left out for brevity
}
Evan
  • 2,441
  • 23
  • 36

1 Answers1

1

You may be able to use a DateAxis with a custom Timeline. SegmentedTimeline, examined here, is a concrete implementation; although deprecated, it may serve as a guide. Based on this example, your notional newWorkdayTimeline() might look something like this:

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

This example illustrates one way to mitigate any rendering artifacts you encounter.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    Just an update for anyone that runs across this. I grabbed the latest segmented timeline code, plugged it in as a new file to my code. Used trashgod's workday timeline. It functions exactly as I'd want it to. But with a couple thousand points, it would completely block the UI thread for far longer than is acceptable for my application. I ended up switching to an XY dataset implementation that returns X as indexes into my normalized time series, then implemented a hacked tick unit that is constructed with my dataset, and gets the date time from the dataset by index to render a date label. – Evan Apr 03 '19 at 16:09