Posted By:jayasankarkesarla
Posted Date: November 18, 2013
Points: 200
Category: SharePoint
URL: http://www.emanonlabs.in/
In this article I am exploring custom chart web part in SharePoint.
In this article I am exploring custom chart web part in SharePoint.
|
Introduction:
In this article I am exploring custom chart web part in SharePoint.
Reporting
is an important part of any project. Sometime client required to
display the record in a visual format.so we need to display record in
any visual format like chart web part or other
For displaying a list record in chart format please follow below steps.
1.create a list with numeral values like below
2. Add the following references to your web.config in their respective locations:
01 | <SafeControls> |
02 |
03 |
04 | <SafeControl Assembly= "System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace= "System.Web.UI.DataVisualization.Charting" TypeName= "*" Safe= "True" /> |
05 |
06 | <system.web> |
07 |
08 |
09 | <httpHandlers> |
10 |
11 | <add path= "ChartImg.axd" verb= "GET,HEAD,POST" type= "System.Web.UI.DataVisualization.Charting.ChartHttpHandler,
System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" validate= "false" /> |
12 |
13 | </httpHandlers> |
14 |
15 | |
16 |
17 | <appSettings> |
18 |
19 |
20 | <add key= "ChartImageHandler" value= "storage=file;timeout=60;" /> |
21 |
22 |
23 | <system.webServer> |
24 | <handlers> |
25 |
26 |
27 | <add name= "ChartImageHandler" preCondition= "integratedMode" verb= "GET,HEAD,POST" path= "ChartImg.axd" type= "System.Web.UI.DataVisualization.Charting.ChartHttpHandler,
System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35" /> |
3. Add the below code in in cs file :
001 | <font color= "#000000" > using System.Data; |
002 |
003 | using System.Linq; |
004 |
005 | using System.Web.UI; |
006 |
007 | using Microsoft.SharePoint; |
008 |
009 | using System.Web.UI.DataVisualization.Charting; |
010 |
011 | namespace SPChartWp.ChartWp |
012 |
013 | { |
014 |
015 | public partial class ChartWpUserControl : UserControl |
016 |
017 | { |
018 |
019 | DataView dv; |
020 |
021 | protected void Page_Load( object sender, EventArgs e) |
022 |
023 | { |
024 |
025 | try |
026 |
027 | { |
028 |
029 | SPSecurity.RunWithElevatedPrivileges( delegate () |
030 |
031 | { |
032 |
033 | using (SPSite oSite = new SPSite(SPContext.Current.Site.Url)) |
034 |
035 | { |
036 |
037 | using (SPWeb oWeb = oSite.OpenWeb()) |
038 |
039 | { |
040 |
041 | SPListCollection oListCollection = oWeb.Lists; |
042 |
043 | |
044 |
045 | SPList oList = oWeb.Lists[ "Tracker" ]; |
046 |
047 | |
048 |
049 | SPListItemCollection oItems = oList.GetItems(); |
050 |
051 | |
052 |
053 | DataTable dtMain = ConvertSPListToDataTable(oItems); |
054 |
055 | |
056 |
057 | var dtNkdkdew = (from r in dtMain.AsEnumerable() |
058 |
059 | select r).Distinct(); |
060 |
061 | DataTable resulttbl = new DataTable(); |
062 |
063 | |
064 |
065 | IEnumerable< string > DistinRRMasterID = new List< string >(); |
066 |
067 | DistinRRMasterID = (from results in dtMain.AsEnumerable() |
068 |
069 | select ( string )results[ "Role" ]).Distinct(); |
070 |
071 | |
072 |
073 | //Create DataTable |
074 |
075 | DataTable returnDt = new DataTable(); |
076 |
077 | DataRow oRow = returnDt.NewRow(); |
078 |
079 | string [] ColumnsArr = { "Role" , "Number Postions" , "In Progress" , "On Hold" , "L1 Scheduled" , "L2 Scheduled" , "HR Scheduled" , "?Offer Released" }; |
080 |
081 | int noOfColumns = ColumnsArr.Length; |
082 |
083 | for ( int cntr = 0; cntr < noOfColumns; cntr++) |
084 |
085 | { |
086 |
087 | DataColumn column = new DataColumn(); |
088 |
089 | column.ColumnName = ColumnsArr[cntr]; |
090 |
091 | column.DataType = typeof ( string ); |
092 |
093 | returnDt.Columns.Add(column); |
094 |
095 | } |
096 |
097 | |
098 |
099 | foreach (var rrRole in DistinRRMasterID) |
100 |
101 | { |
102 |
103 | |
104 |
105 | |
106 |
107 | var rrInProgressCnt = (from r in dtMain.AsEnumerable() |
108 |
109 | where (r.Field< string >( "Role" ) == rrRole |
110 |
111 | && r.Field< string >( "Requirement Status" ) == "In Progress" ) |
112 |
113 | select r).Count(); |
114 |
115 | |
116 |
117 | var OnHoldCnt = (from r in dtMain.AsEnumerable() |
118 |
119 | where (r.Field< string >( "Role" ) == rrRole |
120 |
121 | && r.Field< string >( "Requirement Status" ) == "On Hold" ) |
122 |
123 | select r).Count(); |
124 |
125 | var L1RoundCnt = (from r in dtMain.AsEnumerable() |
126 |
127 | where (r.Field< string >( "Role" ) == rrRole |
128 |
129 | && r.Field< string >( "Requirement Status" ) == "L1 Round" ) |
130 |
131 | select r).Count(); |
132 |
133 | var L2RoundCnt = (from r in dtMain.AsEnumerable() |
134 |
135 | where (r.Field< string >( "Role" ) == rrRole |
136 |
137 | && r.Field< string >( "Requirement Status" ) == "L2 Round" ) |
138 |
139 | select r).Count(); |
140 |
141 | var HRRoundCnt = (from r in dtMain.AsEnumerable() |
142 |
143 | where (r.Field< string >( "Role" ) == rrRole |
144 |
145 | && r.Field< string >( "Requirement Status" ) == "HR Round" ) |
146 |
147 | select r).Count(); |
148 |
149 | |
150 |
151 | var OfferReleasedCnt = (from r in dtMain.AsEnumerable() |
152 |
153 | where (r.Field< string >( "Role" ) == rrRole |
154 |
155 | && r.Field< string >( "Requirement Status" ) == "Offer Released" ) |
156 |
157 | select r).Count(); |
158 |
159 | |
160 |
161 | var MainColl = (from r in dtMain.AsEnumerable() |
162 |
163 | where (r.Field< string >( "Role" ) == rrRole) |
164 |
165 | select r); |
166 |
167 | |
168 |
169 | #region Bind Fields datatable |
170 |
171 | //Add Data to row |
172 |
173 | //Create DataTable Rows |
174 |
175 | oRow = returnDt.NewRow(); |
176 |
177 | var item = MainColl.FirstOrDefault(); |
178 |
179 | oRow[ "Role" ] = rrRole; |
180 |
181 | oRow[ "Number Postions" ] = item[ "NmberOfPostion" ]; |
182 |
183 | oRow[ "In Progress" ] = rrInProgressCnt; |
184 |
185 | oRow[ "On Hold" ] = OnHoldCnt; |
186 |
187 | oRow[ "L1 Scheduled" ] = L1RoundCnt; |
188 |
189 | oRow[ "L2 Scheduled" ] = L2RoundCnt; |
190 |
191 | oRow[ "HR Scheduled" ] = HRRoundCnt; |
192 |
193 | oRow[ "?Offer Released" ] = OfferReleasedCnt; |
194 |
195 | |
196 |
197 | returnDt.Rows.Add(oRow); |
198 |
199 | #endregion |
200 |
201 | } |
202 |
203 | |
204 |
205 | Chart1.ChartAreas[ "ChartArea1" ].AxisY.Title = "Number Postions" ; |
206 |
207 | Chart1.ChartAreas[ "ChartArea1" ].AxisX.Title = "Role" ; |
208 |
209 | Chart1.DataSource = returnDt; |
210 |
211 | Chart1.DataBind(); |
212 |
213 | dv = new DataView(returnDt); |
214 |
215 | dv.Sort = "Role ASC" ; |
216 |
217 | //Bind Data to Grid |
218 |
219 | GridView1.DataSource = dv; |
220 |
221 | GridView1.PagerTemplate = null ; |
222 |
223 | GridView1.DataBind(); |
224 |
225 | |
226 |
227 | } |
228 |
229 | } |
230 |
231 | }); |
232 |
233 | } |
234 |
235 | catch (Exception ex) |
236 |
237 | { |
238 |
239 | Response.Write(ex.Message); |
240 |
241 | } |
242 |
243 | |
244 |
245 | } |
246 |
247 | private static DataTable ConvertSPListToDataTable(SPListItemCollection spItemCollection) |
248 |
249 | { |
250 |
251 | DataTable dt = new DataTable(); |
252 |
253 | |
254 |
255 | try |
256 |
257 | { |
258 |
259 | dt = spItemCollection.GetDataTable(); |
260 |
261 | foreach (DataColumn c in dt.Columns) |
262 |
263 | c.ColumnName = System.Xml.XmlConvert.DecodeName(c.ColumnName); |
264 |
265 | return (dt); |
266 |
267 | |
268 |
269 | } |
270 |
271 | catch |
272 |
273 | { |
274 |
275 | return (dt); |
276 |
277 | } |
278 |
279 | } |
280 |
281 | } |
282 |
283 | } |
284 |
285 | </font> |
4.Web part will be display as below
Comments