将 JFreeChart TimeSeries 限制为营业时间

2022-01-11 00:00:00 time-series java jfreechart

使用包含 24 小时数据的数据集呈现几天的图表,但它仅在 M-F、早上 7 点到下午 5 点有用.如果我使用下面的代码设置时间序列,我会得到一个包含每周 7 天、所有 24 小时的图表.有道理,但不适用于我的用例.

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.

有没有办法定义时间序列显示的间隔?或者我是否需要使用不同的图表类型并尝试将我的数据适合定期周期?我希望不是后者,虽然我收到的数据通常以 30 秒为间隔,但很容易出现差距.

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.

几乎不可能发布带有图表的工作 UI 的 SSCE,动态地从服务器请求数据,但下面是一些亮点,以了解我正在使用的图表类型.

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.

某些 plot.add、CombinedDomainXY、index 0 代码可能看起来很奇怪.我有三个共享时间值的子图,我在这里将其缩减为一个以保持简短.我认为有一种方法可以为一个图做我需要的事情,它适用于具有多个子图的图表.

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
}

推荐答案

您也许可以使用 DateAxis 与自定义 时间线.SegmentedTimeline,检查here,是一个具体的实现;虽然 已弃用,但它可以作为指南.基于这个 示例,您的概念 newWorkdayTimeline() 可能如下所示:

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.

相关文章