In this article, we will see how we can represent SharePoint data in graphical form.

Let's start.

Create a custom SharePoint list 'TaskDetails' with the below details.
Column NameType
TitleSingle line
statusChoice
Add list items into 'TaskDetails'.
Then add the below HTML elements on the page.
  1. <div>
  2. <canvas id="AssetStatus" width="500" height="500"></canvas>
  3. </div>
  4. <div id="chartjs-legend" class="noselect" ></div>
Add reference for jQuery and ChartJS plugin.
  1. <script type="text/javascript" src="jquery-1.7.2.min.js"> </script>
  2. <script type="text/javascript" src="Chart.js"></script>
Code for getting SharePoint 'TaskDetails' list data using Rest API.
  1. $.ajax({
  2. url: "<Add SharePoint Site Url>/_api/Web/Lists/GetByTitle('TaskDetails')/items",
  3. type: "Get",
  4. headers: {
  5. "accept": "application/json;odata=verbose",
  6. },
  7. success: function (data) {
  8. LoadChart(data);
  9. },
  10. error: function (err) {
  11. //show error
  12. }
  13. });
Convert data into chartJS dataset and load the chart.
  1. var LoadChart = function (data) {
  2. var items = [];
  3. var statuses = [];
  4. var chartData = [];
  5. var temp = "";
  6. //Loop through items to find unique statuses
  7. for (var i = 0; i < data.d.results.length; i++) {
  8. items.push(data.d.results[i]);
  9. if (statuses.filter(findStatus(data.d.results[i].status)).length > 0) {
  10. continue;
  11. }
  12. temp = data.d.results[i].status;
  13. statuses.push(temp);
  14. }
  15. function findStatus(arg) {
  16. return function (items) {
  17. return items.indexOf(arg) > -1;
  18. }
  19. }
  20. var clrs = ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850"];
  21. for (var i = 0; i < statuses.length; i++) {
  22. var tempCount = items.filter(findStatusInData(statuses[i])).length;
  23. var object = {
  24. value: tempCount,
  25. color: clrs[i],
  26. label: statuses[i]
  27. }
  28. chartData.push(object);
  29. }
  30. function findStatusInData(arg) {
  31. return function (items) {
  32. return items.status.indexOf(arg) > -1;
  33. }
  34. }
  35. var ctx = document.getElementById('AssetStatus').getContext("2d");
  36. var chart = new Chart(ctx).Doughnut(chartData, {});
  37. document.getElementById("chartjs-legend").innerHTML = chart.generateLegend();
  38. }
The Output will be as below.

Output

The integrated code will be as below.
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <script type="text/javascript" src="jquery-1.7.2.min.js"> </script>
  5. <script type="text/javascript" src="Chart.js"></script>
  6. <script type="text/javascript" >
  7. $.ajax({
  8. url: "<Add SharePoint Site Url>/_api/Web/Lists/GetByTitle('TaskDetails')/items",
  9. type: "Get",
  10. headers: {
  11. "accept": "application/json;odata=verbose",
  12. },
  13. success: function (data) {
  14. LoadChart(data);
  15. },
  16. error: function (err) {
  17. //show error
  18. }
  19. });
  20. var LoadChart = function (data) {
  21. var items = [];
  22. var statuses = [];
  23. var chartData = [];
  24. var temp = "";
  25. //Loop through items to find unique statuses
  26. for (var i = 0; i < data.d.results.length; i++) {
  27. items.push(data.d.results[i]);
  28. if (statuses.filter(findStatus(data.d.results[i].status)).length > 0) {
  29. continue;
  30. }
  31. temp = data.d.results[i].status;
  32. statuses.push(temp);
  33. }
  34. function findStatus(arg) {
  35. return function (items) {
  36. return items.indexOf(arg) > -1;
  37. }
  38. }
  39. var clrs = ["#3e95cd", "#8e5ea2", "#3cba9f", "#e8c3b9", "#c45850"];
  40. for (var i = 0; i < statuses.length; i++) {
  41. var tempCount = items.filter(findStatusInData(statuses[i])).length;
  42. var object = {
  43. value: tempCount,
  44. color: clrs[i],
  45. label: statuses[i]
  46. }
  47. chartData.push(object);
  48. }
  49. function findStatusInData(arg) {
  50. return function (items) {
  51. return items.status.indexOf(arg) > -1;
  52. }
  53. }
  54. var ctx = document.getElementById('AssetStatus').getContext("2d");
  55. var chart = new Chart(ctx).Doughnut(chartData, {});
  56. document.getElementById("chartjs-legend").innerHTML = chart.generateLegend();
  57. }
  58. </script>
  59. <style type="text/css">
  60. ol,
  61. ul {
  62. list-style: none;
  63. margin: 0;
  64. padding: 0;
  65. text-align: center;
  66. }
  67. li {
  68. display: inline-table;
  69. }
  70. .noselect {
  71. -webkit-touch-callout: none;
  72. -webkit-user-select: none;
  73. -khtml-user-select: none;
  74. -moz-user-select: none;
  75. -ms-user-select: none;
  76. user-select: none;
  77. }
  78. #chartjs-legend {
  79. position: absolute;
  80. width: 100%;
  81. bottom: 10%;
  82. }
  83. #chartjs-legend li {
  84. cursor: pointer;
  85. margin: 10px 4px;
  86. }
  87. #chartjs-legend li span {
  88. position: relative;
  89. padding: 6px 20px;
  90. border-radius: 13px;
  91. color: white;
  92. z-index: 2;
  93. }
  94. </style>
  95. <meta charset="utf-8" />
  96. <title></title>
  97. </head>
  98. <body>
  99. <div>
  100. <canvas id="AssetStatus" width="500" height="500"></canvas>
  101. </div>
  102. <div id="chartjs-legend" class="noselect" ></div>
  103. </body>
  104. </html>
Refer to the attached resources for more.