Bug in Y Axis if I use AutoRange for X axis

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Bug in Y Axis if I use AutoRange for X axis

Post by Nisbo » Mon Aug 15, 2016 12:30 pm

Hi,

if I use AutoRange and I have only 1 Value for the Y axis it will show 7,8E1 instead of 78
I can NOT use a fixed range

Code: Select all

final XYPlot plot  = chart.getXYPlot();
ValueAxis    axisX = plot.getDomainAxis();

axisX.setAutoRange(true);
axisX.setFixedAutoRange(360000.0); 
Image

How can I fix this ?

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Bug in Y Axis if I use AutoRange for X axis

Post by matinh » Tue Aug 16, 2016 7:45 am

Are you mixing up axes? The domain-axis is for x-values and the range-axis is for y-values. In your code you are setting the auto-range feature for x-axis (time) and this seems to work fine (I don't know the data, though). For the y-axis you are not settings anything, so I guess the auto-range feature is active but without any fixed-auto-range. To the actual range for the y-axis is probably very, very small. And as the format for the y-axis seems to be somthing like "0.#", you only see the same value for all ticks.

To fix yoiur problem, try setting the fixed-auto-range for the y-axis instead of the x-axis to get a more suitable range. Or try to change the format for the tick-labels to get more suitable labels.

hth,
- martin

Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Re: Bug in Y Axis if I use AutoRange for X axis

Post by Nisbo » Tue Aug 16, 2016 8:22 am

Hi Martin, I can not set a fixed Autorange for the Y axis because I am getting my data dynamically from a MySQL database, sometimes I have a value of 20 °C and sometimes a value of 450 Watt so if I set the range fixed to 500 I will see the plot for the temperature (of course in a different chart) at the bottom, I can explain it "longer" if you want ;)

Here is a video of my program (start at second 45)
https://www.youtube.com/watch?v=JmRYR8PLXfA

So what I need is: if during the visible time on the x axis the value (maybe 450 Watt) is the same than I want to see 450 on the left side of the chart (y axis) and not 45E1

and yes I tries also axisY.setAutoRange(true);

EDIT: axisY.setAutoTickUnitSelection(false);
is doing exactly what I want but only if I have 1 plot on the chart, if I have more than 1 plot the y axis is not readable (to much entries)
EDIT2: ok and if the constant value on a single plot chart change the y axis is also not readable

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Bug in Y Axis if I use AutoRange for X axis

Post by matinh » Tue Aug 16, 2016 8:46 am

Nisbo wrote:Hi Martin, I can not set a fixed Autorange for the Y axis because I am getting my data dynamically from a MySQL database, sometimes I have a value of 20 °C and sometimes a value of 450 Watt so if I set the range fixed to 500 I will see the plot for the temperature (of course in a different chart) at the bottom, I can explain it "longer" if you want ;)
You missed the point of "fixed autorange". Autorange means, the end-points of the scale are determined automatically depending on the data. So using this feature is what you want. But fixed autorange just defines what JFreeChart does, when finding a range is not easily possible. For example, if you just have a single value (say 20,0), what would you expect to be the upper and lower ends of the scale? [19,21]? or [10,30]? Or something else? For this, the fixed-autorange can be set, so you can control this. Read the JavaDoc for details.
Nisbo wrote: So what I need is: if during the visible time on the x axis the value (maybe 450 Watt) is the same than I want to see 450 on the left side of the chart (y axis) and not 45E1
This is actually only formatting! Try using axis.setNumberFormatOverride(...) to format the numbers as you want.

hth,
- martin

Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Re: Bug in Y Axis if I use AutoRange for X axis

Post by Nisbo » Tue Aug 16, 2016 9:53 am

Hi Martin,

thx 4 your reply and you are right, I missed the meaning of Autoplot

Now I tried
axisY.setFixedAutoRange(1);
this removed the xxE1 Values but now the plot is on the top and only the half width of the plot is visible (see screenshot)
Image
For example, if you just have a single value (say 20,0), what would you expect to be the upper and lower ends of the scale? [19,21]? or [10,30]? Or something else? For this, the fixed-autorange can be set, so you can control this. Read the JavaDoc for details.
After this I was expecting the plot in the middle and not on the top

I did "RTFM" as you recommend ^^
http://www.jfree.org/jfreechart/api/jav ... ge-double-
but it says only: Sets the fixed auto range for the axis.

So any idea to get the plot in the middle ? Or bedder let the plot on the top (makes more sense) but in this case complete visible.

I tried
axisY.setUpperMargin(10);
setUpperMargin(double margin)
Sets the upper margin for the axis (as a percentage of the axis range) and sends an AxisChangeEvent to all registered listeners.
but no change

