如何仅显示“过去24小时"从“最后72小时"开始JFreeChart时间序列 [英] How to display only "last 24 hours" from a "last 72 hours" JFreeChart TimeSeries
问题描述
我编写了这两行代码以使用XYDataset
创建图表:
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);
这些行创建了一个很好的过去72小时"图表:
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
我想要一个(看似)简单的更改-仅将此更改剪切"到最后24小时.我的意思是只在图表中看到最近的24小时,就像这样(图片中是我使用相同技术制作的另一张图表,但是信息仅在最近的24小时内存在):
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):
我浏览了API,找不到合适的答案,因为我认为这应该有一些聪明而简单的解决方案.
I have looked through the API and could not find an appropriate answer, as I believe this should have some clever but simple solution.
推荐答案
而不是像建议的那样丢弃旧数据, SlidingCategoryDataset
提供了此功能CategoryDataset
的功能,XYDataset
没有内置的解决方案. SlidingXYDataset
(在此处中看到),也许是另一种选择.在下面说明的SliderDemo2
的变体中,我将三天的数据添加到具有一日窗口的单个系列中.我保留了滑块,以便于查看较早的值.
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);
顺便说一句,请注意,自提交补丁以来,有些类已经移动:
As an aside, note that some classes have moved since the patch was submitted:
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
我不能只是使用
setMaximumItemAge()
吗?
不是孤立的; addOrUpdate()
,等,在通知侦听器之前先调用removeAgedItems(false)
.
Not in isolation; addOrUpdate()
, et al., calls removeAgedItems(false)
before notifying listeners.
我正在使用
Second
.
Second
数据的三天时间表示setMaximumItemAge(3 * 24 * 60 * 60)
.
Threes days of Second
data, e.g., would imply setMaximumItemAge(3 * 24 * 60 * 60)
.
由于我将图表另存为JPEG,因此可能无法使用滑动窗口.
A sliding window might not be available to me since I am saving the chart as a JPEG.
您可以将所需的firstItemIndex
作为参数传递给SlidingXYDataset
构造函数;您可以稍后根据需要通过setFirstItemIndex()
更新索引.
You can pass the desired firstItemIndex
as a parameter to the SlidingXYDataset
constructor; you can update the index later via setFirstItemIndex()
if needed.
这篇关于如何仅显示“过去24小时"从“最后72小时"开始JFreeChart时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!