1

I´m new at this of programming in sharepoint 2016. I search a litle bit about filling a graph/chart with data from a SPListItem and i see a lot of confusing answers.

Anyone know's a really good away to do this, some kind of good guide or tutorial for this?

Thanks in advance.

Tiago dias
  • 133
  • 13

1 Answers1

1

You could build chart with SharePoint list data in a web part like:

<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>

Code behind:

 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();

Reference:

How to Create Custom Chart with data from SharePoint List

Jerry
  • 3,480
  • 1
  • 10
  • 12