Question

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?

Was it helpful?

Solution

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);

}

OTHER TIPS

I encountered the same problem, which I solved with:

axis.setAutoRangeMinimumSize(100); // Ensures graph always shows at least 0-100.
axis.setRangeType(RangeType.POSITIVE);

I'm using JFreeChart v1.0.14. Perhaps they've fixed a bug with setAutoRangeType since the question was originally posted?

One downside of this approach is that zero values are not visible.

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);
}

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);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top