为什么饼图只显示一个字段的值?

时间:2012-10-01 20:49:32

标签: asp.net .net sql-server charts

我正在尝试使用饼图,我希望在其上显示2个区域。但是对于我的下面的代码,只显示了最后一个代码。

Code behind:

 protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();

        dt.Columns.Add("status");
        dt.Columns.Add("count");

        DataRow dr = dt.NewRow();
        dr["status"] = "safe";
        dr["count"] = loadChart("safe");

        dt.Rows.Add(dr);

        dr["status"] = "unsafe";
       dr["count"] = loadChart("unsafe");

        dr = dt.NewRow();

       dt.Rows.Add(dr);

        Chart1.DataSource = dt;

        Chart1.Series[0].XValueMember = "status";
        Chart1.Series[0].YValueMembers = "count";

        Chart1.DataBind();
    }

标记:

 <asp:Chart ID="Chart1" runat="server">
        <series>
            <asp:Series ChartType="Pie" Name="Series1">
            </asp:Series>

        </series>
        <chartareas>
            <asp:ChartArea Name="ChartArea1">
            </asp:ChartArea>
        </chartareas>
    </asp:Chart>

输出:

enter image description here

我也希望看到安全部分。我有价值。如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您要在同一行中设置两次值:


DataRow dr = dt.NewRow();
dr["status"] = "safe";
dr["count"] = loadChart("safe");
dt.Rows.Add(dr);
//still in the same row
dr["status"] = "unsafe";
dr["count"] = loadChart("unsafe");
dr = dt.NewRow();
dt.Rows.Add(dr);

您应该创建新行,然后设置第二个值:


DataRow dr = dt.NewRow();
dr["status"] = "safe";
dr["count"] = loadChart("safe");
dt.Rows.Add(dr);
//new row creation
dr = dt.NewRow();
dr["status"] = "unsafe";
dr["count"] = loadChart("unsafe");    
dt.Rows.Add(dr);