Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

java - Limit JFreeChart TimeSeries to Business Hours

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...