SharePoint - How to create webpart and display chart in that webpart in SP2010?

Asked By Muthu Kumar on 29-Sep-10 10:45 AM
Hi all,

Now, I have been developing a sample webpart using VS 2010 Sandboxed Solution in SharePoint 2010 to display the chart (pie, bar etc) based on the list items available on the target SharePoint list. While pressing F5, it automatically deployed successfully and that custom webpart is also available in the webpart galleries. But, while dragging the custom webpart, I got the error below,

Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred.

Please note that I need a solution for how to create a webpart and display the relevant charts in that custom webpart using Sandbox Solution in SharePoint 2010. Any help?...Appreciated...

I have written the code in my sample as below,

protected override void CreateChildControls()

{

try


{

Chart dashboardChart = new Chart();

Series f = new Series("Series1");

dashboardChart.Series.Add(f);

f.ChartType = SeriesChartType.Bar;

ChartArea a = new ChartArea("ChartArea1");

dashboardChart.ChartAreas.Add(a);

DataTable dt = new DataTable();

DataColumn dc;

dc = new DataColumn();

dc.ColumnName = "Name";

dt.Columns.Add(dc);

dc = new DataColumn();

dc.ColumnName = "Age";

dt.Columns.Add(dc);

DataRow dr;

dr = dt.NewRow();

dr["Name"] = "Fred";

dr["Age"] = "54";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["Name"] = "Bill";

dr["Age"] = "66";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["Name"] = "Rhona";

dr["Age"] = "32";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["Name"] = "Peter";

dr["Age"] = "46";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["Name"] = "Tina";

dr["Age"] = "26";

dt.Rows.Add(dr);

dr = dt.NewRow();

dr["Name"] = "Ella";

dr["Age"] = "2";

dt.Rows.Add(dr);

dashboardChart.DataSource = dt;

dashboardChart.Series["Series1"].XValueMember = "Name";

dashboardChart.Series["Series1"].YValueMembers = "Age";

dashboardChart.DataBind();

this.Controls.Add(dashboardChart);

}

catch (Exception ex)

{

throw ex;

}


}





























Alon Havivi replied to Muthu Kumar on 30-Sep-10 04:10 PM
Hello Muthu,

This error code means you run non-authorized method in your Sandbox solution. I do not think it is possible to use .NET Chart Controls in SnadBox solutions. The Chart control saves the rendered chart images as files in memory or on disk.  (more information about Chart Image Rendering http://msdn.microsoft.com/en-us/library/dd456682.aspx).

You can try to embed Google Chart in your Sandbox solutions. Google Chart API lets you dynamically generate charts with a URL string. You can embed these charts on your web parts. see sample here http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World