Expand Strategy
When you add new items to the LegendData object, the legend may become too long to display in the control window or image. For this reason you may wish to alter the legend layout strategy or, in other words, the way the legend expands when new items are added. The ExpandMode property of the LegendData controls this. The following table lists the possible strategies (for details, see LegendExpandMode Enumeration):
LegendExpandMode.RowsOnly |
The legend expands by adding new rows only and there is only one column. |
LegendExpandMode.ColsOnly |
The legend expands by adding new columns only and there is only one row. |
LegendExpandMode.RowsFixed |
The legend expands by adding new rows until it reaches the value RowCount (which is initially 10) and then adds a new column. In other words, the row count is limited and the column count is unlimited. |
LegendExpandMode.ColsFixed |
The legend expands by adding new columns until it reaches the value ColCount (which is initially 10) and then adds a new row. In other words, the column count is limited and the row count is unlimited. |
The following example shows this:
VB.NET |
|
legend.Data.ExpandMode = LegendExpandMode.RowsFixed legend.Data.RowCount = 4
|
C# |
|
legend.Data.ExpandMode = LegendExpandMode.RowsFixed; legend.Data.RowCount = 4; |
If you have 12 data items in the legend, this layout will produce a legend similar to this one:
Position
The legend's position is controlled with the HorizontalMargin, VerticalMargin, HorzAlign, and VertAlign properties of the Legend object. The first two define the margins of the legend in percent values of the control window dimensions, and the latter two how the legend should align to the margins. The following table shows how these properties work together to determine the final legend position:
The blue lines represent the position defined by the HorizontalMargin and VerticalMargin properties.
Predefined Legend Styles
Often you need to alter both the legend position (by default at the right top edge of the control window) and the legend expand mode. For example, consider that you want to display the following chart:
You can avoid having to write the code required to position the legend at the bottom, align it properly, and then change the expand mode to ColsOnly by using the SetPredefinedLegendStyle method of the of the Legend object. For instance:
VB.NET |
|
legend.SetPredefinedLegendStyle(PredefinedLegendStyle.Bottom) |
C# |
|
legend.SetPredefinedLegendStyle(PredefinedLegendStyle.Bottom); |
The following table lists the available legend styles (for more details, see PredefinedLegendStyle Enumeration):
PredefinedLegendStyle.Top |
Positions the legend at the top of the control window. The following code accomplishes this:
VB.NET |
|
legend.HorzAlign = HorzAlign.Center legend.VertAlign = VertAlign.Top legend.HorizontalMargin = 50 legend.VerticalMargin = 3 legend.Data.ExpandMode = LegendExpandMode.ColsOnly
|
C# |
|
legend.HorzAlign = HorzAlign.Center; legend.VertAlign = VertAlign.Top; legend.HorizontalMargin = 50; legend.VerticalMargin = 3; legend.Data.ExpandMode = LegendExpandMode.ColsOnly;
| |
PredefinedLegendStyle.Bottom |
Positions the legend at the bottom of the control window. The following code accomplishes this:
VB.NET |
|
legend.HorzAlign = HorzAlign.Center legend.VertAlign = VertAlign.Bottom legend.HorizontalMargin = 50 legend.VerticalMargin = 97 legend.Data.ExpandMode = LegendExpandMode.ColsOnly
|
C# |
|
legend.HorzAlign = HorzAlign.Center; legend.VertAlign = VertAlign.Bottom; legend.HorizontalMargin = 50; legend.VerticalMargin = 97; legend.Data.ExpandMode = LegendExpandMode.ColsOnly;
| |
PredefinedLegendStyle.Left |
Positions the legend at the left side of the control window. The following code accomplishes this:
VB.NET |
|
legend.HorzAlign = HorzAlign.Left legend.VertAlign = VertAlign.Center legend.HorizontalMargin = 2 legend.VerticalMargin = 50 legend.Data.ExpandMode = LegendExpandMode.RowsOnly
|
C# |
|
legend.HorzAlign = HorzAlign.Left; legend.VertAlign = VertAlign.Center; legend.HorizontalMargin = 2; legend.VerticalMargin = 50; legend.Data.ExpandMode = LegendExpandMode.RowsOnly;
| |
PredefinedLegendStyle.Right |
Positions the legend at the right side of the control window. The following code accomplishes this:
VB.NET |
|
legend.HorzAlign = HorzAlign.Right legend.VertAlign = VertAlign.Center legend.HorizontalMargin = 98 legend.VerticalMargin = 50 legend.Data.ExpandMode = LegendExpandMode.RowsOnly
|
C# |
|
legend.HorzAlign = HorzAlign.Right; legend.VertAlign = VertAlign.Center; legend.HorizontalMargin = 98; legend.VerticalMargin = 50; legend.Data.ExpandMode = LegendExpandMode.RowsOnly;
| |
PredefinedLegendStyle.TopRight |
Positions the legend at the top right side of the control window. The following code accomplishes this:
VB.NET |
|
legend.HorzAlign = HorzAlign.Right legend.VertAlign = VertAlign.Top legend.HorizontalMargin = 98 legend.VerticalMargin = 2 legend.Data.ExpandMode = LegendExpandMode.RowsOnly
|
C# |
|
legend.HorzAlign = HorzAlign.Right; legend.VertAlign = VertAlign.Top; legend.HorizontalMargin = 98; legend.VerticalMargin = 2; legend.Data.ExpandMode = LegendExpandMode.RowsOnly;
| |
PredefinedLegendStyle.TopLeft |
Positions the legend at the top left side of the control window. The following code accomplishes this:
VB.NET |
|
legend.HorzAlign = HorzAlign.Left legend.VertAlign = VertAlign.Top legend.HorizontalMargin = 2 legend.VerticalMargin = 3 legend.Data.ExpandMode = LegendExpandMode.RowsOnly
|
C# |
|
legend.HorzAlign = HorzAlign.Left; legend.VertAlign = VertAlign.Top; legend.HorizontalMargin = 2; legend.VerticalMargin = 3; legend.Data.ExpandMode = LegendExpandMode.RowsOnly; | |
Related Examples
Windows Forms: Legend\Position and Style
Web Forms: Legend\Position and Style
See Also
Legend | LegendData