問題描述
使用包含 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.
這篇關于將 JFreeChart TimeSeries 限制為營業時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!