How to display only "last 24 hours" from a "last 72 hours" JFreeChart TimeSeries
Asked Answered
R

1

2

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:

last 72hrs 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):

last 24hrs chart

I have looked through the API and could not find an appropriate answer, as I believe this should have some clever but simple solution.

Rappel answered 8/9, 2017 at 17:8 Comment(0)
F
2

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 a CategoryDataset, there is no built-in solution for an XYDataset. SlidingXYDataset, seen here, maybe an alternative. In the variation of SliderDemo2 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.

static final int COUNT = 3 * 24 * 60;
public static final int WINDOW = 24 * 60;
public static final int FIRST = 2 * 24 * 60;
…
this.slider.setValue(FIRST);

image

As an aside, note that some classes have moved since the patch was submitted:

import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

Can't I just use setMaximumItemAge()?

Not in isolation; addOrUpdate(), et al., calls removeAgedItems(false) before notifying listeners.

I am using Second.

Threes days of Second data, e.g., would imply setMaximumItemAge(3 * 24 * 60 * 60).

A sliding window might not be available to me since I am saving the chart as a JPEG.

You can pass the desired firstItemIndex as a parameter to the SlidingXYDataset constructor; you can update the index later via setFirstItemIndex() if needed.

Florrie answered 9/9, 2017 at 11:48 Comment(3)
Thank you! Can't I just use setMaximumItemAge() though? I understand that I am using Second so I will have to use setMaximumItemAge(86400) ? I am asking since the solution of a sliding window might not be available to me since I am saving the chart as a JPEG...Rappel
I am still not 100% on this. What does "not in isolation" mean? I have actually just tested setting maximum age of 86400 on a 3-day chart and it worked perfectly (showed just last 24 hours of data instead of 3 days...). To clarify, I added all the information normally, and just before new TimeSeriesCollection(series); I set series.setMaximumItemAge(86400);Rappel
Yeah I see but it only discards if I set the setMaximumItemAge, otherwise the default is 9223372036854775807L which is basically all the information...Rappel

© 2022 - 2024 — McMap. All rights reserved.