Jun 11, 2009

Charts with Power WebPart ( Management-Eye-Catchers )

In this post I’ll give you some ideas how you can script charts in SharePoint with Power WebPart featuring Microsoft Chart Controls.

Visualizing a SharePoint list as a pie

image

image

Setup and Configuration:

  1. 1. Download and install Microsoft Chart Controls (ensure that System.Web.DataVisualization.dll is either in the GAC or in the web app’s bin folder)
  2. 2. Download and install Power WebPart (follow the instructions in the readme.txt)
  3. 3. To use the Chart Controls in ASP.NET or SharePoint you have to modify the web.config:

Add this line to appSettings section:

<add key="ChartImageHandler" value="Storage=session;Timeout=20;" />

Add this line to the httpHandlers section:

<add path="ChartImg.axd" verb="GET,HEAD" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false" />

The Script

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Web.DataVisualization")

$chart = New-Object System.Web.UI.DataVisualization.Charting.Chart
$series = new-object System.Web.UI.DataVisualization.Charting.Series
$chartArea = New-Object System.Web.UI.DataVisualization.Charting.ChartArea
$legend = New-Object System.Web.UI.DataVisualization.Charting.Legend

function createchildcontrols($controls)
{
$data = $web.Lists["Chart"].Items.GetDataTable()

$chart.DataSource = $data
$chart.Width=500

$series.ChartType = [System.Web.UI.DataVisualization.Charting.SeriesChartType]::Pie
$series.XValueMember ="Title"
$series.YValueMembers ="Value"
$series.ShadowOffset = 2
$series.IsValueShownAsLabel = $true

$chart.Series.Add($series)

$chartArea.Area3DStyle.Enable3D = $true
$chart.ChartAreas.Add($chartArea)

$chart.Legends.Add($legend)

$controls.Add($chart)

$chart.DataBind()

$series.Points.FindMaxByValue()["Exploded"] = $true
}

This just one example! The Chart Control is really mighty! Download the chart control examples from Microsoft to get an impression:


ChartImgCACU0PM6 ChartImgCAEO5EDD Bollinger2



The limitation is your imagination!

8 comments:

Share said...

Good information about SharePoint Charts

Anonymous said...

I love this, as I always found the built-in SharePoint chart functionality to be insufficient. It would be great if you could add an Excel-style GUI, so that non-developers could more easily use this.

Dave said...

Is there a missing step in the instructions or somethng missing? Tried this, but I get the error Error on render
Exception:
Object reference not set to an instance of an object.
Stack:
at iLoveSharePoint.WebControls.PowerControl.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) at System.Web.UI.Control.RenderControl(HtmlTextWriter writer) at iLoveSharePoint.WebControls.PowerWebPart.Render(HtmlTextWriter writer)

Christian said...

Hi Dave,
I would say no. But I'll double check this on another system. Or do you have it running already? You couls also ensure that list title and the columns match to the names in the example.
Bye, Christian

Dave said...

Thanks for replying Christian,

I tried this on a second "clean" MOSS 2007 SP2 system, step-by-step, and still no go.
The list is called Chart, in the same site. It has Title and Value columns, with the same numbers as your screenshot. I tried different data types for Value (text, number) but still the same error. The chart controls are installed, as is .net3.5sp1.
The webconfig for webapp under this site has the two edits you mentioned, and iis was restarted. The script was copied from your "clipboard copy" on your website using your (cool) PowerGui integration, and saved into the Script window in your web part. The editor script is all commented out.
I don't have AJAX or JQuery installed or selected in the web part properties.
This is an awesome post, as are most of yours and I'd love to get this working if you see something I missed or have any ideas.
Thanks.

Christian said...

Hi Dave,

I just checked the configuration on a new Web Application and it works like a charm.
The "Object reference not set to an instance of an object" could indicate that the System.Web.DataVisualization.dll could not found. Please check that the Assembly is in Global Assembly Cache(GAC). You can also check this through executing this line in PowerShell directly "[System.Reflection.Assembly]::LoadWithPartialName("System.Web.DataVisualization")". If returns null the assembly could not be found. At the moment I haven't any other idea. Hope this helps.
Bye, Christian

Anonymous said...

Hi,
I seem to have the same problem as Dave has.
I tried the "[System.Reflection.Assembly]::LoadWithPartialName("System.Web.DataVisualization")".
and it finds the dll from GAC.

I am using a brand new installation of WSS 3.0 SP2.

Is there a way to print to your debug-screen so I could see what is inside variables ?

regards,

petri.

Christian said...

Hi Petri,

can't tell you what the cause could be. As you suggest you could use the debug console to find further informations.
1) Start the debug console (in the Tools Folder)
2) Set in the Web Part Editor the console to tour your IP (Port:8777)
2a) Click to ping debug console to verify tthat the connections works
3) check the debug flag
4) apply
5) to now you should be able to step through the lines

When you choose suspend (s). You can enter scripts in the debug console and use the variables. To exit enter exit.

Hope you will get it running.
Bye, Christian