Chart View w/ Legend and Tool Tip Text

1 Answer 21 Views
ChartView
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
Mark asked on 16 Jan 2024, 07:22 PM

Is there a way to add Tool Tip Text to the legend items that get generated?  I have Tool Tip Text on the Series object, but I would like it on the Legend Items as well. 

Also, one other question, is there a way to customize how the legend shows the value next to the key?  Right now, our legand keys are "ABC 123".  I would like to add a "-" between the KEY and the COUNT value. 

 

TIA

 

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 19 Jan 2024, 09:15 AM

Hello, Mark,

Yes, you can add a tooltip to the legend items. You can use the ToolTipTextNeeded event and show the desired text when hovering legend items:

private void RadChartView1_ToolTipTextNeeded(object sender, ToolTipTextNeededEventArgs e)
{
    LegendItemTitle legendItem = sender as LegendItemTitle;
    if (legendItem != null)
    {
        e.ToolTipText = legendItem.Text;
    }
}

According to your second question, if I understand you correctly you would like to customize the legend item title text. Please refer to the following code snippet:

this.radChartView1.ChartElement.LegendElement.Items[0].Title = "ABC-123";

More information about legend in RadChartView is available here: Legend - ChartView - Telerik UI for WinForms 

I hope this helps. Should you have any other questions do not hesitate to contact me.

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 22 Jan 2024, 12:44 PM

For the second part, is there an event that is called when these LEGEND ITEMS are generated so I can update them as I need them. I looked for something, just didn't find it, and maybe I am looking in the wrong area.
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 22 Jan 2024, 12:49 PM

and the first part, while this gets me close, I actually want to use a different value, so I have no way to get to the DATAITEM it is related to....I will keep looking
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 22 Jan 2024, 01:36 PM

So, after digging around a little bit more and reading more in the help/docs link you provided, I am using the VisualItemCreating event to set the label and tooltip text as needed.  However, I am getting TEXT overlap and I am not sure why.

 

Here is my code


      private void HandleLegendElementVisualItemCreating(object sender, LegendItemElementCreatingEventArgs e)
      {
         e.ItemElement = new NtsLegendItemElement(e.LegendItem);
      }



Here is my class, similar to the class in the example.


namespace Nts.Suite
{
   public class NtsLegendItemElement : LegendItemElement
   {
      #region Public Constructors

      public NtsLegendItemElement(LegendItem item) : base(item)
      {
         if (item.Element is DataPointElement dataPointElement)
         {
            if (dataPointElement.DataPoint.DataItem is ChartResults myResults)
            {
               ToolTipText = myResults.ToolTipText;
               Text = $"{myResults.Category} ({myResults.Value:N0})";
            }
         }
      }

      #endregion Public Constructors

      #region Protected Methods

      protected override void Synchronize()
      {
         base.Synchronize();
         SyncVisualStyleProperties(LegendItem.Element, TitleElement);

      }

      #endregion Protected Methods
   }
}

 

This is my results

 

 

Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
commented on 22 Jan 2024, 02:16 PM

Okay, I think I have it figured out.   Took a little trial and error, but I got it.   

this is my updated CustomLegendItemElement class.  This works perfectly

 


 public class NtsLegendItemElement : LegendItemElement
   {
      #region Public Constructors

      public NtsLegendItemElement(LegendItem item) : base(item)
      {
         if (item.Element is DataPointElement dataPointElement)
         {
            if (dataPointElement.DataPoint.DataItem is ChartResults myResults)
            {
               TitleElement.ToolTipText = myResults.ToolTipText;
               item.Title = $"{myResults.Category} ({myResults.Value:N0})";
            }
         }
      }

Here are my results (shown with ToolTipText) :)

 

 

Nadya | Tech Support Engineer
Telerik team
commented on 25 Jan 2024, 06:45 AM

Hello, Mark,

It seems that you managed to solve the problem you had with text overlapping. I am glad that your tooltips and custom NtsLegendItemElement are working fine. In case you have any other difficulties do not hesitate to contact us.

Tags
ChartView
Asked by
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or