Alignment of NLegend item's text


https://www.nevron.com/Forum/Topic11970.aspx
Print Topic | Close Window

By Igor VASILIEV - 8 Years Ago
Hi guys,

I'm using version RSVision Vol1 2012 for VS2008.
I wanted the text on the legend to be left adjusted. On the picture it looked centered.
My idea was to iterate Data.Items property of NLegend object in RSMain method and set item's ContentAlignment propery = ContentAlignment.BottomLeft.
However I found that Data.Items has 0 items in it but chart and legend are rendered correctly.
Please advise how to achieve the desirable alignment.


https://www.nevron.com/forum/uploads/images/c0624493-ec98-4742-94a3-87e1.png
By Nevron Support - 8 Years Ago
Hi Igor,
In this case you need to use the series legend object that has a property called ContentAlignment - for example:
  NLineSeries line1 = new NLineSeries();
   line1.Legend.ContentAlignment = ContentAlignment.MiddleCenter;
   chart.Series.Add(line1);
   NLineSeries line2 = new NLineSeries();
   line2.Name = "Some Long Series Name";
   chart.Series.Add(line2);
An alternative approach is to use a legend in manual mode and specify the legend data items through code. DataItems has zero entries as the legend is populated after the chart is layout which isn't the case when the custom code is executed. Hope this helps - let us know if you meet any problems or have any questions.
By Igor VASILIEV - 8 Years Ago
Thank you for response.
I tried the first approach. I did this in RSMain code:
bar.Legend.ContentAlignment = ContentAlignment.BottomLeft;
Value was definitely assigned but visually alignment was not changed.
I want to try the second one - a legend in manual mode. Can you point me out to the code example or demo project?

By Igor VASILIEV - 8 Years Ago
Can you please give me some code examples how to do it using a legend in manual mode?
The first approach you suggested did not work.

By Nevron Support - 8 Years Ago
Hi Igor,

The following code shows how to add custom legend items in manual mode:

using System;
using System.Drawing;
using Nevron.GraphicsCore;
using Nevron.Chart;
using Nevron.ReportingServices;

namespace MyNamespace
{
 /// <summary>
 /// Sample class
 /// </summary>
 public class MyClass
 {
  /// <summary>
  /// Main entry point
  /// </summary>
  /// <param name="context"></param>
  public static void RSMain(NRSChartCodeContext context)
  {
   NLegend legend = context.document.Legends[0];
   
   legend.Mode = LegendMode.Manual;

   {
    NLegendItemCellData licd = new NLegendItemCellData();
    licd.MarkLineStyle.Width = new NLength(0);
    licd.MarkFillStyle = new NColorFillStyle(Color.Red);
    licd.ContentAlignment = ContentAlignment.MiddleLeft;
    licd.Text = "Left aligned text";
    legend.Data.Items.Add(licd);
   }

   {
    NLegendItemCellData licd = new NLegendItemCellData();
    licd.MarkLineStyle.Width = new NLength(0);
    licd.MarkFillStyle = new NColorFillStyle(Color.Green);
    licd.ContentAlignment = ContentAlignment.MiddleRight;
    licd.Text = "Right aligned text";
    legend.Data.Items.Add(licd);
   }

   {
    NLegendItemCellData licd = new NLegendItemCellData();
    licd.MarkLineStyle.Width = new NLength(0);
    licd.MarkFillStyle = new NColorFillStyle(Color.Blue);
    licd.ContentAlignment = ContentAlignment.MiddleCenter;
    licd.Text = "Some long text in the middle";
    legend.Data.Items.Add(licd);
   }
  }
 }
}

Hope this helps - let us know if you meet any problems or have any questions.
By rash Pisal - 6 Years Ago

I'm using nevron 2011. I cant access  NLegendItemCellData licd = new NLegendItemCellData();

By Nevron Support - 6 Years Ago
Hi Rash,
Please consider to upgrade to the current release. We no longer issue support for 2011.