Change chart type in ASP.net when event is changed in DropDownList SelectedIndex

I have a DropDownList and a diagram generated in ASP.net. The existing elements of this are the bar and pie charts.

I need to select my graph in the format that is selected in the DropDownList. Is there any possible way to do this? Thanks in advance...!

My coding looks like this.

<asp:DropDownList ID="drpChart" runat="server" AutoPostBack="True" Height="24px" Width="142px">
                            <asp:ListItem>Bar Chart</asp:ListItem>
                            <asp:ListItem>Pie Chart</asp:ListItem>
                            </asp:DropDownList>

<asp:Chart ID="Chart2" runat="server" style="margin-left: 40px">
                    <Series>
                        <asp:Series Name="Series1">
                        </asp:Series>
                    </Series>
                    <ChartAreas>
                        <asp:ChartArea Name="ChartArea1">
                        </asp:ChartArea>
                    </ChartAreas>
                </asp:Chart>

      

+3


source to share


2 answers


A simple switch statement will do the trick:

ASPX:

<asp:DropDownList ID="drpChart" runat="server" AutoPostBack="True" Height="24px"
    Width="142px">
    <asp:ListItem>Bar Chart</asp:ListItem>
    <asp:ListItem>Pie Chart</asp:ListItem>
</asp:DropDownList><br />
<asp:Chart ID="Chart1" runat="server">
    <Series>
        <asp:Series Name="Series1">
        </asp:Series>
    </Series>
    <ChartAreas>
        <asp:ChartArea Name="ChartArea1">
        </asp:ChartArea>
    </ChartAreas>
</asp:Chart>

      



Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    var employees = new Dictionary<int, string>
        {
            {10, "Product A"},{20, "Product B"},{30, "Product C"}
        };

    foreach (KeyValuePair<int, string> employee in employees)
    {
        Chart1.Series[0].Points.AddXY(employee.Value, employee.Key);
    }

    string chartType = drpChart.SelectedValue;
    switch(chartType)
    {
        case "Bar Chart":
            Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Bar;
            break;
        case "Pie Chart":
            Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
            break;
    }
}

      

+1


source


Use the code in DropDownList

event i using the same function

here is my code:



protected void Dd_Graph_Selection_SelectedIndexChanged(object sender, EventArgs e)
    {
        string chartType = Dd_Graph_Selection.SelectedValue;
        switch (chartType)
        {
            case "Column Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Column;
                break;
            case "Pie Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Pie;
                break;
            case "Bar Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Bar;
                break;
            case "BoxPlot Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.BoxPlot;
                break;
            case "Funnel Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Funnel;
                break;
            case "Point Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Point;
                break;
            case "Spilinr Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Spline;
                break;
            case "Bubble Chart":
                Chart1.Series[0].ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Bubble;
                break;
        }
    }

      

+3


source







All Articles