Modify font of ChartView legend after the series has been created

1 Answer 92 Views
ChartView
Toby
Top achievements
Rank 3
Iron
Iron
Iron
Toby asked on 28 Jul 2023, 12:53 PM

In my aplication I create a graph with lots of scatter line series. Once created I would like to alter the format of one specific item in the legend to show this in bold fold (or possibly highlight in a different colour).

How is this possible to achieve?

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 31 Jul 2023, 05:13 PM

Hi Toby,

What you can try is to subscribe to the this.radChartView1.ChartElement.LegendElement.VisualItemCreating event of the control. In the event handler, you can create a new LegendItemElement and modify the Font and FontColor of the TitleElement per your requirement. Here is what I have in mind. First you can see how I have set-up the chart:

public Form1()
{
    InitializeComponent();
    this.radChartView1.ChartElement.LegendElement.VisualItemCreating += LegendElement_VisualItemCreating;
    BarSeries barSeries = new BarSeries("Performance", "RepresentativeName");
    barSeries.LegendTitle = "Q1";
    barSeries.DataPoints.Add(new CategoricalDataPoint(177, "Harley"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(128, "White"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(143, "Smith"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(111, "Jones"));
    barSeries.DataPoints.Add(new CategoricalDataPoint(118, "Marshall"));
    this.radChartView1.Series.Add(barSeries);
    BarSeries barSeries2 = new BarSeries("Performance", "RepresentativeName");
    barSeries2.LegendTitle = "Q2";
    barSeries2.DataPoints.Add(new CategoricalDataPoint(153, "Harley"));
    barSeries2.DataPoints.Add(new CategoricalDataPoint(141, "White"));
    barSeries2.DataPoints.Add(new CategoricalDataPoint(130, "Smith"));
    barSeries2.DataPoints.Add(new CategoricalDataPoint(88, "Jones"));
    barSeries2.DataPoints.Add(new CategoricalDataPoint(109, "Marshall"));
    this.radChartView1.Series.Add(barSeries2);

    this.radChartView1.ShowLegend = true;
}

And now the event handler:

private void LegendElement_VisualItemCreating(object sender, LegendItemElementCreatingEventArgs e)
{
    e.ItemElement = new LegendItemElement(e.LegendItem);
    e.ItemElement.TitleElement.Font = new Font(e.ItemElement.TitleElement.Font.FontFamily, 14, FontStyle.Bold);
    e.ItemElement.TitleElement.ForeColor = Color.Red;
}

Here is the result:

private void LegendElement_VisualItemCreating(object sender, LegendItemElementCreatingEventArgs e)
{
    if (e.LegendItem.Title == "Q1")
    {
        e.ItemElement = new LegendItemElement(e.LegendItem);
        e.ItemElement.TitleElement.Font = new Font(e.ItemElement.TitleElement.Font.FontFamily, 14, FontStyle.Bold);
        e.ItemElement.TitleElement.ForeColor = Color.Red;
    }
}

You can see that I am modifying a specific text:

You can use the code and extend it to cover your requirements. Give it a try and let me know if further assistance is required.

Regards,
Dinko | 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.

Toby
Top achievements
Rank 3
Iron
Iron
Iron
commented on 01 Aug 2023, 11:36 AM | edited

Thank you for your suggestion, I have tried your solution but it would appear that the event handler is called when adding each data series and not when ShowLegend is set to true.

At this point I do not know which one to highlight, so cannot hard code this in the event handler.

Can I modify a specific legend title element after it has been created ?

 

Dinko | Tech Support Engineer
Telerik team
commented on 02 Aug 2023, 06:49 AM

Yes, you can. You can use the StackElement property of the LegendElement to access the LegendItemElements.

private void radButton1_Click(object sender, EventArgs e)
{
    var test = this.radChartView1.ChartElement.LegendElement.Items[1];
    var legendElement = (this.radChartView1.ChartElement.LegendElement.StackElement.Children[1] as LegendItemElement);
    legendElement.TitleElement.Font = new Font(legendElement.TitleElement.Font.FontFamily, 14, FontStyle.Bold);
}

Toby
Top achievements
Rank 3
Iron
Iron
Iron
commented on 03 Aug 2023, 06:04 PM

Hi Dinko, 

Thanks for example code, I cannot get this code to run, it throws an exception.

Your var "test" is not used in your example, and when I run in the debugger, StackElement has no children, hence the exception

Any ideas?

 

 

Dinko | Tech Support Engineer
Telerik team
commented on 04 Aug 2023, 07:40 AM

It is strange that the code leads to an exception in your application as I am unable to observe this on my side. I am attaching my sample project so you can test it on your side. The project contains one RadChartView with two series. There are 2 items In the legend container. The second one is customized in the VisualItemCreating event and the first one will be customized when you press the button. You can check if this exception is raised when you press the button. If not, can you share how my set-up is different from yours. 
Toby
Top achievements
Rank 3
Iron
Iron
Iron
commented on 04 Aug 2023, 10:21 AM

 

mmm, after investigation I have found that the issue arises because I am changing the legend layout as follows:

radChartTop.ChartElement.LegendItemsLayout = LegendItemsLayout.Wrap;
radChartTop.ChartElement.LegendElement.WrapPanelElement.Orientation = Orientation.Horizontal;

The following code now works successfully

if ( radChartTop.ChartElement.LegendElement.WrapPanelElement.Children [ 0 ] is LegendItemElement legendElement )
{
  legendElement.TitleElement.Font = new Font ( legendElement.TitleElement.Font.FontFamily, 20, FontStyle.Bold );
  legendElement.TitleElement.ForeColor = Color.Blue;
}

Thanks for your help
Toby

Tags
ChartView
Asked by
Toby
Top achievements
Rank 3
Iron
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or