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
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.
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 ?
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);
}
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?
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