Using Open Source Graph In SharePoint/O365

Introduction
 
It has been a long-standing problem to represent real-time data from SharePoint lists. Microsoft has introduced PowerBI as a powerful reporting solution, but it comes with an extra cost. There is a dependency on your organization's infrastructure team to implement integration with PowerBI if you are on premise.

For simple and quick graphs, Chart.js and Google Charts are considered quick and robust solutions.

In this article, I will be covering Chart.js.

Background
 
Many organizations don't allow deployment of SPFx or other custom solutions. Usage of Script Editor Webpart can also provide the same solution. You may need to set up the solution by giving permission in a way that the users are unable to delete the webparts.
 
Prerequisites 
 
Knowledge of SharePoint RestAPI, jQuery, and Chart.js.
 
Create a SharePoint List
 
Create a SharePoint list using the below columns of type text. Add some sample data.
 
Add the Script Editor webpart to this SharePoint page. Add the below snippet.
 
Using the Code
 
Script Editor webpart renders graph.js and the REST API fetches the data from the SharePoint list.
 
Then, the data is boundd to chart.js and is represented in graphical format.
  1. <canvas height="100" id="myChart" width="300"></canvas> // rendering chartjs  
  2. //using cdn for chartjs  
  3.   
  4. <script>  
  5. var records = [0,0,0,0];  
  6.   
  7. $(function () {  
  8. //waiting for sharepoint page to load  
  9. ExecuteOrDelayUntilScriptLoaded(queryData, "sp.js");  
  10.        
  11. });  
  12.   
  13. function queryData() {  
  14.   
  15. //looping the countries  
  16. var region = ['India','China','United States','United Kingdom'];  
  17. $.each(region, function (index, value) {  
  18.    
  19. var siteUrl = _spPageContextInfo.webAbsoluteUrl;  
  20. //setting up rest query  
  21. var fullUrl = siteUrl + "/_api/web/lists/GetByTitle('Incidents')/items/  
  22.               ?$top=5000&$select=Country&$filter=Country eq "  +"'"+ value +"'";  
  23.   
  24. $.ajax({  
  25. url: fullUrl,  
  26. type: "GET",  
  27. headers: {  
  28. "accept""application/json;odata=verbose",  
  29. "content-type""application/json;odata=verbose",  
  30. },  
  31. success: function onQuerySucceeded(sender, args) {  
  32.   
  33.     //fetching results   
  34. records[index] = sender.d.results.length;  
  35.   
  36. var ctx = document.getElementById("myChart").getContext('2d');  
  37. //populating and configuring chartjs  
  38. var myChart = new Chart(ctx, {  
  39.     //define type of chart  
  40.     type: 'bar',  
  41.     data: {  
  42.         labels: ['India','China','United States','United Kingdom'],  
  43.         //setting data source  
  44.         datasets: [  
  45.             {  
  46.             //setting header  
  47.             label: '# of Incidents by Country',  
  48.             data: [parseInt(records[0]), parseInt(records[1]),   
  49.                    parseInt(records[2]), parseInt(records[3])],  
  50.             //setting background color of bars  
  51.             backgroundColor: [  
  52.                 'rgba(255, 99, 132, 0.2)',  
  53.                 'rgba(54, 162, 235, 0.2)',  
  54.                 'rgba(255, 206, 86, 0.2)',  
  55.                 'rgba(75, 192, 192, 0.2)',                  
  56.             ],  
  57.             //setting border color of bars  
  58.             borderColor: [  
  59.                 'rgba(255,99,132,1)',  
  60.                 'rgba(54, 162, 235, 1)',  
  61.                 'rgba(255, 206, 86, 1)',  
  62.                 'rgba(75, 192, 192, 1)',                 
  63.             ],  
  64.             //setting border width  
  65.             borderWidth: 1  
  66.         }  
  67.        ]  
  68.     },  
  69.     options: {  
  70.     //setting title  
  71.     title: {   
  72.                     display: true,   
  73.                     text: 'No of Incidents'   
  74.            },   
  75.                   
  76.                 layout: {  
  77.             padding: {  
  78.                 left: 0,  
  79.                 right: 0,  
  80.                 top: 0,  
  81.                 bottom: 0  
  82.             }  
  83.         },  
  84.     tooltips: {  
  85.             //mode: 'dataset'  
  86.             //mode: 'y'  
  87.              mode: 'index'  
  88.         }  
  89. ,  
  90.     events:["mousemove""mouseout""click""touchstart""touchmove""touchend"] ,  
  91.         scales: {  
  92.             yAxes: [{  
  93.             stacked: true,  
  94.                 ticks: {  
  95.                     beginAtZero:true  
  96.                 }  
  97.             }]  
  98.         } ,  
  99.          legend: {  
  100.             display: true,  
  101.             labels: {  
  102.                 fontColor: '#0b6623',  
  103.                 backgroundColor :'rgba(11, 102, 35, 0.2)'  
  104.             }  
  105.         }         
  106.     }  
  107. });  
  108. },  
  109.   
  110. error: function onQueryFailed(sender, args) {  
  111.   
  112. alert('Error Retrieving Chart Data' + sender.responseText);  
  113. console.log(sender.responseText);  
  114. }  
  115. });  
  116. });  
  117. }  
  118. </script>  
Understanding Code
 
Please see the comments in the above code for an explanation of the flow of data.
 
Script Editor Web Part
 
Let us have a nice view of data from the custom list in a SharePoint Script Editor webpart.
Reference
 
https://www.chartjs.org/