Scale date axis of XYPlot to a fixed interval

2019-08-07 06:53发布

问题:

In Java project, I used JFreeChart 1.0.19 version and XYPlot to draw lines on plot.

I want scale xAxis (Date axis) as date in example - 10:00:05, 10:00:05, 10:00:10, 10:00:15…—scaling for every 5 seconds.

But now when user start with 10:00:03 time, it looks as 10:00:03, 10:00:08, 10:00:13, 10:00:18…

I want that always be scaled of 5 seconds as 10:00:05, 10:00:05, 10:00:10, 10:00:15 and be independently of start time, how to set it?

I configurated domain axis(time axis) as below:

domainAxis.setAutoRange(true);
domainAxis.setAutoTickUnitSelection(true);
domainAxis.setLowerMargin(0.001);
domainAxis.setUpperMargin(0.001);

domainAxis is a object of ValueAxis

My plot looks like as You see the time values are 11:21:57, 11:22:07, 11:22:17 etc. I want that always should be 11:21:55, 11:22:05, 11:22:15 etc.

回答1:

Because domainAxis is an instance of DateAxis, you can use setTickUnit() to specify a DateTickUnit having the desired unitType and multiple. In this fragment, the interval is five seconds, and the format includes seconds:

domainAxis.setTickUnit(new DateTickUnit(DateTickUnitType.SECOND, 5));
domainAxis.setDateFormatOverride(new SimpleDateFormat("HH:mm:ss"));