Histogram bars are shifted when x range is changed

TeeChart for Java (NetBeans, Eclipse, Android Studio, etc)
Post Reply
Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Histogram bars are shifted when x range is changed

Post by Jonathan » Wed Jul 21, 2010 4:29 pm

Hi,

I found a bug on a histogram plot. Whenever the range on bottom axis is changed manually, the entire histogram bars are shifted and misaligned with marks any longer.

In the following run as-is example, I set min and max value for the bottom axis. Then I see two problems/bugs.
1. Bars are shifted to left. eg. The x value for the 1st bar from left to right should be 10, but it looks like 0 on the chart. (BTW, marks are rendered correctly, though) That is, bars should be rendered correctly, aligned with its corresponding marks.
2. Always, the last bar width is way wider than the other bars.

I would appreciate if you can tell me how to fix them or if there is any workaround to resolve these issues.

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		TChart chart = new TChart();
		
		panel.add(chart);
		Bar b = new Bar(chart.getChart());
		b.add(10.0, 10.0);
		b.add(20.0, 20.0);
		b.add(30.0, 30.0);
		b.add(40.0, 40.0);
		b.add(50.0, 50.0);
		b.setMultiBar(MultiBars.STACKED);
		b.setBarWidthPercent(100);
		b.getMarks().setVisible(true);
		
		chart.getAxes().getBottom().getLabels().setStyle(AxisLabelStyle.VALUE);
		chart.getAxes().getBottom().setAutomatic(false);
		chart.getAxes().getBottom().setMinMax(-10, 150);

		//set 2D
		chart.getAspect().setView3D(false);
		
		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Yeray » Fri Jul 23, 2010 11:34 am

Hi Jonathan,

Try playing with the bar width:

Code: Select all

b.setCustomBarWidth(40);
If you comment the line b.setBarWidthPercent(100), you see that problem is that the second bar is drawn over the first, the third overlaps the second,... and so on. In fact all the bars have the same width, but only the last bar isn't overlapped.
This explains the two problems, the bars aren't displaced, but only a portion in their left is visible and they seem to be displaced. And the marks are in the middle of the bars, as you see in the last bar.
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: Histogram bars are shifted when x range is changed

Post by Jonathan » Fri Jul 23, 2010 3:21 pm

Hi Yeray,

1. I can't comment the line b.setBarWidthPercent(100) since my plot is HISTOGRAM, not pareto.

2. What you suggested doesn't work. Here is similar codes as above for run as-is. In this code, I changed BottomMax=60, setCustomBarWidth=10, and set BottomIncrement=10(Optional, but it makes sense since histogram bar has same barWidth) to see more problems clearly. If you run the code below, you will see the following problems.

Problem 1. the width of the last bar is always smaller than the other bars width.
Problem 2. all bars except the last one are displaced as you can see the marks are not in the middle of bars.
Problem 3. not this example plot, but I can see a problem with another histogram, where bar width is not integer, but double. The data in histogram plot are distorted by casting double bar width to integer in order to use setCustomBarWidth(int)

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		TChart chart = new TChart();
		final int INCREMENT = 10;
		
		panel.add(chart);
		Bar b = new Bar(chart.getChart());
		b.add(10, 10.0);
		b.add(20, 20.0);
		b.add(30, 30.0);
		b.add(40, 40.0);
		b.add(50, 50.0);
		
		b.setMultiBar(MultiBars.STACKED);
		b.setBarWidthPercent(100);
		b.getMarks().setVisible(true);
		
		chart.getAxes().getBottom().getLabels().setStyle(AxisLabelStyle.VALUE);
		chart.getAxes().getBottom().setAutomatic(false);
		chart.getAxes().getBottom().setMinMax(-10, 60);
		chart.getAxes().getBottom().setIncrement(INCREMENT);
		
		//set the bar width
		b.setCustomBarWidth(INCREMENT);
		
		//set 2D
		chart.getAspect().setView3D(false);
		
		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Yeray » Mon Jul 26, 2010 11:45 am

