Does anybody know if there is a way to change the legend dynamically for graphs that created mainly from Report Designer? I created graphs by using Telerik report designer but would like to be able to change the legends based on series that users select...
I know we can do if graphs are created programmatically, but I already used report designer to create these reports and this dynamic legend is the requirement... It would be a pain to go back and redo the whole thing...
Thanks for sharing!
9 Answers, 1 is accepted
In the following code snippet we illustrated how to add legend items programmatically. You can add legend items within the report's constructor after the InitializeComponent() or within the chart's ItemDataBinding event.
Telerik.Reporting.Charting.LabelItem labelItem1 = new Telerik.Reporting.Charting.LabelItem();
Telerik.Reporting.Charting.LabelItem labelItem2 = new Telerik.Reporting.Charting.LabelItem();
labelItem1.Marker.Visible = true;
labelItem1.Name = "LegendItem1";
labelItem1.TextBlock.Text = "Text1";
labelItem2.Marker.Visible = true;
labelItem2.Name = "LegendItem2";
labelItem2.TextBlock.Text = "Text2";
this.chart1.Legend.Items.AddRange(new Telerik.Reporting.Charting.LabelItem[] {
labelItem1,
labelItem2});
Just to note that the current series legend can be changed from the series1 --> Appearance -->LegendDisplayMode property.
Sincerely yours,Peter
the Telerik team
Thank you for the response! What I wanted to do with the legend is to have the name display dynamically. So from your code, is there an easy way to have the "LegendItem1" in the code labelItem1.Name = "LegendItem1" changed base on user select? So if users select to run data for 2010, the LegendItem1 is 2010 but if users select to run for 2007 then the LegendItem1 is 2007. I can't hardcode...
I created by using the report designer not totally programtically but would like to be able to change the legend name/text base on user selects. Is that possible?
Thank You!
You can set the legends item text depending on the customer's report parameter selection as illustrated in the following code snippet:
private void chart1_ItemDataBinding(object sender, EventArgs e)
{
var procChart = ((Telerik.Reporting.Processing.Chart)sender);
Telerik.Reporting.Charting.LabelItem labelItem1 = new Telerik.Reporting.Charting.LabelItem();
labelItem1.Marker.Visible = true;
labelItem1.Name = "LegendItem1";
labelItem1.TextBlock.Text = procChart.Report.Parameters["Parameter1"].Value.ToString();
this.chart1.Legend.Items.AddRange(new Telerik.Reporting.Charting.LabelItem[] {
labelItem1,});
}
Peter
the Telerik team
Thanks for the response! Hope this will be the last question on this Legend stuff... So if I have the code as below, where do I call the chart1_ItemDataBinding to show my legend?
Again, I used report designer to create the chart... Thanks!
public partial class Report1 : Telerik.Reporting.Report
{
public Report1()
{
//
// Required for telerik Reporting designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
private void chart1_ItemDataBinding(object sender, EventArgs e)
{
var procChart = ((Telerik.Reporting.Processing.Chart)sender);
Telerik.Reporting.Charting.LabelItem labelItem1 = new Telerik.Reporting.Charting.LabelItem();
labelItem1.Marker.Visible = true;
labelItem1.Name = "LegendItem1";
labelItem1.TextBlock.Text = procChart.Report.Parameters["Year"].Value.ToString();
this.chart1.Legend.Items.AddRange(new Telerik.Reporting.Charting.LabelItem[] {labelItem1,});
}
)
Your code looks correct - check out the attached video that shows the provided code in action.
All the best,Peter
the Telerik team
Thanks so much! Would it be possible to have the sample that you did in the video attached here as well?
The legend that I'm trying to show contains two series( data from two columns), if I can get for one I should be able to get the other. So if users select 2009, my legend will show 2009 and 2008 for the two series/columns( the current selection and one year prior) on the graph.
BTW, do you have a sample fully working graph/solution that are created programmatically that I can download as an example? I would like an example of a graph/chart that is created totally programmatically to use as an example if I need to rewrite my current graphs.
Thanks!
-Bryan
I have attached a sample project to illustrate both of the approaches. Check it out and let us know how it goes.
Sincerely yours,Peter
the Telerik team
Thank so much... Now I got the legend items to appear the way I wanted... Is there a generic way to do for the last two lines at the bottom of the method as shown below where I don't have to specify "this.chart1."? I have lots of graphs in my report, so dont want to do that for each of the graphs.
Also, the background color of the item figures are not matching to the series' when I specify the same color for both. Do you know why? Is there a way to fix this?
Thanks!
private void chart1_ItemDataBinding(object sender, EventArgs e)
{
var procChart = ((Telerik.Reporting.Processing.Chart)sender);
Telerik.Reporting.Charting.
LabelItem labelItem1 = new Telerik.Reporting.Charting.LabelItem();
//labelItem1.Marker.Visible = true;
//labelItem1.Name = "LegendItem1";
var year = procChart.Report.Parameters["Parameter1"].Value;
labelItem1.TextBlock.Text = year.ToString();
labelItem1.Marker.Appearance.Figure =
"rectangle";
labelItem1.Marker.Appearance.FillStyle.MainColor = System.Drawing.
Color.Maroon;
labelItem1.Marker.Appearance.Dimensions.Height =
new Telerik.Reporting.Charting.Styles.Unit(2, Telerik.Reporting.Charting.Styles.UnitType.Pixel);
this.chart1.Legend.Clear();
this.chart1.Legend.Items.AddRange(new Telerik.Reporting.Charting.LabelItem[] {labelItem1,labelItem2,labelItem3,});
}
By default the labels FillType is Gradient. In order to have a consistent color our suggestion is to set it to Solid as shown in the following code snippet:
labelItem1.Marker.Appearance.FillStyle.FillType = Telerik.Reporting.Charting.Styles.FillType.Solid;
Peter
the Telerik team