如何将不同的ChartType从diffrant方法传递给此CreateGraph()方法的参数

本文关键字:方法 CreateGraph 参数 diffrant ChartType | 更新日期: 2023-09-27 18:24:41

哪里我错了建议我。

我有两种不同的方法,一种是CreateGraph

示例:

protected void PaiGraph_Click(object sender, EventArgs e)
{
    CreateGraph() //pass SeriesChartType.Pie  
}
protected void BarGraph_Click(object sender, EventArgs e)
{
    CreateGraph() //pass SeriesChartType.Bar
}

呼叫

public void CreateGraph()
{
    DataTable taskTable = (DataTable)Session["TaskTable"];
    int no = Convert.ToInt16("5");
    string[] x = new string[no];
    int[] y = new int[no];
    for (int j = 0; j < taskTable.Rows.Count; j++)
    {
            for (int i = 8; i < 9; i++)
            {
                x[j] = taskTable.Rows[j][i].ToString();
                y[j] = Convert.ToInt16(taskTable.Rows[j][i].ToString().Split(' ')[0]);
            }
    }
    Chart1.Series[0].Points.DataBindXY(x, y);
    Chart1.Series[0].ChartType = SeriesChartType.Pie;
    Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
    Chart1.Legends[0].Enabled = true;
}

当我们调用PaiGraph_Click()时,它将把SeriesChartType.Pie参数传递给这个CreateGraph()方法,而BarGraph_Click()调用它将把SeriesChartType.Bar参数传递给CreateGraph()方法。

怎么能通过?

如何将不同的ChartType从diffrant方法传递给此CreateGraph()方法的参数

将CreateGraph()方法更改为:

public void CreateGraph(SeriesChartType type){   
    if (type == SeriesChartType.Pie){
        // arrange data for pie chart
        Chart1.Series[0].Points.DataBindXY(x, y);
        Chart1.Series[0].ChartType = SeriesChartType.Pie;
        Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
        Chart1.Legends[0].Enabled = true;
    }
    else {
        //arrange data for barchart
        Chart1.Series[0].Points.DataBindXY(x, y);
        Chart1.Series[0].ChartType = SeriesChartType.Bar;
        Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = false;
        Chart1.Legends[0].Enabled = true;
    }
}

然后您在点击时更改您的呼叫

 CreateGraph(SeriesChartType.Pie)

将饼图替换为您在BarGraph中调用的条形图。单击

编辑:

或者,您可以使用switch语句,但似乎只使用了两个选项。