Hi Jonathan,

I see the labels aligned with their correct axis value. The problem is that, having barWidthPercent as 100, the right side of the each bar is changed and it moves to the next bar left side. That's why they look wrong.
test.png
test.png (16.54 KiB) Viewed 24369 times
What you could do to correct it would be setting a barWidth of the same size that the distance from axis value 10 and 20:

Code: Select all

        b.setCustomBarWidth(b.calcXPos(1) - b.calcXPos(0));
test2.png
test2.png (16.26 KiB) Viewed 24378 times
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: Histogram bars are shifted when x range is changed

Post by Jonathan » Wed Aug 11, 2010 10:02 pm

Hi Yeray,

Thanks for the help. However, I have an another issue.
Once I added a paint listener, it works only after I make a mouse click on chart. Why is this so? Did I miss something? Is there a way to refresh the chart after the chart is painted? In this way, i don't have to make a mouse click to see the correct bar width/location.

Code: Select all

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		JPanel panel = new JPanel();
		panel.setSize(600, 600);
		final TChart chart = new TChart();
		final int INCREMENT = 10;
		ChartPaintListener chartPaintListener;
		
		panel.add(chart);
		Bar b = new Bar(chart.getChart());
		b.add(10, 10.0);
		b.add(20, 20.0);
		b.add(30, 30.0);
		b.add(40, 40.0);
		b.add(50, 50.0);
		
		b.setMultiBar(MultiBars.STACKED);
		b.setBarWidthPercent(100);
		b.getMarks().setVisible(true);
		
		chart.getAxes().getBottom().getLabels().setStyle(AxisLabelStyle.VALUE);
		chart.getAxes().getBottom().setAutomatic(false);
		chart.getAxes().getBottom().setMinMax(-10, 150);
		chart.getAxes().getBottom().setIncrement(INCREMENT);
		
		//add paint listener
		chartPaintListener = new ChartPaintAdapter() {
			@Override
			public void chartPainted(ChartDrawEvent arg0) {
				System.out.println("chartPainted");
				super.chartPainted(arg0);
				resizeBarWidth();
			}
			
			private void resizeBarWidth(){
				for(int i=0; i<chart.getSeriesCount(); i++){
					Series series = chart.getSeries(i);
					if(series instanceof Bar){
						Bar b = (Bar) series;
						System.out.println("bar count: " + b.getCount());
						if(b.getCount()> 1){
							System.out.println("1st bar loc: " + b.calcXPos(0));
							System.out.println("2nd bar loc: " + b.calcXPos(1));
							b.setCustomBarWidth(b.calcXPos(1)-b.calcXPos(0));
						}
					}
				}
			} 
		};
		chart.addChartPaintListener(chartPaintListener);
		
		//set 2D
		chart.getAspect().setView3D(false);

		frame.add(panel);
		frame.setSize(600, 600);
		frame.setVisible(true);
	}

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Yeray » Wed Aug 18, 2010 9:45 am

Hi Jonathan,

I've tried several methods to force a chart repaint in Java but I'm afraid I couldn't find anyone that allows you to calculate the CustomBarWidth correctly before showing the Chart the first time.
I've added it to the wish list to be investigated for future releases (TJ71015094).
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Marc
Site Admin
Site Admin
Posts: 1272
Joined: Thu Oct 16, 2003 4:00 am
Location: Girona
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Marc » Thu Aug 19, 2010 2:19 pm

Hello,

The following codeline should force a repaint:

Code: Select all

chart1.getImage();
(where chart is class TChart)

Regards,
Marc Meumann
Steema Support

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: Histogram bars are shifted when x range is changed

Post by Jonathan » Thu Aug 19, 2010 2:40 pm

Hi Marc,

Where should i put that code you mentioned for forcing a repaint? If I put it within the listener, there is infinite loop.

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Yeray » Fri Aug 20, 2010 8:08 am

Hi Jonathan,

Have you tried putting it before the end of the main and after adding the paint listener?

