asp.net - How to change chart legend style programmatically

Change the Chart legend style programmatically
The Chart class serves as the root class of the Chart control. The Chart class exposes all of the properties, methods, and events of the Chart Web server control. The Chart Series collection property stores Series objects which are used to store data that is to be displayed, along with attributes of that data. The Chart ChartAreas collection property stores ChartArea objects, which are primarily used to draw one or more charts using one set of axes.

The following asp.net c# web development tutorial code demonstrates how we can change the Chart legend style programmatically. Here we will use the Chart class Legends property to get the legend collection. Then we will get the specified legend instance by its id. And then we will set or change the Chart legend’s default style by setting the Legend class LegendStyle property value.

The Chart Legends property gets or sets a LegendCollection object that is used to store all Legend objects used by the Chart control. This property value is a LegendCollection that is used to store all Legend objects. This Chart collection property stores all Legend objects used by the Chart control.

The Legend class represents the legend for the chart image. The Legend class encapsulates all the functionality of the Chart control's legend and is exposed using the Legends collection property of the root Chart object. In a Chart object can be any number of legends for a chart image.

The Legend class LegendStyle property gets or sets the style of the legend. This property value is a LegendStyle enumeration value that determines the legend style. The default value of this property is Table.

The Chart legend is displayed as a series of items in a table by default. The asp.net web developers can specify whether to expand items in the table width-wise or height-wise by setting the TableStyle property.

The asp.net web developers can select a style from the three possible styles for a legend. Those legend styles are Column, Row, and Table.

In a Column style, legend items are displayed in one column, with multiple rows. Most commonly the asp.net developers used it when the legend is docked to the left or right of the chart.

In a Row style, legend items are displayed in one row, with multiple columns. Most commonly the asp.net developers used it when the legend is docked to the top or bottom of the chart.

In a Table style, legend items are displayed using multiple columns and rows.
ChartLegendLegendStyle.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        Chart1.Legends["Legend1"].LegendStyle = LegendStyle.Column;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to set change Chart Legend LegendStyle programmatically in asp.net</title>
    <style type="text/css">
        h2
        {
            color:DarkBlue;
            font-style:italic;
            }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2>Chart example and tutorial: How to set change Chart Legend<br /> LegendStyle programmatically in asp.net</h2>
        <hr width="625" align="left" color="LightBlue" />
        <br />
        <asp:Chart 
            ID="Chart1" 
            runat="server"
            Width="625" 
            BackColor="Violet"
            BorderlineColor="DarkViolet"
            BorderlineDashStyle="Dash"
            BorderlineWidth="2"
            >
            <Legends>
                <asp:Legend 
                    Name="Legend1"
                    BackColor="Snow"
                    ForeColor="Green"
                    BorderColor="SeaGreen"
                    Docking="Top"
                    >
                </asp:Legend>
            </Legends>
            <Series>
                <asp:Series 
                    Name="NokiaPhone" 
                    YValueType="Double" 
                    ChartArea="DefaultChartArea"
                    ChartType="Pyramid"
                    Palette="SeaGreen"
                    >
                    <Points>
                        <asp:DataPoint AxisLabel="Nokia N8" YValues="549" />
                        <asp:DataPoint AxisLabel="Nokia N97 mini" YValues="379.99" />
                        <asp:DataPoint AxisLabel="X6 16GB Driver Edition" YValues="329" />
                        <asp:DataPoint AxisLabel="Nokia C6" YValues="309" />
                    </Points>
                </asp:Series>
            </Series>
            <ChartAreas>
                <asp:ChartArea 
                    Name="DefaultChartArea" 
                    BorderDashStyle="Dot"
                    BorderWidth="2"
                    BorderColor="MediumOrchid"
                    BackColor="MediumPurple"
                    >
                    <Area3DStyle Enable3D="true" LightStyle="Realistic" />
                </asp:ChartArea>
            </ChartAreas>
        </asp:Chart>
        <br />
        <asp:Button 
            ID="Button1"
            runat="server"
            Text="Set Chart Legend LegendStyle Column"
            Font-Bold="true"
            OnClick="Button1_Click"
            ForeColor="DarkBlue"
            Height="45"
            />
    </div>
    </form>
</body>
</html>
More asp.net chart examples