0

我正在尝试使用饼图,我希望在上面显示 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>

输出:

在此处输入图像描述

我也想看看安全的部分。我有它的价值。如何解决这个问题?

4

1 回答 1

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);
于 2012-10-01T21:24:29.153 回答