Client-Side Rendering was introduced in SharePoint 2013. The primary purpose of CSR is to provide the conditional formatting of the data present within the List Views. Prior to SharePoint 2013, XSLT formatting was the way to implement the conditional formatting of List Views. XSLT formatting requires an in depth knowledge of working with XSL and debugging was also cumbersome. However with CSR, we can tap in to the rendering process and override the default properties of the List View, using JavaScript and convert SharePoint List Data to modified HTML.
Some of the properties that can be overridden are given below.
- OnPreRender
- OnPostRender
- View
- Body
- Item
- Fields
- Header
- Footer
OnPrerender allows us to write some logic even before List View is rendered, while OnPostrender allows us to modify List View, once the view has been rendered. Similarly, each of the properties can be overridden during run time to accomplish different List View modifications and at different List View locations.
Chart JS
Chart JS is an open source JavaScript charting library, using which we can create different types of charts by pumping in the data to the chart creation command. We can download the latest version of Chart.js on GitHub or just use these Chart.js CDN links.
What are we going to do?
In this walk through, we will use Chart JS along with Client Side Rendering (CSR) to convert SharePoint List View data into a Radar Chart during the run time. Initially, SharePoint List will have the car sales details of the vehicles.
During the run time, we will make use of JSLink and convert it to Radar Chart representation given below.
You can also see, how we can convert the list view to Bar chart from the article.
Prerequisites
- A SharePoint List with the columns given below. Create a new View named ‘RadarView’, which we will convert to the graphical representation, using CSR and Chart JS.
- Download Chart JS from js on GitHub or just use these Chart.js CDN links. Upload it to SharePoint Site Assets.
- Get jQuery minified version. Upload it to SharePoint Site Assets.
The required JS files are zipped and uploaded along with this article too. You can use it as well.
Implementation
Once the above prerequisites are in place, let’s go to the implementation logic. At a high level, ChartJS works by placing a div in the page and uses the new command to create a chart, using the available values at the particular div. In order to get started, we will require a couple of arrays, which will hold the values from SharePoint List, which will be plotted in Radar Chart.
-
- var RadarChart = RadarChart || {};
- Colors = ['#5e92c1', '#b23a3a', '#b2a839'];
- ChartTitle =[];
- Q1Values = [];
- Q2Values = [];
- Q3Values= [];
- Description = '';
The main starting point function of Client Side Rendering is discussed below. During the runtime, we will override the header, footer and Item properties of the context information object.
- var ContextOverride = {};
- Templates = {};
- Templates.Header = RadarChart.Header;
- Templates.Item = RadarChart.Item;
- Templates.Footer = RadarChart.Footer;
- TemplateManager.RegisterTemplateOverrides(ContextOverride);
An Item override method will run for each SharePoint list item. It will push SharePoint list item values to the variable arrays, which we had declared earlier. These data arrays will be used to plot Radar chart. The Value attribute will store SharePoint Values and Color attribute stores Radar chart color to be associated with it. Description will store the legend details, which will also act as a hyperlink towards the corresponding list item.
- Item = function(ctx) {
- var Q1Val = ctx.CurrentItem.Quarter1.replace(",", "");
- var Q2Val = ctx.CurrentItem.Quarter2.replace(",", "");
- var Q3Val = ctx.CurrentItem.Quarter3.replace(",", "");
- ChartTitle.push(ctx.CurrentItem.Product);
- Q1Values.push(parseInt(Q1Val));
- Q2Values.push(parseInt(Q2Val));
- Q3Values.push(parseInt(Q3Val));
- return '';
- }
Chat JS requires a div element with an Id, which will be used while running the new command to create the chart. This div element will be added to the page by overriding the Header property. In the Footer Property override method, we will create the chart, using new chart command and this command takes in parameter array‘data’, which contains the list data and color schemes to be used.
- Header = function(ctx) {
- return '<canvas id="RadarChart" width="600" height="400" style="float:left;margin-right:20px;"></canvas><divid="chartDescription"></div>';
- }
-
- Footer = function() {
- debugger;
- var data = {
- labels: RadarChart.ChartTitle,
- datasets: [{
- fillColor: RadarChart.Colors[0],
- strokeColor: "#6e6b70",
- pointColor: RadarChart.Colors[0],
- pointStrokeColor: "#fff",
- data: RadarChart.Q1Values
- }, {
- fillColor: RadarChart.Colors[1],
- strokeColor: "#6e6b70",
- pointColor: RadarChart.Colors[1],
- pointStrokeColor: "#fff",
- data: RadarChart.Q2Values
- }, {
- fillColor: RadarChart.Colors[2],
- strokeColor: "#6e6b70",
- pointColor: RadarChart.Colors[2],
- pointStrokeColor: "#fff",
- data: RadarChart.Q3Values
- }]
- }
-
- var options = {
- scaleOverride: true,
- scaleSteps: 12,
- scaleStepWidth: 1000
- };
- var chart = $("#RadarChart").get(0).getContext("2d");
- new Chart(chart).Radar(data, {
- animationSteps: 15
- });
It is new chart command, which will create Radar Chart with the provided parameters. We can customize Radar chart by adding the legends responsiveness. The other configurable properties can be set to the chart, as described in the official documentation here.
Full code
-
- var RadarChart = RadarChart || {};
- Colors = ['#5e92c1', '#b23a3a', '#b2a839'];
- ChartTitle = [];
- Q1Values = [];
- Q2Values = [];
- Q3Values = [];
- Description = '';
-
- BuildChart = function() {
- var ContextOverride = {};
- Templates = {};
- Templates.Header = RadarChart.Header;
- Templates.Item = RadarChart.Item;
- Templates.Footer = RadarChart.Footer;
- TemplateManager.RegisterTemplateOverrides(ContextOverride);
- };
-
- Item = function(ctx) {
- var Q1Val = ctx.CurrentItem.Quarter1.replace(",", "");
- var Q2Val = ctx.CurrentItem.Quarter2.replace(",", "");
- var Q3Val = ctx.CurrentItem.Quarter3.replace(",", "");
- ChartTitle.push(ctx.CurrentItem.Product);
- Q1Values.push(parseInt(Q1Val));
- Q2Values.push(parseInt(Q2Val));
- Q3Values.push(parseInt(Q3Val));
- return '';
- }
-
- Header = function(ctx) {
- return '<canvas id="RadarChart" width="600" height="400" style="float:left;margin-right:20px;"></canvas><divid="chartDescription"></div>';
- }
-
- Footer = function() {
- debugger;
- var data = {
- labels: RadarChart.ChartTitle,
- datasets: [{
- fillColor: RadarChart.Colors[0],
- strokeColor: "#6e6b70",
- pointColor: RadarChart.Colors[0],
- pointStrokeColor: "#fff",
- data: RadarChart.Q1Values
- }, {
- fillColor: RadarChart.Colors[1],
- strokeColor: "#6e6b70",
- pointColor: RadarChart.Colors[1],
- pointStrokeColor: "#fff",
- data: RadarChart.Q2Values
- }, {
- fillColor: RadarChart.Colors[2],
- strokeColor: "#6e6b70",
- pointColor: RadarChart.Colors[2],
- pointStrokeColor: "#fff",
- data: RadarChart.Q3Values
- }]
- }
-
- var options = {
- scaleOverride: true,
- scaleSteps: 12,
- scaleStepWidth: 1000
- };
- var chart = $("#RadarChart").get(0).getContext("2d");
- new Chart(chart).Radar(data, {
- animationSteps: 15
- });
- Description += '<h2><span style="color:' + RadarChart.Colors[0] + ';font-weight:italic;">Quarter1 Sales</span><h2>';
- Description += '<h2><span style="color:' + RadarChart.Colors[1] + ';font-weight:italic;">Quarter2 Sales</span><h2>';
- Description += '<h2><span style="color:' + RadarChart.Colors[2] + ';font-weight:italic;">Quarter3 Sales</span><h2>';
- $('#chartDescription').html(RadarChart.Description);
- return '';
- }
-
- $(document).ready(RadarChart.BuildChart());
Add JS Link
We can save the code given above to a JS file and upload it to Site Assets within SharePoint Site. We can then go to List View’s "Edit" page by appending “?toolpaneview=2” . In the Edit page of the View, go to Edit properties of the List View and add the JS file in the JSLink section. We will be adding reference to 3 JS files, using the short hand.
~site/siteassets/jquery-1.12.1.min.js|~site/siteassets/Chart.js|~site/siteassets/Radar.js
The JS files, which are referenced as JSLink are given below.
- jQuery file, which we have uploaded to Site assets library.
- JS file, which is uploaded to same Site Assets Library.
- JS file, which contains our override and chart creation logic, which is uploaded to SiteAssets.
We can add it to other locations as well and make the changes in JSLink URL accordingly. Once we save the page after adding the JSLink, the List View will be rendered as a Radar Chart, as shown below.
Summary
Thus, we saw how to convert a SharePoint List View to Radar Chart, using Client Side Rendering and ChartJS in SharePoint Server 2016 and SharePoint Online.