This is a migrated thread and some comments may be shown as answers.

Dynamic Legends

9 Answers 331 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Bryan Doan
Top achievements
Rank 1
Bryan Doan asked on 01 Nov 2010, 03:49 PM
Hello everyone,

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

Sort by
0
Peter
Telerik team
answered on 04 Nov 2010, 01:53 PM
Hello Bryan Doan,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Bryan Doan
Top achievements
Rank 1
answered on 04 Nov 2010, 02:04 PM
Hi Peter,

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!

0
Peter
Telerik team
answered on 08 Nov 2010, 06:37 PM
Hi Bryan Doan,

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,});
}

Kind regards,
Peter
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Bryan Doan
Top achievements
Rank 1
answered on 08 Nov 2010, 10:50 PM
Hi Peter,

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,});

        }

)

0
Peter
Telerik team
answered on 11 Nov 2010, 08:40 AM
Hi Bryan,

Your code looks correct - check out the attached video that shows the provided code in action.

All the best,
Peter
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Bryan Doan
Top achievements
Rank 1
answered on 11 Nov 2010, 10:44 AM
Hi Peter...

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
0
Peter
Telerik team
answered on 16 Nov 2010, 03:47 PM
Hi Bryan Doan,

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
See What's New in Telerik Reporting in Q3 2010 on Wednesday, November 17, 11am Eastern Time: Register here>>
0
Bryan Doan
Top achievements
Rank 1
answered on 17 Nov 2010, 11:11 AM
Hi Peter,

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,});
}

 

 

 

0
Accepted
Peter
Telerik team
answered on 23 Nov 2010, 09:01 AM
Hello Bryan Doan,

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;

Greetings,
Peter
the Telerik team
Get started with Telerik Reporting with numerous videos and detailed documentation.
Tags
General Discussions
Asked by
Bryan Doan
Top achievements
Rank 1
Answers by
Peter
Telerik team
Bryan Doan
Top achievements
Rank 1
Share this question
or