Custom Legend Items in Chart Controls
By default, legend items are each bound to a series, and their properties are the same for all series. You can use custom legend items that are not bound to any series and that enable you to adjust the appearance, position, and margin of every cell. To do this, use the Legend.CustomItems collection property (a LegendItemCollection object) to add a customized legend item (a LegendItem object) to a legend. Legend items in this collection are always attached to the end of other legend items in the legend.
Legend items in the Legend.CustomItems collection enable you to customize the following:
Specify the symbol using the LegendItem.ImageStyle property. Choose between rectangle, line, and marker.
Use images as symbols using the LegendItem.Image or LegendItem.MarkerImage property.
Add cells to the legend.
Adjust the appearance, position, and margin of each cell.
To add legend cells (LegendCell objects) to a customized legend item, use the LegendItem.Cells collection property (a LegendCellCollection object).
Specify the cell type in the LegendCell.CellType property. If you set this property to LegendCellType.SeriesSymbol, the legend cell uses the symbol that is used by the container legend item.
To merge two adjacent cells into one, such as to accommodate for longer strings, use the LegendCell.CellSpan property.
When there is at least one legend cell in the legend item, the appearance properties of the legend item has no effect.
The following code uses a customized legend item with legend cells at run time to show the region with the highest statistical average.
double avgWA = Chart1.DataManipulator.Statistics.Mean("WA"); double avgOR = Chart1.DataManipulator.Statistics.Mean("OR"); double avgCA = Chart1.DataManipulator.Statistics.Mean("CA"); String top = (avgWA >= avgOR ? "Washington":"Oregon"); if (avgCA >= avgWA && avgCA >= avgOR) top = "California"; LegendItem newItem = new LegendItem(); newItem.ImageStyle = LegendImageStyle.Marker; newItem.MarkerStyle = MarkerStyle.Diamond; newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleCenter); newItem.Cells.Add(LegendCellType.Text, "State Average =", ContentAlignment.MiddleCenter); newItem.Cells.CellSpan = 2; newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleCenter); newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleCenter); Chart1.Legends.CustomItems.Add(newItem);