I wrote these 2 lines of code to create a chart using an XYDataset
:
final XYDataset dataset = new TimeSeriesCollection(myInfo.getSeries());
JFreeChart timechart = ChartFactory.createTimeSeriesChart(myInfo.getName()
+ " CPU (last 72h)", "", "CPU %", dataset, false, false, false);
These lines created this nice "last 72h" chart:
This is how I added the information to build this chart (this piece of code can run multiple times):
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
Date date = simpleDateFormat.parse(dateAsStringToParse);
Second second = new Second(date);
myInfo.getSeries().addOrUpdate(second, maxValue); // maxValue is an Integer
I want a (seemingly) simple alteration—to "cut" this only to the last 24 hours. I mean to see only the most "recent" 24 hours in the graph, like so (in the picture is a different chart I made using the same technique, but the information exists only for the last 24hrs):
I have looked through the API and could not find an appropriate answer, as I believe this should have some clever but simple solution.
Instead of discarding old data, as suggested here, it looks like you want to display a contiguous subset of the data, as if looking through a window. While
SlidingCategoryDataset
provides this feature for aCategoryDataset
, there is no built-in solution for anXYDataset
.SlidingXYDataset
, seen here, maybe an alternative. In the variation ofSliderDemo2
illustrated below, I've added three days of data to a single series with a one day window. I've retained the slider for easy review of earlier values.As an aside, note that some classes have moved since the patch was submitted:
Not in isolation;
addOrUpdate()
, et al., callsremoveAgedItems(false)
before notifying listeners.Threes days of
Second
data, e.g., would implysetMaximumItemAge(3 * 24 * 60 * 60)
.You can pass the desired
firstItemIndex
as a parameter to theSlidingXYDataset
constructor; you can update the index later viasetFirstItemIndex()
if needed.