Getting the bar width

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
qcrnd
Advanced
Posts: 214
Joined: Mon Sep 04, 2006 12:00 am

Getting the bar width

Post by qcrnd » Mon Aug 16, 2010 1:18 pm

Hi,

I'm trying to get the bar width in order to decide how wide to draw the bar outline (if the bar is too thin then I'd like to set the outline width to 0).
As I understand, the chart calculates the bar width to fit the chart width.
Is there a way to get the actual bar width (i.e. the width that will be drawn eventually). I've tried to use bar.BarBounds.Width or bar.CustomBarWidth but these are always 0.

Thanks in advance
Itai

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

Re: Getting the bar width

Post by Yeray » Mon Aug 16, 2010 2:31 pm

Hi Itai,

You have to retrieve this information once the chart has been drawn.
The following seems to work fine for me here:

Code: Select all

        Steema.TeeChart.Styles.Bar bar1;
        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;

            bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
            bar1.FillSampleValues();

            tChart1.Draw();
            tChart1.Header.Text = bar1.BarBounds.Width.ToString();
        }
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

qcrnd
Advanced
Posts: 214
Joined: Mon Sep 04, 2006 12:00 am

Re: Getting the bar width

Post by qcrnd » Sun Aug 29, 2010 2:39 pm

Hi Yeray,

Perhaps I should elaborate on my problem a bit more.
Usualy, I'd like to draw the bars with a rather thick outline so I initialize my bars with:

Code: Select all

 bar.Pen.Width = 2;
The problem is that sometimes my chart contains a lot of bars and so each bar is very thin. As a result the user can only see the outlines and not the bars themselves. Instead of seeing different bars with different colors the user sees all the bars in white (the outline color). You can see this in the image below: (Note that the legend shows various series with various colors.)
ScreenHunter_01 Aug. 29 16.39.jpg
ScreenHunter_01 Aug. 29 16.39.jpg (92.54 KiB) Viewed 11703 times
In such a case I'd like to reduce the outline width, something like:

Code: Select all

bar.Pen.Width = 0;
If I change the width of the bar outline after draw, it only takes effect in the next draw (e.g. if I minimize the window and restore it again).
I'd rather not draw the chart twice (first time just to get the bars' width and second time to draw the chart with the correct border width) as this causes flickering and also because I'm afraid that draw may be an expensive opperation in certain situations.

My qustion is, is there a way to retrieve the bar's width and set the outline width according to it before draw? or pehaps there's a way to set the outline width to be relative to the bar's width?

Thanks,
Itai

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

Re: Getting the bar width

Post by Yeray » Tue Aug 31, 2010 8:46 am

Hi qcrnd,

You could force the chart to be drawn and show or hide the pen depending on the bars width. For example:

Code: Select all

            tChart1.Draw();
            if (sender.BarBounds.Width < 5)
                sender.Pen.Visible = false;
            else
                sender.Pen.Visible = true;
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

qcrnd
Advanced
Posts: 214
Joined: Mon Sep 04, 2006 12:00 am

Re: Getting the bar width

Post by qcrnd » Tue Aug 31, 2010 10:53 am

Hi Yeray,

I think you didn't understand. If I change the pen AFTER draw then this will be visible only in the next draw. I don't want to draw the chart twice because this causes flickering (i.e. the user can see the chart being drawn once with thick bar outlines and then drawn again with thin bar outlines) and also because it can take a long time to draw the chart.

What I would like is to be able to set the bar.Pen.Width according to what bar.BarBounds.Width is going to be without drawing the chart twice. I understand that the final bar width is being calculated only in draw. I was hoping that there's some way to know the bar width BEFORE or WHILE draw and set the border width accordingly. Perhaps some callback after the bar width has been calculated and before the chart has been drawn.

Is there any such way to set bar.Pen.Width according to bar.BarBounds.Width without drawing the chart twice?

Thanks,
Itai

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

Re: Getting the bar width

Post by Yeray » Wed Sep 01, 2010 12:50 pm

Hi Itai,

Doing it at GetBarStyle event, you'll check it after drawing each bar, so it should be ok for all the bars except for the first one:

Code: Select all

        Steema.TeeChart.Styles.Bar bar1;
        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;

            bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
            bar1.FillSampleValues(200);
            bar1.Marks.Visible = false;
            bar1.Pen.Width = 2;
            bar1.Pen.Color = Color.White;

            tChart1.Axes.Bottom.SetMinMax(-5, 300);

            bar1.GetBarStyle += new Steema.TeeChart.Styles.CustomBar.GetBarStyleEventHandler(bar1_GetBarStyle);
        }

        void bar1_GetBarStyle(Steema.TeeChart.Styles.CustomBar sender, Steema.TeeChart.Styles.CustomBar.GetBarStyleEventArgs e)
        {
            if (sender.BarBounds.Width < 5)
                sender.Pen.Visible = false;
            else
                sender.Pen.Visible = true;
        }
Another option would be calculating the widths that the bars will have manually before they'll be drawn, known the size of the chart:

Code: Select all

        Steema.TeeChart.Styles.Bar bar1;
        private void InitializeChart()
        {
            tChart1.Aspect.View3D = false;

            bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
            bar1.FillSampleValues(200);
            bar1.Marks.Visible = false;
            bar1.Pen.Width = 2;
            bar1.Pen.Color = Color.White;

            tChart1.Axes.Bottom.SetMinMax(-5, 300);

            if ((tChart1.ClientRectangle.Width / (tChart1.Axes.Bottom.Maximum - tChart1.Axes.Bottom.Minimum)) < 5)
                bar1.Pen.Visible = false;
        }
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