3

我有一个 DropDownList 和一个在 ASP.net 中创建的图表。它的现有项目是条形图和饼图。

我需要将我的图表选择为在 DropDownList 中选择的格式。有没有办法做到这一点?提前谢谢你....!

我的编码如下。

<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>
4

2 回答 2

3

DropDownList如果我使用相同的功能,请使用您的代码

这是我的代码:

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;
        }
    }
于 2013-04-03T06:25:16.053 回答
1

一个简单的 switch 语句就可以解决问题:

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>

后面的代码:

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;
    }
}
于 2013-02-02T19:29:16.123 回答