Sunday, 9 April 2017

Parsing recordset in JavaScript

I'm trying to display a live dashboard using Google Charts on my localhost webpage, harvesting data from an SQL Server table. I successfully connected to the DB and displayed the data on localhost.

Where I'm stuck is, in order to draw the pie chart in Google Charts, I need to take the 4 integers I receive from the DB query and make them a percentage. My issue is parsing the DB response so I can access each integer individually.

My response in localhost looks like this:

 {
"recordsets": [
[
{
"Braking_Events": 1441,
"Acceleration_Events": 2480,
"Cornering_Events": 1270,
"Speeding_Events": 2863
}
]
],
"recordset": [
{
"Braking_Events": 1441,
"Acceleration_Events": 2480,
"Cornering_Events": 1270,
"Speeding_Events": 2863
}
],
"output": {},
"rowsAffected": [
1
]
}     

I am trying to access

"Braking_Events": 1441,
"Acceleration_Events": 2480,
"Cornering_Events": 1270,
"Speeding_Events": 2863

and set them to JavaScript variables, manipulate them into a percentage and stick them into

 function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Braking_Events', 1441],
      ['Acceleration_Events',     2480],
      ['Cornering_Events',      1270],
      ['Speeding_Events',  22863]
    ]);

    var options = {
      title: 'Safety Distribution'
    };

    var chart = new google.visualization.PieChart(document.getElementById('piechart'));

    chart.draw(data, options);
  }



via Kobbi Gal

No comments:

Post a Comment