1

I am trying to generate a bar graph based on the count of an instance in a Project Status. The "if..." statement works beautifully, counting all elements containing the string "Complete", outputting on the console and graphing it. However, "else if..." returns nothing on the console and graphs nothing. I am dumbfounded as to what the issue might be. Any insight?

google.charts.load('current', {
  'packages': ['bar']
});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
  var complete = 0;
  var deferred = 0;
  var onhold = 0;
  var uri = "https://company.com/sites/ITApp/_api/Web/Lists/getByTitle('Global%20Projects')/items";
  $.ajax({
    url: uri,
    type: "GET",
    async: false,
    data: {},
    cache: true,
    headers: {
      "ACCEPT": "application/json;odata=verbose"
    },
    success: function(data) {
      var dataResults = data.d.results;
      for (i = 1; i < dataResults.length; i++) {
        if (dataResults[i].Project_x0020_Status == "Completed") {
          complete = complete + 1;
        } else if (dataResults[i].Project_x0020_Status == "Deferred") {
          console.log(deferred);
          deferred = deferred + 1;
        } else if (dataResults[i].Project_x0020_Status == "On-Hold") {
          onhold = onhold + 1;
        } else {}
      }

    }

  });

  var data = google.visualization.arrayToDataTable([
        ['Category', 'Complete', 'Deferred', 'On-Hold'],
        ['Project Status', complete, deferred, onhold],


        var options = {
          chart: {
            title: 'Company Performance',
            subtitle: 'Sales, Expenses, and Profit: 2014-2017',
          }
        };

        var chart = new google.charts.Bar(document.getElementById('columnchart_material'));

        chart.draw(data, google.charts.Bar.convertOptions(options));
      }
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="columnchart_material" style="width: 800px; height: 500px;"></div>

UPDATE: I believe this is a SharePoint issue at this point. When I go to my uri and search for "Deferred" and "On-Hold" in the XML file there, I cannot find either of them. However, when I go to the actual list containing the data on SharePoint, I can find it. Might this be an issue of the table not being translated correctly in XML after import?

Jnewbie
  • 25
  • 4
  • Move the chart into `success` function. – Brahma Dev Oct 30 '17 at 21:47
  • What does your data look like? Have you logged `dataResults[i].Project_x0020_Status` to make sure it contains what you think it should – Patrick Evans Oct 30 '17 at 21:49
  • Possible duplicate of [How to return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Brahma Dev Oct 30 '17 at 21:49
  • 2
    @BrahmaDev it's not async though, see async: false. – James Oct 30 '17 at 21:50
  • @PatrickEvans, yes, console.log(dataResults) returns an array containing my data list. – Jnewbie Oct 30 '17 at 21:53
  • @BrahmaDev, moving the chart into the success function yields no change in outcome – Jnewbie Oct 30 '17 at 21:54
  • Something seems to have gotten lost in the code when you copied it here. Look at the initializaiton of `var data`. – Barmar Oct 30 '17 at 21:55
  • 1
    I meant log the actual property you are testing against, ie `console.log( dataResults[i].Project_x0020_Status )` – Patrick Evans Oct 30 '17 at 21:56
  • @PatrickEvans, when I do that, I get a list of values I want to go through and count (i.e. I get what I expect). – Jnewbie Oct 30 '17 at 22:02
  • @Barmar, I don't have anything in "data" property of the AJAX request because I want everything returned from the server. – Jnewbie Oct 30 '17 at 22:04
  • Not the ajax request, the statement that begins with `var data = google.visualization.arrayToDataTable([`. It ends in the middle of the array. – Barmar Oct 30 '17 at 22:13
  • I think the issue is with AJAX call. when I go to https://company.com/sites/ITApp/_api/Web/Lists/getByTitle('Global%20Projects')/items and search "Deferred", nothing comes up. I go to the actual list on my site and the entries exist. – Jnewbie Oct 30 '17 at 22:27

2 Answers2

0

if the value is null, it may not be included as a property on the row

check to make sure it exists, before checking the value

you could also use a switch statement to check the value...

var dataResults = data.d.results;
for (i = 0; i < dataResults.length; i++) {
  if (dataResults[i].hasOwnProperty('Project_x0020_Status')) {
    switch (dataResults[i].Project_x0020_Status) {
      case "Completed":
        complete = complete + 1;
        break;

      case "Deferred":
        deferred = deferred + 1;
        break;

      case "On-Hold":
        onhold = onhold + 1;
        break;
    }
  }
}
WhiteHat
  • 59,912
  • 7
  • 51
  • 133
0

The issue was with the AJAX call, as I suspected. The above code was correct; I only needed to add "?$top=1000" to the link because there is a limitation due to server side paging.

var uri="https://company.com/sites/ITApp/_api/Web/Lists/getByTitle('Global%20Projects')/items?$top=1000"
Jnewbie
  • 25
  • 4