Changing of the format works
NumberAxis axis = (NumberAxis)chart.getXYPlot().getRangeAxis();
axis.setNumberFormatOverride(new DecimalFormat("0"));
but in this case I have no decimal (1,23456) values if I zoom in or I have always decimal values if I am complete zoomed out

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Bug in Y Axis if I use AutoRange for X axis

Post by matinh » Tue Aug 16, 2016 10:10 am

Nisbo wrote:Now I tried
axisY.setFixedAutoRange(1);
this removed the xxE1 Values but now the plot is on the top and only the half width of the plot is visible (see screenshot)
If you change the fixed-auto-range of the y-axis this should not effect the width of the plot (or the x-axis, i.e. the date) in any way. I'm not sure what you mean by "only the half width of the plot is visible".
Nisbo wrote:
For example, if you just have a single value (say 20,0), what would you expect to be the upper and lower ends of the scale? [19,21]? or [10,30]? Or something else? For this, the fixed-autorange can be set, so you can control this. Read the JavaDoc for details.
After this I was expecting the plot in the middle and not on the top
You mean the data (the line representing your measurements) should be in the (vertical) middle of the plot? Then, you would have to set a suitable fixed-auto-range, where "suitable" really depends on the actual value. I don't know if what you are after is possibly without looking at the actual value.
Nisbo wrote: I did "RTFM" as you recommend ^^
http://www.jfree.org/jfreechart/api/jav ... ge-double-
but it says only: Sets the fixed auto range for the axis.
Right, this is not perfect documentation :)
But you can find what I meant here: http://www.jfree.org/jfreechart/api/jav ... l#line.258
Nisbo wrote: So any idea to get the plot in the middle ? Or bedder let the plot on the top (makes more sense) but in this case complete visible.

I tried
axisY.setUpperMargin(10);
This is something completely different and doesn't help you with your problem.
Nisbo wrote: Changing of the format works
NumberAxis axis = (NumberAxis)chart.getXYPlot().getRangeAxis();
axis.setNumberFormatOverride(new DecimalFormat("0"));
but in this case I have no decimal (1,23456) values if I zoom in or I have always decimal values if I am complete zoomed out
Because you specified a format-pattern without any fraction digits ("0"). Try new DecimalFormat("0.##") instead to see up to 2 fraction digits if necessary.

hth,
- martin

Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Re: Bug in Y Axis if I use AutoRange for X axis

Post by Nisbo » Tue Aug 16, 2016 12:10 pm

OK martin thx for your support, my solution for the moment is to change only the format

Code: Select all

NumberAxis axis = (NumberAxis)chart.getXYPlot().getRangeAxis();
axis.setNumberFormatOverride(new DecimalFormat("0.###"));
this will put the plot in the middle with the correct value

So if I have a value of 0
- than I have to lines above and 2 lines below the plot, the upper labelled with 0 and the lower with -0
If I have a value of 78
- than I have to lines above and 2 lines below the plot, the upper labelled with 78 and the lower also

thats not so beautiful (nice) but its OK, I was expecting this behaviour as I created the topic ^^
Image

The best solution would be
- if I have only 1 value that the value is on the top (and complete visible)
- if I have 2 or more different values the higher value should be on the top and the lower on the bottom


regarding
axisY.setFixedAutoRange(1);
I played around with it
if I start with a value of 445 at the beginning, the value will be displayed on the top, the lower value is 444
if the value decreases to 421 the plot is not visible any more
If you change the fixed-auto-range of the y-axis this should not effect the width of the plot (or the x-axis, i.e. the date) in any way. I'm not sure what you mean by "only the half width of the plot is visible".
I mean, if the Plot is set to a width of 2 Pixel, only 1 Pixel of the 2 Pixel line is visible (if the plot is on the top)

Here a screenshot
http://img5.fotos-hochladen.net/uploads ... zd3jqc.png
Image

How ever I dont want to bother you with my stupid problem ;)

matinh
Posts: 483
Joined: Fri Aug 11, 2006 10:08 am
Location: Austria

Re: Bug in Y Axis if I use AutoRange for X axis

Post by matinh » Tue Aug 16, 2016 1:19 pm

Nisbo wrote:regarding
axisY.setFixedAutoRange(1);
I played around with it
if I start with a value of 445 at the beginning, the value will be displayed on the top, the lower value is 444
if the value decreases to 421 the plot is not visible any more
We have a terminology problem here! In JFreeChart, a "plot" is the area where the axes and datalines will be drawn. It seems you refer to the data-line when saying "plot".

