|
Error when all y-axis values are the same |
Posted by YH Kim on Feb-23-2021 16:31 |
|
If all y-axis values are the same, the line graph is drawn strangely as in the picture attached to the error.
What options should be applied for the y-axis?
|
Re: Error when all y-axis values are the same |
Posted by Peter Kwan on Feb-24-2021 00:04 |
|
Hi YH,
I suspect the y-axis values for the blue line are not all the same. They may have minor difference. For example, one point may be at 5.00000000000000000 and the other point at 5.00000000000000001.
Many floating point computation will introduce rounding errors. For example, if you add 0.1 50 times in a for loop, you will get 4.9999999999999 instead of 5. So if the data have go through some computation, it may not be accurate.
If the above is the cause of the problem, there are two methods:
(a) After computation, round the number to 4 decimal place (or 8 decimal place). Floating point rounding error is usually very small (more than 12 decimal place), so if you round to 8 decimal place, it will remove the floating point rounding error.
(b) If the y-axis is auto-scaled by ChartDirector, you can use the ChartDirector Axis.setMinTickInc method to force the y-axis tick increment to be at least 0.001.
c->yAxis()->setMinTickInc(0.001);
In this case, the labels on the y-axis may become 4.999, 5, 5.001. The small floating point rounding error will not be visible in such an axis scale.
(*** NOTE: The above is in C++. If you need to convert to another language, please let me know.)
Please let me know if the above can solve the problem.
Regards
Peter Kwan |
Re: Error when all y-axis values are the same |
Posted by YH Kim on Feb-24-2021 09:06 |
|
First of all, thank you for your kind response.
I modified the c++ code with method (b) you suggested.
However, as shown in the attached picture, the line of the graph is located at the top or bottom.
In this case, is there a method other than the scale method?
(I often use the code below to set the maximum and minimum values.
c->yAxis()->setLinearScale(y_min_value, y_max_value);)
|
Re: Error when all y-axis values are the same |
Posted by Peter Kwan on Feb-24-2021 16:55 |
|
Hi YH Kim.
If you use Axis.setLinearScale, then the axis scale is determined by your code, not by ChartDirector. For example, if in your code y_min_value = 4.9999999999, and y_max_value = 5, then you will get the axis as shown in your last message. ChartDirector is forced to use 4.9999999999999 to 5 as the y-axis scale, as your code instructs ChartDirector to use that scale. But then your code also forces ChartDirector to use a minimum of 0.001 as the tick distance, so the final scale becomes 4.999 to 5.
If you want to put the line near the center, there are two methods:
(a) Please use suitable values for y_min_value and y_max_value so that the line is near the center.
or
(b) Do not use setLinearScale. ChartDirector will then automatically determine the axis scale. Normally, ChartDirector will make sure there is some distance between the line and the top/bottom of the plot area. You can still use Axis.setAutoScale and Axis.setMinTickInc to put some restrictions on how to auto-scale the axis. For example:
// make sure the tick increment is at least 0.001
c->yAxis()->setMinTickInc(0.001);
// Do not start the axis from 0 if it is not necessary.
c->yAxis()->setAutoScale(0.1, 0.1, 0);
Hope this can help.
Regards
Peter Kwan |
|