0

我无法将不同的 sql 列绑定到饼图的 y 值。我想制作一个饼图,其中每列代表饼图中的一个切片,因此我希望例如将 A 列、B 列和 C 列的总和作为一个饼图中的切片。

这是我的代码:

        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPSConnection"].ConnectionString);
        string query = "SELECT SUM(Pick.Colli) AS TotalPick, SUM(Pack.Colli) AS TotalPack, Pack.Person_pack AS persoon FROM Pick CROSS JOIN Pack WHERE person_pack = 'annem2' GROUP BY person_pack";
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open();

        Chart1.DataSource = cmd.ExecuteReader();
        Series Series1 = new Series();
        Series1.XValueMember = "Person";
        Series1.YValueMembers = "TotalPick" + "TotalPack";

        Chart1.Series.Add(Series1);

        Chart1.Series[0].ChartType = SeriesChartType.Pie;

        Chart1.DataBind();
        conn.Close();

我也尝试了多个系列和 points.databindXY 但这对我也不起作用。

编辑 1.0:

我做了一些更改,但它仍然不起作用,有人知道我做错了什么吗?

        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPSConnection"].ConnectionString);
        string query = "SELECT SUM(Pick.Colli) AS TotalPick, SUM(Pack.Colli) AS TotalPack, Pack.Person_pack AS persoon FROM Pick CROSS JOIN Pack WHERE person_pack = 'annem2' GROUP BY person_pack";
        SqlCommand cmd = new SqlCommand(query, conn);
        conn.Open();

        SqlDataReader read = cmd.ExecuteReader();

        Chart1.DataSource = read;
        Series Series1 = new Series();
        Series1.YValuesPerPoint = 2;
        Series1.Points.DataBindXY(read, "Person", read, "TotalPack,TotalPick");

        Chart1.Series.Add(Series1);

        Chart1.Series[0].ChartType = SeriesChartType.Pie;
        Chart1.Series[0].ToolTip = "#VALY";

        Chart1.DataBind();

        conn.Close();
4

1 回答 1

0

问题已解决:

    conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPSConnection"].ConnectionString);
    string query = "SELECT SUM(Pick.Colli) AS TotalPick, SUM(Pack.Colli) AS TotalPack, Pack.Person_pack AS persoon FROM Pick CROSS JOIN Pack WHERE person_pack = 'annem2' GROUP BY person_pack";
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();

    SqlDataReader sqlReader = cmd.ExecuteReader();


        Series Series1 = new Series();
        Chart1.Series.Add(Series1);

        Series1.YValuesPerPoint = 2;

        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("Personen"));
        dt.Columns.Add(new DataColumn("Total"));


        while (sqlReader.Read())
        {
            dt.Rows.Add(sqlReader.GetValue(2),sqlReader.GetValue(0));
            dt.Rows.Add(sqlReader.GetValue(2), sqlReader.GetValue(1));
        }


        Chart1.Series[0].XValueMember = "Personen";
        Chart1.Series[0].YValueMembers = "Total";

        Chart1.DataSource = dt;

        Chart1.Series[0].ChartType = SeriesChartType.Pie;
        Chart1.Series[0].ToolTip = "#VALY";

        Chart1.DataBind();

    conn.Close();
于 2013-10-31T11:30:39.000 回答