Auto range on x-axis goes negative while not needed

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Jorne
Posts: 10
Joined: Fri Mar 18, 2016 2:21 pm
antibot: No, of course not.

Auto range on x-axis goes negative while not needed

Post by Jorne » Wed Jun 15, 2016 3:02 pm

Hi,

I wrote a java Swing application which draws a graph with incoming data through serialport connection.

The first image shows the chart with a fixed X-axis range programmed in the software going from 0-5000. it has been set with the following piece of code:

Code: Select all

NumberAxis xAxis = (NumberAxis) plot.getDomainAxis();
xAxis.setTickUnit(new NumberTickUnit(1000));
xAxis.setRange(new Range(xAsPbar.getMinimum(), xAsPbar.getMaximum()));
Above code will get the following result
Image

Now when i set with user interaction that the X-axis has to be auto range, in this case the x-axis range should change to 0-3000 approx. because this is the range of the line that is been drawn.
I do this with the following piece of code:

Code: Select all

XYPlot plot = this.chart.getXYPlot();
plot.getDomainAxis().setAutoRange(true);
But when this code is triggered by user's interaction i get the following result:
Image

If i would do the same thing but for the Y-axis in above picture it would work the Y-axis would change it range to 0-500. but for the X-axis it goes from -7000 to 3000. which isn't correct i want it to change correctly with auto range.

Any advise?

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Auto range on x-axis goes negative while not needed

Post by paradoxoff » Wed Jun 15, 2016 6:40 pm

I have never seen the axis ranges go wild without a good reason.
The auto range of the axis is determined based on the value range of the dataset. What kind of dataset and renderer are you using?

Jorne
Posts: 10
Joined: Fri Mar 18, 2016 2:21 pm
antibot: No, of course not.

Re: Auto range on x-axis goes negative while not needed

Post by Jorne » Thu Jun 16, 2016 8:30 am

I'm using XYSeries in a XYSeriesCollection and a XYLineAndShapeRenderer.

I find it strange too. For the Y-axis it does it fine when i make it auto range trough user interaction is just goes from 0-3000 for examples if the highest value is 3000 in the data set.
But for the X-axis it won't do it properly.

I really hope someone can help me with this.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Auto range on x-axis goes negative while not needed

Post by paradoxoff » Thu Jun 16, 2016 1:05 pm

According to the legend, you have two dataset.
Please configure both renderers to show the data points as shapes.
I tentatively assume that you have some data points where both the preceding and the succeeding data points have Double.NaN as x or y coordinate. These data points will probably not be visible in a line chart, but will have an impact of the value range calculation.

Jorne
Posts: 10
Joined: Fri Mar 18, 2016 2:21 pm
antibot: No, of course not.

Re: Auto range on x-axis goes negative while not needed

Post by Jorne » Thu Jun 16, 2016 2:44 pm

Yes i got 2 datasets, the black dot serie is a clone of the red line serie. I created this so i know where the point being drawn is.
I set the draw shape boolean to true. this doesn't solve anything.

double.NaN: i wouldn't think this is the problem? I only add doubles to the series. I even check before adding to the dataset if the value is a double value.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Auto range on x-axis goes negative while not needed

Post by paradoxoff » Fri Jun 17, 2016 8:44 am

Jorne wrote:Yes i got 2 datasets, the black dot serie is a clone of the red line serie.
Why has the black dot series only one data point and the red series at least four?
Jorne wrote:I created this so i know where the point being drawn is.
For this purpose, you should have rather set the shapes visible in the first dataset/series instead of creating a new one.
Jorne wrote:double.NaN: i wouldn't think this is the problem?
Double.NaN can cause problems. See this thread.
Jorne wrote: I only add doubles to the series. I even check before adding to the dataset if the value is a double value.
The value Double.NaN is technically a double. And how would you perform your check? The various add-methods in the XYSeries-class only accept primitive doubles or instances of Number. If the "value" was not a double or a Number, you would not be able to call those methods at all without getting an error from the compiler.

One thing that I just nite is that your series have identical names. Please change that and see whether that is making a difference.

Jorne
Posts: 10
Joined: Fri Mar 18, 2016 2:21 pm
antibot: No, of course not.

Re: Auto range on x-axis goes negative while not needed

Post by Jorne » Fri Jun 17, 2016 9:48 am

Dear paradoxoff,

I'm very pleased of your effort to help me.

But the only thing your telling me is saying what could be the problem. But i need a solution to solve this problem. what can i do about the .NaN double values. How can i get my auto range to work like intented?

About the black dot series. i don't really think you understand the purpose of it.
The red line series is drawn constantly the same line. but i want to know in at which point the signal is at a certain time. but if the chart keeps drawing the same line (because this is the data i get from my hardware) then i need to be able to know where the point being drawn is. and that's why i use the black dot. If i just activate draw shapes in my software it will draw a shape for every point being drawn and this will also create a line after some time running the software. and then i will still not be able to see where my chart is drawing the points. So this is something that has to stay.

paradoxoff
Posts: 1634
Joined: Sat Feb 17, 2007 1:51 pm

Re: Auto range on x-axis goes negative while not needed

Post by paradoxoff » Fri Jun 17, 2016 10:56 am

All I can tell you again is that the auto range determination works as expected in most cases. There are a few cases where it doesn´t or may not do.
- As described in the link above, Double.NaN values might cause problems if the DomainOrder for the XYDataset is NOT DomainOrder.NONE. The DomainOrder for an XYSeriesColelction is dependent on whether the autoSort feature of the XYSeries is used or not. By default, the autoSort is used, unless you use the XYSeries conctructor XYSeries(java.lang.Comparable key, boolean autoSort) and set the autoSort to false.
- Identical series names can cause problems, as described here. I have already suggested to change that and see whether that makes a difference. Have you followed that suggestion?

In your last post, you have brought up some more potential sources of unexpected behaviour.

- Now you mention that you are getting the values from your "hardware"? What kind of hardware are we talking about? How are you retrieving the data? Are you 100 % sure that the data points that you are getting from the hardware are all > 0? You still haven´t explained how you check your values.
- Are we talking about a real time plot? And if we do, what is the update frequency? Maybe we are seeing some kind of threading issue?
- If we are talking about a real time plot: where is the "time information" shown on the plot ? In your screenshot, I am just seeing two axis with what looks like signal information, one measured, one calculated.
- You are absolutely right that I have not understood the purpose of the black series. As explained above, you have mentioned that "the black dot serie is a clone of the red line serie." Do you agree that, if that statement is true, the black dot series and the red line should have the same number of datapoints?
Jorne wrote:But the only thing your telling me is saying what could be the problem. But i need a solution to solve this problem.
Do you really believe that anybody will be able to locate the exact source of the problem, based on the informations that you have given so far, and based on the 5 lines of code in your first post?

Locked