将 JFreeChart TimeSeries 限制为营业时间
Limit JFreeChart TimeSeries to Business Hours
使用具有 24 小时数据的数据集呈现几天的图表,但它仅在周一至周五早上 7 点到下午 5 点期间有用。如果我使用下面的代码设置一个时间序列,我会得到一个包含一周 7 天 24 小时的图表。有道理,但不适用于我的用例。
有没有办法定义时间序列显示的间隔?或者我是否需要使用不同的图表类型并尝试将我的数据适合定期?我希望不是后者,虽然我收到的数据通常以 30 秒为间隔,但很容易出现间隙。
post 工作 UI 的 SSCE 与图表动态地从服务器请求数据几乎是不可能的,但下面的一些要点可以让您了解我的图表类型我正在使用。
某些plot.add、CombinedDomainXY、index 0 的代码可能看起来很奇怪。我有三个具有共享时间值的子图,我在这里将其缩减为一个以保持简短。我假设有一种方法可以完成我需要的一个图,它适用于具有多个子图的图表。
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
with a custom Timeline
. SegmentedTimeline
, examined here, is a concrete implementation; although deprecated, it may serve as a guide. Based on this ,您的假想 newWorkdayTimeline()
可能看起来像这样:
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;
}
此 example 说明了一种减轻您遇到的任何渲染瑕疵的方法。
使用具有 24 小时数据的数据集呈现几天的图表,但它仅在周一至周五早上 7 点到下午 5 点期间有用。如果我使用下面的代码设置一个时间序列,我会得到一个包含一周 7 天 24 小时的图表。有道理,但不适用于我的用例。
有没有办法定义时间序列显示的间隔?或者我是否需要使用不同的图表类型并尝试将我的数据适合定期?我希望不是后者,虽然我收到的数据通常以 30 秒为间隔,但很容易出现间隙。
post 工作 UI 的 SSCE 与图表动态地从服务器请求数据几乎是不可能的,但下面的一些要点可以让您了解我的图表类型我正在使用。
某些plot.add、CombinedDomainXY、index 0 的代码可能看起来很奇怪。我有三个具有共享时间值的子图,我在这里将其缩减为一个以保持简短。我假设有一种方法可以完成我需要的一个图,它适用于具有多个子图的图表。
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
with a custom Timeline
. SegmentedTimeline
, examined here, is a concrete implementation; although deprecated, it may serve as a guide. Based on this newWorkdayTimeline()
可能看起来像这样:
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;
}
此 example 说明了一种减轻您遇到的任何渲染瑕疵的方法。