如何在 jfreecharts 中绘制带有 TimeSeries 的图表,但不为我插入日期值?
我正在尝试使用 JFreechart 在 XYBarChart 上显示数据。图表应显示时间点和计数。因此,如果用户选择 1 月 25 日和 3 月 25 日作为范围,图表应以一定的时间间隔显示这些日期之间的数据:
如果用户选择 HOUR,那么我将创建从 1 月 25 日到 3 月 10 日的每个小时的集合,以及对应的计数。当然,这是一个相当大的收藏。如果用户选择 MONTH,则用户应该看到 3 个月(并且集合中只有 3 个条目)。如果用户选择 DAY,则用户只会看到大约 60 个条目。你看?
当我创建图表(使用下面的代码生成数据集)时,它会“插入”值。如果我选择小时,它会按天显示数据,每天之间有许多条形图(即一天内有 24 个小条形图)。如果我选择月份,我仍然会得到 2 个月的日期,但我会得到一个涵盖该月所有日期的巨大块。如果我选择DAY,看起来还不错。
如何让 jfreecharts 停止插值并仅绘制一个与我提供的数据 1:1 映射的图表。如果我给它两个 2 个月的条目,并给它一个 Month.class 的 RegularTimePeriod,则仅显示两个带有两个标签和两个值的条形。如果我在几个月内给它 1000 小时,然后用标签和计数显示每小时,依此类推...
TimeSeries timeSeries = new TimeSeries(title, "Blah", "blah", clazz);
// clazz is one of Day.class, Month.class, Hour.class
for (final ReportRecord reportRecord : records) {
int count = reportRecord.getCount();
Date start = reportRecord.getDateRange().getStart();
RegularTimePeriod period = null;
switch (type) {
case DAY: period = new Day(start); break;
case MONTH: period = new Month(start); break;
case HOUR: period = new Hour(start); break;
}
timeSeries.add(new TimeSeriesDataItem(period, count));
}
return new TimeSeriesCollection(timeSeries);
谢谢, 乔什
I'm trying to display data on a XYBarChart using JFreechart. The chart should display points in time along with a count. Thus, if the user chooses Jan 25 and March 25th as the range, the chart should display data between those dates in certain intervals:
if the user chooses HOUR, then I create a collection of every hour from Jan 25 to March 10th, along with a correspond count. Naturally, this is quite a big collection. If the user chooses MONTH, then the user should see 3 months (and only 3 entries in the collection). If the user chooses DAY, then the user would only see about 60 entries. You see?
When I create the chart (using the code below for the dataset generation) it 'interpolates' the values. If I choose hours, it shows data by day with many bars in between each day (ie, 24 little bars inside the day). If I choose months, I still get the days for 2 months, but I get one giant block that covers all days of the month. If I choose DAY, it looks alright.
How can I get jfreecharts to stop interpolating values and just draw a chart with a 1:1 mapping to what data I give it. If I give it two entries for 2 months and give it a RegularTimePeriod of Month.class, show just two bars with two labels and two values. If I give it 1000 hours over the course of several months, then display every hour with a label and a count, and so on...
TimeSeries timeSeries = new TimeSeries(title, "Blah", "blah", clazz);
// clazz is one of Day.class, Month.class, Hour.class
for (final ReportRecord reportRecord : records) {
int count = reportRecord.getCount();
Date start = reportRecord.getDateRange().getStart();
RegularTimePeriod period = null;
switch (type) {
case DAY: period = new Day(start); break;
case MONTH: period = new Month(start); break;
case HOUR: period = new Hour(start); break;
}
timeSeries.add(new TimeSeriesDataItem(period, count));
}
return new TimeSeriesCollection(timeSeries);
Thank you,
Josh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来 jfreechart 正在按照你的要求做。例如,当您说:
“如果我选择小时,它会按天显示数据,每天之间有许多条形图(即一天内有 24 个小条形图)。”
您提供的代码每小时向集合中添加一个项目,因此每小时一个单独的小栏是合适的。
如果您将集合添加逻辑更改为仅在您决定的时间添加一个值(例如每天午夜一次),则它应该只在相关日期的图表中生成一个条形。
过去,我只是简单地将值插入到最接近的秒(并不是说您需要这种准确性),jfreechart 将根据您选择的时间尺度(以编程方式或交互方式)处理其余的事情。
如:
Seems to me jfreechart is doing as you asked it. For example, when you say:
"If I choose hours, it shows data by day with many bars in between each day (ie, 24 little bars inside the day)."
The code you supplied adds an item to the collection for each hour, therefore a separate little bar for each hour is appropriate.
If you change the collection adding logic to only add a value at the time you decide (say once a day at midnight), it should only produce a single bar in the chart for the day in question.
In the past I've simply poked values in to the nearest second (not that you need that accuracy), and jfreechart will take care of the rest, depending on the time scale you choose (either programatically or interactively).
as in: