0

I want to create chart from List in SharePoint 2013 with:

http://www.chartjs.org/

and followed : https://www.simego.com/Blog/2013/09/Charts-and-Office-365-SharePoint-Online

blog but it is not working

my code is:

<script type="text/javascript"  src="/sites/testdev/SiteAssets/Chart.js" ></script>     
<script type="text/javascript">
    var collListItem; //ListItems
var chartX = [];  //X-Axis Labels
var chartY = [];  //Y-Axis Values

var chartJs = "/sites/testdev/SiteAssets/Chart.js";
var listName = "TestC"; //Data List Name
var xAxisName = "Title";    //X-Axis Label Names from List
var yAxisName = "Values";    //Y-Axis Values from List
var chartId = "myChart";    //Chart Canvas Div

SP.SOD.executeOrDelayUntilScriptLoaded(function() {

var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle(listName);

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View></View>');
this.collListItem = oList.getItems(camlQuery);
clientContext.load(collListItem);
clientContext.executeQueryAsync(function() {
var listItemEnumerator = collListItem.getEnumerator();

    //Create Points from ListData
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        chartX.push(oListItem.get_item(xAxisName));
        chartY.push(oListItem.get_item(yAxisName));
    }

    //Load Chart JS
    loadJS(chartJs, function() {
        //Generate Data
        var data = {
            labels: this.chartX,
            datasets: [
                {
                    data: this.chartY
                }
            ]
        };

        //Display Chart
        var ctx = document.getElementById(chartId).getContext("2d");
        var myNewChart = new Chart(ctx).Bar(data);
      });

    }, null);

 }, 'SP.js');

 function loadJS(src, callback) {
  var s = document.createElement('script');
  s.src = src;
  s.async = true;
  s.onreadystatechange = s.onload = function() {
    var state = s.readyState;
    if (!callback.done && (!state || /loaded|complete/.test(state))) {
        callback.done = true;
        callback();
    }
  };
 document.getElementsByTagName('head')[0].appendChild(s);
 }
</script>
1
  • Try using chartjs version 1.0.1 Commented Mar 8, 2017 at 13:31

1 Answer 1

1

You can use the below option :

Create Google Chart from SharePoint list:

  1. Download the code from http://www.aasoftech.com/download

  2. Unzip file: Create-Google-LineChart-WithQueryString.zip

  3. Upload all txt file in a document library

  4. In a page you want to display the chart add Content Editor Web part

  5. Provide the link to one of the chart file located at your document library like Create-Google-PieChart-WithQueryString.txt enter image description here

  6. Save the web part

  7. In a page you want to display get data from SharePoint List add Content Editor Web part

  8. Provide the link to Create-GoogleChart-Using-SharePoint-List.txt

  9. You can either use the query string to get to SharePoint List page like: CreateBarChartFromList.aspx?ChartURL=Create-Google-PieChart-WithQueryString.aspx&ListName=Sales&FieldsName=Year|Sales

Where ChartURL is the URL of the chart page and ListName is the name of the list used to create the chart.

This list should exist in your current site.

FieldsName is the list of field used in the chart separated by "|". Field names are case sensitive and should exist in your list or do the following customization:

Customization:

  1. Open Create-GoogleChart-Using-SharePoint-List.txt file
  2. Change the value of your chart page url, SharePoint list, Field names to your own setting in the lines. In this case the values are not retrieved from query string. var ChartPageURL = getParameterByName("ChartURL"); var ListName = getParameterByName("ListName"); var FieldsName = getParameterByName("FieldsName");
  3. Save the file.

Online Demo:

https://aasoftech.sharepoint.com/Demo/SitePages/Create-Google-PieChart-WithQueryString.aspx https://aasoftech.sharepoint.com/Demo/SitePages/Create-Google-BarChart-WithQueryString.aspx https://aasoftech.sharepoint.com/Demo/SitePages/Create-Google-LineChart-WithQueryString.aspx https://aasoftech.sharepoint.com/Demo/SitePages/Create-Google-ColumnChart-WithQueryString.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.