使用具有24小时数据的数据集在几天内渲染图表,但它仅在M-F,7AM到5PM期间有用。如果我使用下面的代码设置时间序列,我会得到一个包含所有24小时,每周7天的图表。有道理,但不是我的用例。
有没有办法定义时间序列显示的间隔?或者我是否需要使用不同的图表类型并尝试将我的数据整合到常规期间?我希望不是后者,而我收到的数据通常是30秒间隔,很容易出现差距。
发布一个工作用户界面的SSCE非常不可能用图表动态询问服务器的数据,但下面是一些要点,以了解我正在使用的图表类型。
一些plot.add,CombinedDomainXY,索引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
与自定义Timeline
。 SegmentedTimeline
,检查here,是一个具体的实施;虽然deprecated,它可以作为指导。基于这个example,你的名义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说明了一种减轻您遇到的渲染工件的方法。