On the other hand. Have you tried with Histogram Series?
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: Histogram bars are shifted when x range is changed

Post by Jonathan » Fri Aug 20, 2010 1:57 pm

Yeray,

Yes, I tried it both cases you mentioned, and it doesn't work. I haven't tried the Histogram series because teeChart supporter told me long time ago that I could use "setBarWidthPercent(100)" in order to draw a histogram chart.

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Yeray » Tue Aug 24, 2010 8:32 am

Hi Jonathan,

It's strange because the code you posted here works fine for me with NetBeans 6.9.1. What IDE do you use?
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: Histogram bars are shifted when x range is changed

Post by Jonathan » Tue Aug 24, 2010 2:56 pm

I am using Elicpse 3.5.1 (Galileo)

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Yeray » Wed Sep 08, 2010 10:12 am

Hi Jonathan,

Excuse us for the delay.
While we investigate the repaint issue, could you please explain us what problem did you have that made us suggest you the Bar series instead of Histogram?
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Jonathan
Newbie
Newbie
Posts: 60
Joined: Tue Mar 11, 2008 12:00 am
Location: Austin

Re: Histogram bars are shifted when x range is changed

Post by Jonathan » Wed Sep 08, 2010 7:36 pm

Yeray,

It's been a long time ago. Sorry I don't remember why the Bar series was used for histogram. I guess the Bar series was the basic framework that had been done.

Yeray
Site Admin
Site Admin
Posts: 9613
Joined: Tue Dec 05, 2006 12:00 am
Location: Girona, Catalonia
Contact:

Re: Histogram bars are shifted when x range is changed

Post by Yeray » Thu Sep 16, 2010 3:34 pm

Hi Jonathan,

Excuse us for the delay in this issue.
We've been investigating this and found that adding a line in the TChart.java (swt) and some more changes in TChart.java (Swing) this can be done in the ChartPaintListener event.
Find attached both revised files and here it is the testing application for eclipse (swt):

Code: Select all

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import com.steema.teechart.TChart;

public class Test {

	static TChart chart;
	static com.steema.teechart.styles.Bar b;	
    public static void main(String [] args){

        final Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        shell.setSize(600,400);       
    
        chart = new TChart(shell, 0);
                
        b = new com.steema.teechart.styles.Bar(chart.getChart());
        b.add(10, 10.0);
        b.add(20, 20.0);
        b.add(30, 30.0);
        b.add(40, 40.0);
        b.add(50, 50.0);
        
        b.setMultiBar(com.steema.teechart.styles.MultiBars.STACKED);
        b.getMarks().setVisible(true);
        
        chart.getAspect().setView3D(false);
        chart.getAxes().getBottom().getLabels().setStyle(com.steema.teechart.axis.AxisLabelStyle.VALUE);
        chart.getAxes().getBottom().setAutomatic(false);
        chart.getAxes().getBottom().setMinMax(-10, 150);
        chart.getAxes().getBottom().setIncrement(10);
        
        com.steema.teechart.events.ChartPaintListener chartPaintListener = new com.steema.teechart.events.ChartPaintAdapter() {
           @Override
           public void chartPainted(com.steema.teechart.events.ChartDrawEvent arg0) {
        	   b.setCustomBarWidth(b.calcXPos(1)-b.calcXPos(0));
           }
        };
        chart.addChartPaintListener(chartPaintListener);        
    
        shell.open();
        while (!shell.isDisposed()) {
        	if (!display.readAndDispatch()) {
        		display.sleep();
        	}
        }
        display.dispose();
    }
}
Attachments
TChart_Swing.zip
(7.57 KiB) Downloaded 916 times
TChart_SWT.zip
(7.17 KiB) Downloaded 1015 times
Best Regards,
ImageYeray Alonso
Development & Support
Steema Software
Av. Montilivi 33, 17003 Girona, Catalonia (SP)
Image Image Image Image Image Image Please read our Bug Fixing Policy

Post Reply