Monday, June 28, 2010

Creating Attractive Bar Charts based on data in sharepoint lists and Surveys.

Errr..Reporting of sharepoint sites, data and lists and the backbone for it are sharepoint charts.We have KPI webparts and so many other things in Sharepoint, But today i am discussing how can we generate Charts using data from sharepoint lists..
So lets discuss how we are going to do this ??

I am creating a sharepoint webpart which will bring the data from a survey and represent it in a wonderfull bar chart, not the basic simple one...




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.DataVisualization.Charting;
using Microsoft.SharePoint;

namespace ChartingMS
{

[ToolboxData("<{0}:Charts runat=server>")]
public class Charts : WebPart
{
System.Web.UI.DataVisualization.Charting.Chart Chart1 = new System.Web.UI.DataVisualization.Charting.Chart();
CheckBox ShowLegend = new CheckBox();
int agree = 0;
int disagree = 0;
protected override void OnLoad(EventArgs e)
{
Chart1.Legends.Add("Legend1");
Chart1.Legends["Legend1"].Enabled = ShowLegend.Checked;



}
protected override void CreateChildControls()
{
remotecount();
ShowLegend.AutoPostBack = true;
ShowLegend.Text = "Show Legend ";
Chart1.Width = 500;
Chart1.Height = 320;
Chart1.RenderType = RenderType.ImageTag;
string imagespath = System.Configuration.ConfigurationSettings.AppSettings["ChartImageHandler"].ToString();
Chart1.ImageLocation = imagespath + "ChartPic_#SEQ(200,30)";
Chart1.Palette = ChartColorPalette.BrightPastel;
Title t = new Title("Each ministry should work independently to automate their own processes using their internal IT manpower. ", Docking.Top, new System.Drawing.Font("Trebuchet MS", 14, System.Drawing.FontStyle.Bold), System.Drawing.Color.FromArgb(26, 59, 105));
Chart1.Titles.Add(t);
Chart1.ChartAreas.Add("Agree");
// create a couple of series
Chart1.Series.Add("Agree");
Chart1.Series.Add("Disagree");
// add points to series 1
Chart1.Series["Agree"].Points.AddY(agree);
//Chart1.Series["Agree"].Points.AddY(8);
// Chart1.Series["Agree"].Points.AddY(12);
// Chart1.Series["Agree"].Points.AddY(6);
// Chart1.Series["Agree"].Points.AddY(9);
// Chart1.Series["Agree"].Points.AddY(4);
// add points to series 2
Chart1.Series["Disagree"].Points.AddY(disagree);
//Chart1.Series["Disagree"].Points.AddY(6);
// Chart1.Series["Disagree"].Points.AddY(18);
// Chart1.Series["Disagree"].Points.AddY(16);
//Chart1.Series["Disagree"].Points.AddY(21);
//Chart1.Series["Disagree"].Points.AddY(14);
Chart1.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
Chart1.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
Chart1.BorderlineDashStyle = ChartDashStyle.Solid;
Chart1.BorderWidth = 2;
this.Controls.Add(Chart1);
this.Controls.Add(ShowLegend);
}


protected override void RenderWebPart(HtmlTextWriter output)
{
RenderChildren(output);

}

public void remotecount()
{
SPSite osite = SPContext.Current.Site;
SPWeb oweb = SPContext.Current.Web;
SPListCollection lists = oweb.Lists;
SPList survey = lists["Ministry Survey"];

SPQuery oquery = new SPQuery();
oquery.Query = "";
SPListItemCollection items = survey.GetItems(oquery);
foreach (SPItem item in items)
{
if (item["ows_Each_x0020_ministry_x0020_should"].ToString() == "Agree")
{

agree = agree + 1;
}

if (item["ows_Each_x0020_ministry_x0020_should"].ToString() == "Disagree")
{

disagree = disagree + 1;
}





}




}


}
}

No comments:

Post a Comment