The Chart control automatically creates a legend item for each series added to a chart at design time and sets the Legend property for each series by default. However, the legend's Visible property must be set to True for getting it displayed with the chart. The text for legend caption is taken from the Name property on the series.
Note: Each Series displayed in the Legend must have a Name. If the Name property is not set, the Series does not show up in the Legend.
The following code demonstrates how to create a legend at run time, add it to the Legends collection of the Chart object and set the legend property of the series to the new legend, resulting in the legend shown above.
To write code in Visual Basic.NET
Visual Basic code. Paste INSIDE the section Format event |
Copy Code
|
' create the legend and title for the legend
Dim legend1 As New GrapeCity.ActiveReports.Chart.Legend
Dim lHeader As New GrapeCity.ActiveReports.Chart.Title
' set the properties for the legend title
lHeader.Backdrop = New GrapeCity.ActiveReports.Chart.Graphics.Backdrop(Chart.Graphics.BackdropStyle.Transparent, _Color.White, Color.White, Chart.Graphics.GradientType.Vertical, Drawing2D.HatchStyle.DottedGrid, Nothing, _Chart.Graphics.PicturePutStyle.Stretched)
lHeader.Border = New GrapeCity.ActiveReports.Chart.Border(New Chart.Graphics.Line(Color.White, 2, _Chart.Graphics.LineStyle.None), 0, Color.Black)
lHeader.Font = New GrapeCity.ActiveReports.Chart.FontInfo (Color.White, New System.Drawing.Font("Arial", 10.0F, _FontStyle.Bold))
lHeader.Text = "Series:"
' set the properties for the legend and add it to the legends collection
legend1.Alignment = GrapeCity.ActiveReports.Chart.Alignment.TopRight
legend1.Backdrop = New GrapeCity.ActiveReports.Chart.BackdropItem(Chart.Graphics.BackdropStyle.Transparent, _Color.Gray, Color.White, Chart.Graphics.GradientType.Vertical, Drawing2D.HatchStyle.DottedGrid, Nothing, _Chart.Graphics.PicturePutStyle.Stretched)
legend1.Border = New GrapeCity.ActiveReports.Chart.Border(New Chart.Graphics.Line(Color.Navy, 2), _0, Color.Black)
legend1.DockArea = Me.ChartControl1.ChartAreas(0)
legend1.LabelsFont = New GrapeCity.ActiveReports.Chart.FontInfo(Color.White, New System.Drawing.Font("Arial", 9.0F)) legend1.Header = lHeader
legend1.MarginX = 5
legend1.MarginY = 5
Me.ChartControl1.Legends.Add(legend1)
' Generate legend items
Dim legenditem As New GrapeCity.ActiveReports.Chart.LegendItem
Dim legenditem2 As New GrapeCity.ActiveReports.Chart.LegendItem
' Set properties of legend item
legenditem.Text = "High Range"
legenditem2.Text = "Low Range"
legenditem.Marker.Style = Chart.MarkerStyle.None
legenditem2.Marker.Style = Chart.MarkerStyle.None
legenditem.Border.Line.Style = Chart.Graphics.LineStyle.None
legenditem2.Border.Line.Style = Chart.Graphics.LineStyle.None
legenditem.Backdrop = New GrapeCity.ActiveReports.Chart.BackdropItem (System.Drawing.Color.LightSteelBlue, Chart.Graphics.AntiAliasMode.Auto)
legenditem2.Backdrop = New GrapeCity.ActiveReports.Chart.BackdropItem (System.Drawing.Color.LightGray, Chart.Graphics.AntiAliasMode.Auto)
' Add to legend collection
legend1.LegendItems.Add(legenditem)
legend1.LegendItems.Add(legenditem2)
' Set the legend property of the series to the legend you created
Me.ChartControl1.Series(0).Legend = legend1
Me.ChartControl1.Series(1).Legend = legend1
Me.ChartControl1.Series(2).Legend = legend1
|
To write code in C#
C# code. Paste INSIDE the section Format event |
Copy Code
|
// create the legend and title for the legend
GrapeCity.ActiveReports.Chart.Legend legend1 = new GrapeCity.ActiveReports.Chart.Legend();
GrapeCity.ActiveReports.Chart.Title lHeader = new GrapeCity.ActiveReports.Chart.Title();
// set the properties for the legend title
lHeader.Backdrop = new GrapeCity.ActiveReports.Chart.Graphics.Backdrop (GrapeCity.ActiveReports.Chart.Graphics.BackdropStyle.Transparent,System.Drawing.Color.White, System.Drawing.Color.White, GrapeCity.ActiveReports.Chart.Graphics.GradientType.Vertical,
System.Drawing.Drawing2D.HatchStyle.DottedGrid, null, GrapeCity.ActiveReports.Chart.Graphics. PicturePutStyle.Stretched);
lHeader.Border = new GrapeCity.ActiveReports.Chart.Border(new GrapeCity.ActiveReports.Chart.Graphics.Line (System.Drawing.Color.White, 2, GrapeCity.ActiveReports.Chart.Graphics.LineStyle.None), 0, System.Drawing.Color.Black);
lHeader.Font = new GrapeCity.ActiveReports.Chart.FontInfo(System.Drawing.Color.White,
new System.Drawing.Font("Arial", 10.0F, System.Drawing.FontStyle.Bold));
lHeader.Text = "Series:";
// set the properties for the legend and add it to the legends collection.
legend1.Alignment = GrapeCity.ActiveReports.Chart.Alignment.TopRight;
legend1.Backdrop = new GrapeCity.ActiveReports.Chart.BackdropItem (GrapeCity.ActiveReports.Chart.Graphics.BackdropStyle.Transparent, System.Drawing.Color.Gray,
System.Drawing.Color.White, GrapeCity.ActiveReports.Chart.Graphics.GradientType.Vertical,
System.Drawing.Drawing2D.HatchStyle.DottedGrid, null, GrapeCity.ActiveReports.Chart.Graphics. PicturePutStyle.Stretched);
legend1.Border = new GrapeCity.ActiveReports.Chart.Border (new GrapeCity.ActiveReports.Chart.Graphics.Line(System.Drawing.Color.Navy, 2),0, System.Drawing.Color.Black);
legend1.DockArea = this.ChartControl1.ChartAreas[0];
legend1.LabelsFont = new GrapeCity.ActiveReports.Chart.FontInfo (System.Drawing.Color.White, new System.Drawing.Font("Arial", 9F));
legend1.Header = lHeader;
legend1.MarginX = 5;
legend1.MarginY = 5;
this.ChartControl1.Legends.Add(legend1);
// Generate legend items
GrapeCity.ActiveReports.Chart.LegendItem legenditem = new GrapeCity.ActiveReports.Chart.LegendItem();
GrapeCity.ActiveReports.Chart.LegendItem legenditem2 = new GrapeCity.ActiveReports.Chart.LegendItem();
// Set properties of legend item
legenditem.Text = "High Range";
legenditem2.Text = "Low Range";
legenditem.Marker.Style = GrapeCity.ActiveReports.Chart.MarkerStyle.None;
legenditem2.Marker.Style = GrapeCity.ActiveReports.Chart.MarkerStyle.None;
legenditem.Border.Line.Style = GrapeCity.ActiveReports.Chart.Graphics.LineStyle.None;
legenditem2.Border.Line.Style = GrapeCity.ActiveReports.Chart.Graphics.LineStyle.None;
legenditem.Backdrop = new GrapeCity.ActiveReports.Chart.BackdropItem (System.Drawing.Color.LightSteelBlue, GrapeCity.ActiveReports.Chart.Graphics.AntiAliasMode.Auto);
legenditem2.Backdrop = new GrapeCity.ActiveReports.Chart.BackdropItem (System.Drawing.Color.LightGray, GrapeCity.ActiveReports.Chart.Graphics.AntiAliasMode.Auto);
// Add to legend collection
legend1.LegendItems.Add(legenditem);
legend1.LegendItems.Add(legenditem2);
// set the legend property of the series to the legend you created.
this.ChartControl1.Series[0].Legend = legend1;
this.ChartControl1.Series[1].Legend = legend1;
this.ChartControl1.Series[2].Legend = legend1;
|
See Also