Legend with Alignment = Button not centering

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
tms4000
Newbie
Newbie
Posts: 17
Joined: Mon Jun 02, 2014 12:00 am

Legend with Alignment = Button not centering

Post by tms4000 » Mon Jun 09, 2014 5:26 pm

I'm using the latest version of TeeChart .Net on windows CE using the compact framework.

I am using a bottom aligned legend like so:
chartTank.Legend.Alignment = Steema.TeeChart.LegendAlignments.Bottom;

I expected the legen to be centered horizontally, but that isn't the case. See Below:

Image

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Legend with Alignment = Button not centering

Post by Christopher » Tue Jun 10, 2014 10:19 am

Hello,

This issue has already been added to our bugs database and has been fixed.

In the meantime, you can use custom positioning to rectify the issue:

Code: Select all

    private void InitializeChart()
    {
      Random rnd = new Random();

      tChart1.Aspect.View3D = false;

      for (int j = 0; j < 2; j++)
      {
        var barProduct = new Steema.TeeChart.Styles.Bar();

        for (int i = 0; i < 10; i++)
        {
          barProduct.Add(rnd.Next(0, 100));
        }

        tChart1.Series.Add(barProduct);
      }

      tChart1.Legend.Alignment = LegendAlignments.Bottom;
      tChart1.Legend.CustomPosition = true;
      tChart1.Legend.Left = Utils.Round(tChart1.Width / 2.5);
      tChart1.Legend.Top = tChart1.Height - 40;
      tChart1.Panel.MarginBottom = 10;
    }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

tms4000
Newbie
Newbie
Posts: 17
Joined: Mon Jun 02, 2014 12:00 am

Re: Legend with Alignment = Button not centering

Post by tms4000 » Wed Jun 11, 2014 2:20 pm

Is there any way to Remove the box around the legend?

In a perfect world, I would like to position the legend like so
Image

Is that possible?

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Legend with Alignment = Button not centering

Post by Christopher » Wed Jun 11, 2014 2:53 pm

tms4000 wrote:Is there any way to Remove the box around the legend?

In a perfect world, I would like to position the legend like so
Image

Is that possible?
Possibly the easiest thing to do here is to draw your own "legend", e.g.

Code: Select all

    private void InitializeChart()
    {
      Random rnd = new Random();

      tChart1.Aspect.View3D = false;

      for (int j = 0; j < 2; j++)
      {
        var barProduct = new Steema.TeeChart.Styles.Bar();

        for (int i = 0; i < 10; i++)
        {
          barProduct.Add(rnd.Next(0, 100));
        }

        tChart1.Series.Add(barProduct);
      }

      tChart1.Legend.Visible = false;

      tChart1.Panel.MarginBottom = 10;
      tChart1.AfterDraw +=tChart1_AfterDraw;
    }

    void tChart1_AfterDraw(object sender, Graphics3D g)
    {
      DrawMyLegend(g, tChart1.Axes.Left.Position, tChart1.Axes.Bottom.Position + 30, tChart1[0]);
      DrawMyLegend(g, tChart1.Axes.Left.Position + g.Chart.ChartRect.Width - 50, tChart1.Axes.Bottom.Position + 30, tChart1[1]);
    }

    private void DrawMyLegend(Graphics3D g, int left, int top, Series series)
    {
      g.Brush.Color = series.Color;
      g.Rectangle(Utils.FromLTRB(left, top, left + 10, top + 10));
      g.TextOut(left + 15, top - 2, series.Title);
    }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

tms4000
Newbie
Newbie
Posts: 17
Joined: Mon Jun 02, 2014 12:00 am

Re: Legend with Alignment = Button not centering

Post by tms4000 » Wed Jun 11, 2014 3:32 pm

Can I remove the box around it without resorting to drawing the legend myself?

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Legend with Alignment = Button not centering

Post by Christopher » Wed Jun 11, 2014 4:11 pm

tms4000 wrote:Can I remove the box around it without resorting to drawing the legend myself?
To do that you need:

Code: Select all

      tChart1.Legend.Transparent = true;
which you could use in addition to e.g.

Code: Select all

      tChart1.Legend.ColumnWidthAuto = false;
      tChart1.Legend.ColumnWidths = new int[2] { 10, 300 };
      tChart1.Legend.Symbol.Width = 5;
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

tms4000
Newbie
Newbie
Posts: 17
Joined: Mon Jun 02, 2014 12:00 am

Re: Legend with Alignment = Button not centering

Post by tms4000 » Thu Jun 12, 2014 2:04 am

I'm using the following code on setup:


chartTank.Legend.Bevel.Outer = Steema.TeeChart.Drawing.BevelStyles.None;
chartTank.Legend.Alignment = Steema.TeeChart.LegendAlignments.Bottom;
chartTank.Legend.CustomPosition = true;
chartTank.Legend.Top = chartTank.Height - 68;
chartTank.Panel.MarginBottom = 15;

And depending on certain conditions, I'm changing the series that are visible. After I alter the series visibility, I try to reposition the legend with the following code:
chartTank.Legend.Left = Convert.ToInt32((chartTank.Width - chartTank.Legend.Width) / 2.0);

However it seems like the chartTank.Legend.Width isn't recalculated until after this line, therefore the legend get positioned based on the previous legend width.

Is there anyway to force the legend to recalculate it's width before I set it's position?

Narcís
Site Admin
Site Admin
Posts: 14730
Joined: Mon Jun 09, 2003 4:00 am
Location: Banyoles, Catalonia
Contact:

Re: Legend with Alignment = Button not centering

Post by Narcís » Thu Jun 12, 2014 1:50 pm

Hello,
tms4000 wrote: Is there anyway to force the legend to recalculate it's width before I set it's position?
Try calling TChart.Draw() method before this line, for example:

Code: Select all

      chartTank.Draw();
      chartTank.Legend.Left = Convert.ToInt32((chartTank.Width - chartTank.Legend.Width) / 2.0);
You may also be interested in using the GetLegendRect event, where you can change its dimensions using the e.Rectangle argument, for example:

Code: Select all

    void tChart1_GetLegendRect(object sender, Steema.TeeChart.GetLegendRectEventArgs e)
    {
      Rectangle rect = e.Rectangle;
      rect.Width /= 2;

      e.Rectangle = rect;
    }
Best Regards,
Narcís Calvet / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply