views:

2228

answers:

3

I have a JFreeChart line plot that is updated dynamically with one data point for every iteration of my algorithm. Because the number of data points can quickly become very large, I have used the setFixedAutoRange(double) method on the domain axis. This restricts the graph to displaying the n most recent iterations (200 in my case).

This works well, except during the first 200 iterations. The problem is that, until there have been 200 iterations, the axis includes negative values (for example, after 50 iterations, the range is from -150 to 50). Negative iterations make no sense. I would like the axis to start at zero rather than a negative value. How can I achieve this?

I don't mind whether the axis goes from 0 to 200 initially (with the right hand part of the chart left blank until the plot fills it up) or whether it starts at 0 to 1 and grows (so that the plot is always stretched across the full width of the chart). Either would acceptable, though I have a slight preference for the former.

Things I have tried:

Any ideas?

A: 

I now have a working solution, although I'm still interested in better approaches,

Initially I set the range of the domain axis to a fixed range of 0 to 200:

domainAxis.setRange(0, 200);

In the code that adds the data to the plot, I check to see whether it is the 200th value that is being added and if it is I switch the range to a fixed auto range of 200. This works, though it's a little bit clunky (especially as I also have to check whether the user has selected the option to disable the fixed window entirely and have it display all values).

if (!allDataButton.isSelected() && count == 200)
{
    domainAxis.setAutoRange(true);
    domainAxis.setFixedAutoRange(200);
}
Dan Dyer
+1  A: 

It looks like you're looking for a solution that involves configuring JFreeChart to do it for you, rather than manually setting the range.

I can't help with that....but here are some other ugly solutions :P ....

You could do something like this (sorry for the pseudo-code):

while(producingData) {
   this.produceData();
   if(!allDataButton.isSelected()) {
      domainAxis.setRange((count < 200) ? 0 : count-200), count);
    } else {
      domainAxis.setRange(0, count);
    }
}

If I were a perl-coder, I'd write it like this, just to make it a smidget harder to read :P

while(producingData) {
   this.produceData();
   domainAxis.setRange(
       (((count < 200) || allDataButton.isSelected()) ? 0 : count-200), count);

}
Jared
Thanks. This looks a bit simpler than my solution because it does it all in terms of the setRange method.
Dan Dyer
I've since discovered that JFreeChart's auto-range performs poorly. Each time you add a data point, it searches the whole data set for the min and max in order to adjust the range. Your solution (avoiding setAutoRange and setFixedAutoRange) doesn't suffer from that problem.
Dan Dyer
A: 

Things I have tried:

Calling setLowerBound doesn't play nicely with setFixedAutoRange. Calling setRangeType(RangeType.POSITIVE) doesn't seem to make any difference. Any ideas?

Did you try axis.setRange(0, 200);

penguru