ASP饼图将多个列绑定到y值

时间:2013-10-30 11:09:59

标签: asp.net sql charts series pie-chart

我无法将不同的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();

1 个答案:

答案 0 :(得分: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();