How to avoid negative values with JFreeChart fixed auto range

主宰稳场 提交于 2019-12-04 17:27:16

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

}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!