ASP.NET饼图仅显示YValueMembers

时间:2017-07-13 10:39:31

标签: c# asp.net .net charts mschart

我正在尝试创建一个图表来显示已检查和未检查的答案小册子的百分比。但饼图仅显示未选中的值,即YValueMember。如何解决这个问题?

"test.txt"

asp代码:

 protected void DropDown_Subjects_SelectedIndexChanged(object sender, EventArgs e)
{
    Chart4.Visible = true;
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
    SqlCommand cmd = new SqlCommand("select checked_percent, unchecked_percent From(select COUNT(*) * 100.0 / (select count(*)from[newexam2017].[dbo].[newexam2017] where sub_code = '" + DropDown_Subjects.SelectedValue + "') as checked_percent from[newexam2017].[dbo].[newexam2017]  where CheckBy is not null and sub_code = '" + DropDown_Subjects.SelectedValue + "' )checked,(select COUNT(*) * 100.0 / (select count(*)from[newexam2017].[dbo].[newexam2017] where sub_code = '" + DropDown_Subjects.SelectedValue + "')as unchecked_percent from[newexam2017].[dbo].[newexam2017]  where CheckBy is  null and sub_code = '" + DropDown_Subjects.SelectedValue + "')unchecked", connection);
    connection.Open();


    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    DataTable ChartData = ds.Tables[0];
    Chart4.DataSource = ChartData;

    Chart4.Series[0].Points.DataBind(ChartData.DefaultView, "checked_percent", "unchecked_percent", "");

    for (int i = 0; i < Chart4.Series[0].Points.Count; i++)
    Chart4.Series[0].Points[i].Label = string.Format("{0:0.00}%", ChartData.Rows[i]["checked_percent"], "{0:0.00}%", ChartData.Rows[i]["unchecked_percent"]);


    connection.Close();
   }

我想要的是什么:

chart1

我得到了什么:

chart2

1 个答案:

答案 0 :(得分:1)

在饼图中,checked_percentunchecked_percent都必须为Y值。像这样:

 protected void Chart4_Load(object sender, EventArgs e)
 {
    Chart4.Series[0].Points.Add(new DataPoint(0, (double)ChartData.Rows[0]["unchecked_percent"]));
    Chart4.Series[0].Points.Add(new DataPoint(1, (double)ChartData.Rows[0]["checked_percent"]));
 }

enter image description here

编辑:包含自定义标签:

enter image description here

protected void Chart4_Load(object sender, EventArgs e)
{
    DataPoint dp = new DataPoint(0, (double)ChartData.Rows[0]["unchecked_percent"]);
    dp.Label = string.Format("unchecked\n{0:0.00}%", ChartData.Rows[0]["unchecked_percent"]);
    Chart4.Series[0].Points.Add(dp);

    dp = new DataPoint(1, (double)ChartData.Rows[0]["checked_percent"]);
    dp.Label = string.Format("checked\n{0:0.00}%", ChartData.Rows[0]["checked_percent"]);
    Chart4.Series[0].Points.Add(dp);
}