Overview of Google Charts

Google Chart tools are powerful, simple to use, and free. They have many features namely:

Basic Library Loading

  1. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  2. <script type="text/javascript">
  3. google.charts.load('current', {
  4. packages: ['corechart']
  5. });
  6. google.charts.setOnLoadCallback(drawChart);...
  7. </script>

Basic Chart Implementation in HTML page

The code given below generates Pie Chart.

  1. <html>
  2. <head>
  3. <!--Load the AJAX API-->
  4. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  5. <script type="text/javascript">
  6. // Load the Visualization API and the piechart package.
  7. google.charts.load('current', {
  8. packages: ['corechart']
  9. });
  10. // Set a callback to run when the Google Visualization API is loaded.
  11. google.charts.setOnLoadCallback(drawChart);
  12. // Callback that creates and populates a data table,
  13. // instantiates the pie chart, passes in the data and
  14. // draws it.
  15. function drawChart() {
  16. // Create the data table.
  17. var data = new google.visualization.DataTable();
  18. data.addColumn('string', 'Topping');
  19. data.addColumn('number', 'Slices');
  20. data.addRows(4);
  21. data.setCell(0, 0, "mashroom");
  22. data.setCell(0, 1, 4);
  23. data.setCell(1, 0, "Cheese");
  24. data.setCell(1, 1, 3);
  25. data.setCell(2, 0, "Mixedveg");
  26. data.setCell(2, 1, 2);
  27. data.setCell(3, 0, "Merghartia");
  28. data.setCell(3, 1, 1);
  29. // Set chart options
  30. var options = {
  31. 'title': 'How Much Pizza I Ate Last Night',
  32. 'width': 400,
  33. 'height': 300
  34. };
  35. // Instantiate and draw our chart, passing in some options.
  36. var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  37. chart.draw(data, options);
  38. }
  39. </script>
  40. </head>
  41. <body> //Div that will hold the pie chart
  42. <div id="chart_div" style="width:400; height:300"></div>
  43. </body>
  44. </html>

To set the data, we are using google.visualization.datatable, which stores the data in tabular structure and in the example given above for each of the cells, we are setting the value after which we are providing the title, width and height of the chart to displayed

Finally, creating the instance of google.visualization.piechart by passing an appropriate div element, which holds the Pie Chart, calling chart.draw() function will generate the chart.

Preparation of SharePoint data for Google Charts

In order to implement the Google Chart in SharePoint we must first create a data source which can used to generate the chart and can be dynamic too. To create a data source we can create a custom list with two columns of type strings and numbers respectively. String type column is to hold labels for pie chart and number type is for the value for each of the labels.

To fetch the data from SharePoint list, JavaScript object model can be used and the code given below demonstrates the fetching of data from the custom list and pushing the values in to two arrays, one which holds labels for the chart and other the values (tasks array is holding the labels, hours array is holding the values).

  1. var hours = [];
  2. var tasks = [];
  3. SP.SOD.executeFunc('sp.js', 'SP.ClientContext', Test);
  4. function Test() {
  5. var clientcontext = new SP.ClientContext();
  6. var web = clientcontext.get_web();
  7. var list = web.get_lists().getByTitle('ChartList'); //replace this with your list name //
  8. listitems = list.getItems(SP.CamlQuery.createAllItemsQuery());
  9. clientcontext.load(listitems);
  10. clientcontext.executeQueryAsync(success, failure);
  11. }
  12. function success() {
  13. var listitemenumerator = listitems.getEnumerator();
  14. while (listitemenumerator.moveNext()) {
  15. var item = listitemenumerator.get_current().get_item('Title'); // replace with your column name//
  16. tasks.push(item);
  17. var itemvalue = listitemenumerator.get_current().get_item('Duration'); // replace with your column name//
  18. hours.push(itemvalue);
  19. }
  20. }
  21. function failure() {
  22. alert("request failed" + args.get_message());
  23. }

It is a straightforward implementation to fetch the list items from the list. We are saving this JavaScript file and uploading it to site assets library from which we can refer this file to the main HTML file.

Main HTML

  1. <html>
  2. <head>
  3. <meta http-equiv='cache-control' content='no-cache'>
  4. <meta http-equiv='expires' content='0'>
  5. <meta http-equiv='pragma' content='no-cache'>
  6. <script src="test.js"></script>
  7. <!—replace with your path where js file is located in sharepoint!>
  8. <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
  9. <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
  10. <script type="text/javascript">
  11. $(document).ready(function() {
  12. google.charts.load('current', {
  13. 'packages': ['corechart']
  14. });
  15. google.charts.setOnLoadCallback(drawChart);
  16. });
  17. function drawChart() {
  18. var data = new google.visualization.DataTable();
  19. data.addColumn('string', 'Task');
  20. data.addColumn('number', 'Hours Per Day');
  21. if (hours.length > 1) {
  22. data.addRows(hours.length);
  23. for (var i = 0; i <= hours.length - 1; i++) {
  24. data.setCell(i, 0, tasks[i]);
  25. data.setCell(i, 1, hours[i]);
  26. }
  27. }
  28. var options = {
  29. title: 'My Daily Activities'
  30. };
  31. var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  32. chart.draw(data, options);
  33. }
  34. </script>
  35. </head>
  36. <body>
  37. <div id="piechart" style="width: 900px; height: 500px"></div>
  38. </body>
  39. </html>

Here in JavaScript code given above, we are setting the data, which we obtained from the referenced JavaScript file.

The code given above is used in content editor Web part to generate the Pie Chart and the final result would be as shown below.

Pie Chart shows casing my daily activities