Search code examples
javascriptjsongetjsongoogle-visualization

how to manupulate json data in javascript dynamically?


following is my json data when i hit url.

[
 {
    "date": "07-APR-16",
    "total": 6,
    "acceptedCount": 0,
    "submittedCount": 0,
    "rejectedCount": null,
    "createdCount": 1
},
{
    "date": "03-APR-16",
    "total": 2,
    "acceptedCount": 0,
    "submittedCount": 0,
    "rejectedCount": null,
    "createdCount": 2
},
{
    "date": "06-APR-16",
    "total": 13,
    "acceptedCount": 0,
    "submittedCount": 5,
    "rejectedCount": null,
    "createdCount": 0
},
{
    "date": "01-APR-16",
    "total": 20,
    "acceptedCount": 0,
    "submittedCount": 13,
    "rejectedCount": null,
    "createdCount": 0
},
{
    "date": "29-MAR-16",
    "total": 10,
    "acceptedCount": 0,
    "submittedCount": 8,
    "rejectedCount": null,
    "createdCount": 0
},
{
    "date": "04-APR-16",
    "total": 5,
    "acceptedCount": 0,
    "submittedCount": 3,
    "rejectedCount": null,
    "createdCount": 0
},
{
    "date": "31-MAR-16",
    "total": 30,
    "acceptedCount": 0,
    "submittedCount": 28,
    "rejectedCount": null,
    "createdCount": 0
},
{
    "date": "30-MAR-16",
    "total": 5,
    "acceptedCount": 0,
    "submittedCount": 4,
    "rejectedCount": null,
    "createdCount": 0
 }
]

following is part of my javascript

  var data = google.visualization.arrayToDataTable([
   ['Day', 'Submitted', 'Accepted', 'Rejected', 'Created'],
   ['29-MAR-16', 100, 410, 230, 40],
   ['30-MAR-16', 170, 346, 302, 430],
   ['31-MAR-16', 60, 100, 130, 40],
   ['1-APRIL-16', 302, 350, 520, 40]
   ]);

how should i map each object of json in appropriate row?? anyone can help me in this? no of rows may increase dynamically .exapmle now it is 4 maybe after sometime it may become 8 it depends upon json data

i added my html file could you please give me full solution.

<!DOCTYPE html>
<html>
<head>
    <title>Analytics of Payment Service</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("visualization", "1", {
  packages: ["corechart", "bar"]
});
google.setOnLoadCallback(function(){
    drawChart()
    drawChart3()
    drawChart2()
})



function drawChart() {

  var data = google.visualization.arrayToDataTable([
    ['Day', 'Submitted', 'Published', 'Rejected', 'Created'],
    ['29-MAR-16', 100, 410, 230, 40],
    ['30-MAR-16', 170, 346, 302, 430],
    ['31-MAR-16', 60, 100, 130, 40],
    ['1-APRIL-16', 302, 350, 520, 40]
  ]);


  var options = {
    chart: {
      title: 'Company Performance',
      subtitle: 'Sales, Expenses, and Profit: 2014-2017',
    },
    bars: 'horizontal', // Required for Material Bar Charts.
    hAxis: {
      format: 'decimal'
    },
    height: 500,
    colors: ['#1b9e77', '#d95f02', '#7570b3', '#db7f0b']
  };

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

  chart.draw(data, google.charts.Bar.convertOptions(options));

}




function drawChart3() {
  var data3 = google.visualization.arrayToDataTable([
    ['Day', 'Sales'],
    ['4-APRIL-16', 1000],
    ['5-APRIL-16', 1170],
    ['6-APRIL-16', 660],
    ['7-APRIL-16', 1030]
  ]);

  var options3 = {
    title: 'Company Performance',
    curveType: 'function',
    legend: {
      position: 'bottom'
    }
  };

  var chart3 = new google.visualization.LineChart(document.getElementById('curve_chart'));

  chart3.draw(data3, options3);
}



function drawChart2() {
  var data2 = google.visualization.arrayToDataTable([
    ['Day', 'CreaditCard', 'Invoice'],
    ['4-APRIL-16', 1000, 400],
    ['5-APRIL-16', 1170, 460],
    ['6-APRIL-16', 660, 1120],
    ['7-APRIL-16', 1030, 540]
  ]);

  var options2 = {
    title: 'CreaditCard/Invoice Details',
    curveType: 'function',
    legend: {
      position: 'bottom'
    }
  };

  var chart2 = new google.visualization.LineChart(document.getElementById('cc/invoice'));

  chart2.draw(data2, options2);
}



    </script>

</head>
<body>

<div id="chart_div1" style="width: 900px; height: 500px;"></div>

<div id="curve_chart" style="width: 900px; height: 500px"></div>

<div id="cc/invoice" style="width: 900px; height: 500px"></div>

#foreach($item in ${chartBarForSubmittedManid})
count=$!{item.count}
#end
</body>
</html>


Solution

  • You have to Parse that JSON response data (the one returned by URL) using jQuery, JSON, etc.

    var listOfData = $.parseJSON(responsedata); //jQuery
    var listOfData  = JSON.parse(responsedata); // JSON
    

    the listOfData will be an array containing objects. You need to loop into it to build the array output you are targeting.

    var outputArray = new Array();
    // header
    var headerArray = ['Day', 'Submitted', 'Accepted', 'Rejected', 'Created'];
    outputArray.push(headerArray);
    for (var i = 0; i < listOfData.length; i++){
        var obj = listOfData[i];
        var innerArray = new Array();
        innerArray[0] = obj.date;
        innerArray[1] = obj.submittedCount;
        innerArray[2] = obj.acceptedCount;
        innerArray[3] = obj.rejectedCount;
        innerArray[4] = obj.createdCount;
        outputArray.push(innerArray);        
    }
    

    Now then use the output on this line of your code

    var data = google.visualization.arrayToDataTable(outputArray);