简体   繁体   中英

how to add data in google charts using javascript ?

I am using Google Charts for my website and parse.com for server-side. I have one tag <div id="chart_div"> </div> which draws the chart. For Chart this is Javascript part :

$( document ).ready(function() {
    var currentUser = Parse.User.current();
    console.log( currentUser );

var stepcount = Parse.Object.extend("stepCount");
var query = new Parse.Query(stepcount);

query.equalTo("user", currentUser);
query.find({
  success: function(results) {  
    for (var i = 0; i < results.length; i++) {
      var object = results[i];    
      alert(object.id + ' - ' + object.get('numberOfSteps'));
    }
  },
  error: function(error) {
    alert("Error: " + error.code + " " + error.message);
  }  });  });

function drawMultSeries() {
      var data = new google.visualization.DataTable();
      data.addColumn('string', 'Day');
      data.addColumn('number', 'Motivation Level');
      data.addColumn('number', 'Energy Level');

      data.addRows([
        ['Monday', 1, .25],
        ['Monday', 1, .25],
        ['Monday', 1, .25],
        ['Monday', 1, .25],
        ['Monday', 1, .25],
        ['Monday', 1, .25],
        ['Monday', 1, .25],
      ]);

      var options = {
        title: 'Motivation and Energy Level Throughout the Day',
        hAxis: {
          title: 'Time of Day',
          format: 'h:mm a',
          viewWindow: {
            min: [7, 30, 0],
            max: [17, 30, 0]
          }
        },
        vAxis: {
          title: 'Steps'
        }
      };

      var chart = new google.visualization.ColumnChart(
        document.getElementById('chart_div'));

      chart.draw(data, options);
    }

I am trying to retrieve number of steps for current user, but I don't know how to insert it in the chart.

u can do like this.

   $( document ).ready(function() {
        var currentUser = Parse.User.current();// igebs romeli user aris shemosuli da abrunebs objectId
        console.log( currentUser ); 

    var stepcount = Parse.Object.extend("stepCount"); //database shi class saxeli 

    var query = new Parse.Query(stepcount); 

    query.equalTo("user", currentUser);
    query.find({
      success: function(results) {  


        for (var i = 0; i < results.length; i++) {
          var object = results[i];    
          alert(object.id + ' - ' + object.get('numberOfSteps'));
         //console.log(object.get('numberOfSteps'));

         arrayOfSteps.push(object.get('numberOfSteps'));
         console.log(arrayOfSteps[i] ) ;

        }
      },
      error: function(error) {
        alert("Error: " + error.code + " " + error.message);
      }  });  });

    function drawMultSeries() {

      x = [];

      for(var j=0; j<arrayOfSteps.length;j++){
          x.push(
            ['date', 1, arrayOfSteps[j]]

          );};

          var data = new google.visualization.DataTable();
          data.addColumn('string', 'Day');
          data.addColumn('number', 'Motivation Level');
          data.addColumn('number', 'Energy Level');
          data.addRows([x[0],x[1],x[2],]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM