phpjsongoogle-visualizationpygooglechart

How to create row for Google's calendar graph api from JSON Object


I received JSON Object in my console as follows: enter image description here

and I want to convert this JSON Object so that I will pass this to Google's calendar graph API's addRows() function.

My Script is as follows:

function drawChart() {
   var dataTable = new google.visualization.DataTable();
   dataTable.addColumn({ type: 'date', id: 'Date' });
   dataTable.addColumn({ type: 'number', id: 'Won/Loss' });
   dataTable.addRows([
        //HERE TO ADD DATA LIKE [new Date(2017, 09, 19),9],[new Date(2017, 09, 20),9]
    ]);

   var chart = new google.visualization.Calendar(document.getElementById('calendar_basic'));

   var options = {
     title: "Daily hours utilization",
     height: 350,
   };

   chart.draw(dataTable, options);

So basically I want to convert

{today_date: "2017-09-19", hours: "9"}

To

[ new Date(2017, 9, 19), 9 ],

and Output like enter image description here


Solution

  • Essentially you're looking to convert the date string into a date object.

    One method of achieving this would be to loop over your json data structure and build a rows array from that. Once this loop has finished you can pass the rows array to the addRows method.

    Lets assume your json data structure is stored in a variable called $jsonData

    //Store processed rows in this array
    $rows = [];
    
    //Sample json data structure (pull yours from the source)
    $jsonData = [
        {
        today_date: "2017-09-19",
        hours: "9"
      },
      {
        today_date: "2017-09-20",
        hours: "9"
      },
    ];
    
    //Loop over every object within the json data, parsing the date string and
    //creating a date object from it.
    //Add the date object and the hours field to the rows array
    $jsonData.forEach(function($data){
        $dateElements = $data.today_date.split("-");
        $rows.push([
          new Date($dateElements[0], $dateElements[1], $dateElements[2]),
          $data.hours
      ]);
    });
    
    //Using your code above, add the rows
    var dataTable = new google.visualization.DataTable();
    dataTable.addColumn({ type: 'date', id: 'Date' });
    dataTable.addColumn({ type: 'number', id: 'Won/Loss' });
    dataTable.addRows($rows);
    

    I have created a little jsfiddle so you can see the output of the rows array from my test data: https://jsfiddle.net/JParkinson1991/aukwxo6L/

    Hope this helps!


    EDIT: From Abhijit Kumbhar

    With help of your guideline I edited my code and it works fine now, some changes I did they are as follows:

    Final solution:

    function drawChart() {
        var rows =[];
    
        jsDailyCount.forEach(function(data){
            var dateElements = data.today_date.split("-");
            rows.push([
                new Date(dateElements[0], dateElements[1], dateElements[2]),
                parseFloat(data.hours)
            ]);
        });
    
        var dataTable = new google.visualization.DataTable();
        dataTable.addColumn({ type: 'date', id: 'Date' });
        dataTable.addColumn({ type: 'number', id: 'hours' });
        dataTable.addRows(rows);
    
        var chart = new google.visualization.Calendar(document.getElementById('calendar_basic'));
        var options = {
            title: "Daily hours utilization",
            height: 350,
        };
       chart.draw(dataTable, options);