如何DataBind DataList中的饼图

时间:2018-05-16 20:25:38

标签: c# asp.net charts datalist

我有来自SQL Server表的以下结果数据

Product_ID  Unit_Type  Product_Pic    TotalUnique   TotalFail  Yield
1           Lawn Mower Lawnmower.jpg  470           27         94.25
2           Bicycle    Bicycle.jpg    5             1          80
3           Tractor    Tractor.jpg    0             0          100
5           Blender    Blender.jpg    360           12         96.66
6           Fridge     Fridge.jpg     11            6          94.59

我有一个数据列表,显示每个产品的给定信息。我想为使用Total Unique和TotalFail的datalist中的每个产品添加一个简单的饼图。我到处寻找,无法找到让每个产品饼图使用绑定到datalist的数据的方法。我似乎必须将数据绑定到datalist,并将数据专门绑定到图表。

我想我的问题是我可以这样做吗?如果是这样,你能引导我朝着正确的方向前进吗?

以下是一些更多背景信息。我的Datalist看起来像这样(图表已注释掉了。

   <asp:DataList ID="ProductDataList" runat="server" RepeatColumns="5" CellPadding="10" HorizontalAlign="Center" ItemStyle-VerticalAlign="Top" 
        RepeatDirection="Horizontal" Width="90%" OnSelectedIndexChanged="ProductDataList_SelectedIndexChanged">
        <ItemTemplate>

           <asp:Label ID="lblProductName" runat="server" Text='<%# Eval("Unit_Type") %>' /><br /><br />
           <asp:Label ID="lblUniqueTests" runat="server" Text='<%# "Unique Tests: " + Eval("TotalUnique") %>' /><br />
           <asp:Label ID="lblFailedTests" runat="server" Text='<%# "Failed Tests: " + Eval("TotalFail") %>' /><br />
           <asp:Label ID="lblYield" runat="server" Text='<%# "Yield: " + Eval("Yield", "{0:0.00}") + "%" %>' /><br /><br />
           <asp:Image ID="imgProductImage" runat="server" ImageURL='<%# "Images/" + Eval("Product_Pic") %>'/><br /><br /><br />

            <asp:Chart ID="Chart1" runat="server">
                <Series>
                    <asp:Series Name="PieData" ChartType="Pie"></asp:Series>
                </Series>
                <ChartAreas>
                    <asp:ChartArea Name="ChartArea1"></asp:ChartArea>
                </ChartAreas>
            </asp:Chart>

        </ItemTemplate>
    </asp:DataList>

以下是我用来填充Datalist的代码的一部分。

    lsSQL = "SELECT Product_ID, Product_Type, Unit_Type, Product_Pic, t.TotalUnique, t.TotalFail, ";
    lsSQL = lsSQL + "IsNull(((TotalUnique - NullIf(TotalFail, 0)) / Convert(Numeric(38, 2), TotalUnique) * 100), 100) AS Yield ";
    lsSQL = lsSQL + "FROM ";
    lsSQL = lsSQL + "( ";
    lsSQL = lsSQL + "SELECT Products.Product_ID, Products.Product_Type, Products.Unit_Type, Products.Image, ";
    lsSQL = lsSQL + "(Select COUNT(DISTINCT Functional_Tests.UUT_Serial_Number) AS MyCount FROM Functional_Tests ";
    lsSQL = lsSQL + "    WHERE Time_Stamp >= '" + txtDateFrom.Text + "' AND Time_Stamp <= '" + txtDateTo.Text + "' AND Product_ID = Products.Product_ID) AS TotalUnique, ";
    lsSQL = lsSQL + "(Select COUNT(DISTINCT Functional_Tests.UUT_Serial_Number) AS MyCount FROM Functional_Tests ";
    lsSQL = lsSQL + "    WHERE Time_Stamp >= '" + txtDateFrom.Text + "' AND Time_Stamp <= '" + txtDateTo.Text + "' AND Product_ID = Products.Product_ID AND Test_Result = 'Fail' ) AS TotalFail ";
    lsSQL = lsSQL + "FROM Products WHERE " + DBProduct_ID_String;
    lsSQL = lsSQL + " ) t";

    SqlConnection DBConnection = new SqlConnection(cnnString);
    SqlCommand cmd = new SqlCommand();

    cmd.CommandText = lsSQL;
    cmd.CommandType = CommandType.Text;
    cmd.Connection = DBConnection;
    cmd.Connection.Open();

    ProductDataList.DataSource = cmd.ExecuteReader();
    ProductDataList.DataBind();

0 个答案:

没有答案