将SPLitsItem数据转换为某种图表或图形

时间:2019-03-15 15:42:12

标签: c# sharepoint visual-studio-2015 sharepoint-2016

我是SharePoint 2016编程的新手。我搜索了一小段关于用SPListItem中的数据填充图形/图表的信息,我看到了很多令人困惑的答案。

任何人都知道这样做真的很不错,为此有一些好的指南或教程吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在以下Web部件中使用SharePoint列表数据构建图表:

<asp:Chart ID="Chart1" runat="server" Width="500"> 
    <series> 
         <asp:Series 
                Name="Population" 
                XValueMember="Year" 
                YValueMembers="Population" 
                IsVisibleInLegend="true"> 
        </asp:Series> 
    </series> 
    <chartareas> 
        <asp:ChartArea 
                Name="ChartArea1" 
                Area3DStyle-Enable3D="true"> 
               <AxisX LineColor="DarkGreen"> 
                   <MajorGrid LineColor="LightGray" /> 
               </AxisX> 
               <AxisY LineColor="DarkGreen"> 
                   <MajorGrid LineColor="LightGray" /> 
               </AxisY> 
         </asp:ChartArea> 
    </chartareas> 
    <Legends> 
        <asp:Legend></asp:Legend> 
    </Legends> 
</asp:Chart>

后面的代码:

 try
        {
            var table = new DataTable();
            table.Columns.Add("Year", typeof(int));
            table.Columns.Add("Population", typeof(long));
            table.Columns.Add("Lbl");

            using (SPSite site = new SPSite(YourSiteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList _bugetlist = web.Lists["YearTotal"];
                    foreach (SPListItem _item1 in _bugetlist.Items)
                    {
                        var row = table.NewRow();
                        row["Year"] = Convert.ToInt16(_item1["Year"]);
                        row["Population"] = Convert.ToInt16(_item1["Population"]);
                        table.Rows.Add(row);
                    }
                }
            }
            Chart1.ChartAreas["ChartArea1"].AxisY.Title = "Population";
            Chart1.ChartAreas["ChartArea1"].AxisX.Title = "Years";


            Chart1.DataSource = table;
            Chart1.DataBind();

参考:

How to Create Custom Chart with data from SharePoint List