If the a chart has two data-points, one with y-value of 445 and one at 421 and y-auto-range is true, both values should be visible! If this is not the case, there is a bug, but I doubt, that there is a bug. This is a widely used feature of JFreeChart for years.
Nisbo wrote:
If you change the fixed-auto-range of the y-axis this should not effect the width of the plot (or the x-axis, i.e. the date) in any way. I'm not sure what you mean by "only the half width of the plot is visible".
I mean, if the Plot is set to a width of 2 Pixel, only 1 Pixel of the 2 Pixel line is visible (if the plot is on the top)
There is something strange here, but I can't tell from what I've seen from your code and I don't have the time, to further dig into this.

Hope I could still help you (at least a bit),
- martin

Nisbo
Posts: 10
Joined: Sun Aug 14, 2016 5:52 pm
antibot: No, of course not.

Re: Bug in Y Axis if I use AutoRange for X axis

Post by Nisbo » Tue Aug 16, 2016 1:35 pm

Hi Martin,

I see you are from Austria, I am from Germany, we are both able to speak German as Native Speaker ;)
How ever, thank you very much

If someone wants to have a look to the code

Code: Select all

TimeSeriesCollection dataset = new TimeSeriesCollection();
			for(TimeSeries s : series){
				dataset.addSeries(s);
			}
			
			final JFreeChart chart = ChartFactory.createTimeSeriesChart("" + value, "Time", "Value", dataset, true, true, false);
			
			
			final XYPlot plot  = chart.getXYPlot();
	        ValueAxis    axisX = plot.getDomainAxis();
	        ValueAxis    axisY = plot.getRangeAxis();
	        
	        axisY.setAutoRange(true);
	        NumberAxis axis = (NumberAxis)chart.getXYPlot().getRangeAxis();
        	axis.setNumberFormatOverride(new DecimalFormat("0.###"));
	        
                // this is the part I was "playing" around
	        if(dataset.getSeriesCount() == 1){
	        	//NumberAxis axis = (NumberAxis)chart.getXYPlot().getRangeAxis();
	        	//axis.setNumberFormatOverride(new DecimalFormat("0.###"));
	        	//axisY.setAutoTickUnitSelection(false);
	        	//axisY.setFixedAutoRange(1);
	        	//axisY.setUpperMargin(20);
	        	//
	        }
	        axisX.setFixedAutoRange(360000.0);      // 60000 = 60 seconds
	        
	        if(rdbtnmntmUseBlackDesign.isSelected()){
	        	chart.setBackgroundPaint(Color.BLACK); // BG Color of the Chart
				chart.getLegend().setBackgroundPaint(Color.BLACK); // BG color of the label Box
				chart.getXYPlot().setBackgroundPaint(Color.BLACK); // BG color of the grid
				axisX.setTickLabelPaint(Color.GREEN); // Y axis Text Color
				axisY.setTickLabelPaint(Color.GREEN); // Y axis Text Color
				
				// Title Color and BG Color
				Map<TextAttribute, Object> map = new Hashtable<TextAttribute, Object>();
				map.put(TextAttribute.BACKGROUND, Color.BLACK);
				map.put(TextAttribute.FOREGROUND, Color.GREEN);
				map.put(TextAttribute.SIZE, 18);
				//map.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
				TextTitle my_Chart_title=new TextTitle(value, new Font (map));
				chart.setTitle(my_Chart_title);
				
	        }
	        
			ChartPanel chartPanel = new ChartPanel(chart);
			           chartPanel.setPreferredSize(new Dimension(800, 550));
					   chartPanel.setEnforceFileExtensions(false);
					   chartPanel.setBounds(0, 0, 800, 550);
					   chartPanel.setVisible(true);
					   chartPanel.setMouseZoomable(true, false);
					  
			URL url = getClass().getClassLoader().getResource("com/sun/javafx/scene/control/skin/caspian/fxvk-capslock-button.png");
			ImageIcon icon = new ImageIcon(url);
			f.setFrameIcon(icon);
		
			f.getContentPane().removeAll();
			f.getContentPane().add(chartPanel);
			this.internalFramePanel.add(f);
			this.internalFramePanel.moveToFront(f);
			f.updateUI();

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

Re: Bug in Y Axis if I use AutoRange for X axis

Post by paradoxoff » Tue Aug 16, 2016 5:46 pm

I suggest to call setAutoRangeMinimumSize with a value around 1.0. This will extend an extremely small auto range and prevent identical tick labels from showing up
[Edit] Just for clarification: this method should be called on the range ("y") axis.

Locked