Using Google Charts in SharePoint

August 13, 2012 Leave a comment

Hi everyone. I recently had to build some charts for a SP Project and for me , the easiest way to get visually attracting and interactive charts, was to use Google charts. Now there are quite a few ways to get Google charts going in SharePoint (example http://code.google.com/p/googlechartsharp), so I’m sharing my way of doing it and I hope it helps someone else. Disclaimer… I am not claiming that I wrote all this code myself. Some parts are borrowed.

Ok, so here we go:

Firstly, Create a new SharePoint Solution and add a new Visual Web Part. I’m skipping instructions on this but if you don’t know how to do this, I suggest you start with some basic SharePoint development tutorials first.

Open up the User control in your visual web part (the ascx file) and insert the following code:

<div>
<!-- This line adds the Google Javascript API to your page -->
<script type="text/javascript" src="http://www.google.com/jsapi"></script>

<!-- Using the Google jsapi, we load the core charts api -->
<script type="text/javascript">
    google.load('visualization', '1', { packages: ['corechart'] });
</script>

<script type="text/javascript">
function drawVisualization() {

 // Create and populate the data table.
 // MyArrayData is your array declared on server side
 var ChartData = google.visualization.arrayToDataTable(MyArrayData); 

 //Set all your chart options here.
 var ChartOptions = {
  'title': 'Final gold medal count for 2012 olympics',
  'titleTextStyle': { color: 'black', fontName: '"Verdana"', fontSize: 12 },
  'width': 350,
  'height': 350,
  'backgroundColor': 'white',
  'chartArea': { 'width': '90%', 'height': '70%' },
  'pieSliceText': 'percentage',
  'tooltip': { textStyle: { color: 'black', fontName: '"Verdana"',fontSize:12}},
  'is3D': true
  };

    // Create and draw the visualization.
    new google.visualization.PieChart(document.getElementById('ChartAreaDIV')).
    draw(ChartData , ChartOptions );
}
google.setOnLoadCallback(drawVisualization);
</script>

</div>
<div style="border: 0 none;">
<div id="ChartAreaDIV" style="width: 350px; height: 350px;"></div>
</div>

Next up, lets get some data to render. Open the Code behind file for the ASCX, Here is the code for your code behind file:

public partial class TestGoogleChartUserControl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DemoData demodata = new DemoData();

        //Get the Data - Use your data access layer here
        var data = demodata.GetSomeDemoData();

        //Telling the Array what it's going to hold
        Page.ClientScript.RegisterArrayDeclaration(
            "MyArrayData", "['Label', 'Value']");

        //Register items into a javascript (client side) array
        foreach (var dataitem in data)
        {
            Page.ClientScript.RegisterArrayDeclaration("MyArrayData", 
                string.Format("['{0}', {1}]", 
                dataitem.ChartItemLabel, 
                dataitem.ChartItemValue));
        }
    }
}
public class DemoData
{
    //Use your Data access layer or some other datasource
    public List<MyDataContract> GetSomeDemoData()
    {
        List<MyDataContract> data = new List<MyDataContract>();
        data.Add(new MyDataContract { 
            ChartItemLabel = "United States", 
            ChartItemValue = 46 });

        data.Add(new MyDataContract { 
            ChartItemLabel = "China", 
            ChartItemValue = 38 });

        data.Add(new MyDataContract { 
            ChartItemLabel = "Great Britain", 
            ChartItemValue = 29 });

        data.Add(new MyDataContract { 
            ChartItemLabel = "Russia", 
            ChartItemValue = 24 });

        data.Add(new MyDataContract { 
            ChartItemLabel = "Korea", 
            ChartItemValue = 13 });

        data.Add(new MyDataContract { 
            ChartItemLabel = "Germany", 
            ChartItemValue = 11 });
        return data;
    }
}    
public class MyDataContract
{
    //Just a quick data structure 
    //implemention here to make things easier
    public string ChartItemLabel { get; set; }
    public int ChartItemValue { get; set; }                
}

That’s it. Deploy your web part and it should render a nice little interactive Google pie chart:

 

Read more on Google charts here: https://google-developers.appspot.com/chart/

Find SharePoint Databases not in use

Hiya all

I always find it difficult to keep track of the millions of databases which SharePoint 2010 creates especially when you have multiple different SQL Servers and differnet Sharepoint environments (DEV, UAT, PROD etc).

To this end, I figured out a quick way (Powershell to the rescure) to compare which databases sharepoint still uses and which database server they’re on. I know you can do most of this through Central Admin, but to click on every web application/ service application and compare with SQL takes time.

Now, the command I wrote is not really something new, but most of the other blogs demonstrates a PS command to show the Database name, but that’s not all we want to see now is it? We also need the Database Server.

Ok, so without further Delay, here it is:

Get-SPDatabase | ForEach-Object {$_.DatabaseConnectionString}

This will output all the databases in your farm in a connection string (ASP.Net) format which shows the db name, db server, authentication method etc..

The above PS command will output to the screen, but you can easily output to XML using:

Get-SPDatabase | Export-Clixml -InputObject {$_.DatabaseConnectionString} -Path "c:\ConnectionSTrings.xml

So that’s it. The beauty of Powershell. Hope this helped you.

PowerShell tips and tricks – commands and help

February 6, 2012 Leave a comment

Hi guys.

I’m starting to share a couple of great Powershell tips and tricks i’ve learnt and picked up myself as I learnt to use this fantastic tool.

You can at any time prefix your PS Command with “Get-Help”. This will show the documentation available for that command but more importantly it won’t execute the command. It’s like a Command Helpfile.

For Example :

The Command :

Get-Help Enable-SPFeature

will output the following.

image

 

As shown in the example above, you can also use the –examples , –detailed or –full switches which shows valuable info. I normally use –examples. Example, this line :

Get-Help Enable-SPFeature –Examples

will output :

 

image

 

Another important thing about PS to remember is that TAB is your autocomplete (Same as old MS-DOS).

For example typing get-sp and pressing TAB will alternate through all CMDlets which starts wit Get-SP

Hope this has helped

Categories: Powershell, Sharepoint
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: