views:

838

answers:

1

Hello, 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

A: 

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:

  TimeSeriesCollection dataset = new TimeSeriesCollection();
  TimeSeries series = new TimeSeries("Basal", Second.class);
  for (BasalRate basal : pInsulin.basals) {
   Second lSecond = new Second(basal.getTime());
   series.addOrUpdate(lSecond, basal.getValue());
  }
  dataset.addSeries(series);
sjBeta