google / google-visualization-issues

288 stars 35 forks source link

JSON.parse for string array to object conversion when date filed is there #2224

Open rcmeena opened 8 years ago

rcmeena commented 8 years ago

Hi All, I have below output from Ruby Active record to a @systemsLoadStatisticsChart1 variable. output: [['2016-03-27T13:05:03+00:00', 1, 1, 1], ['2016-03-27T13:06:02+00:00', 0, 0, 1], ['2016-03-27T13:07:02+00:00', 0, 0, 1], ['2016-03-27T13:08:02+00:00', 0, 0, 1]]

When I try to use this variable for Google Chart data, it comes in typeof string var data3 = ('%=@systemsLoadStatisticsChart1%'); try { var data4 = JSON.parse(data3);
} catch(err) { document.getElementById("error").innerHTML = err.message; } I am getting below error during JSON.parse due to datetime value filed in String array: JSON.parse: unexpected character at line 1 column 3 of the JSON data

Please let me know how to convert below String to Array object

[[new Date('2016-03-27T13:05:03+00:00'), 1, 1, 1], [new Date('2016-03-27T13:06:02+00:00'), 0, 0, 1], [new Date('2016-03-27T13:07:02+00:00'), 0, 0, 1], [new Date('2016-03-27T13:08:02+00:00'), 0, 0, 1]] Thanks in advance,

dlaliberte commented 8 years ago

Hi Ramesh,

You should use the string formatted dates, as described here: https://developers.google.com/chart/interactive/docs/datesandtimes#dates-and-times-using-the-date-string-representation

On Mon, Mar 28, 2016 at 3:37 AM, rcmeena notifications@github.com wrote:

Hi All, I have below output from Ruby Active record to a @systemsLoadStatisticsChart1 variable. output: [['2016-03-27T13:05:03+00:00', 1, 1, 1], ['2016-03-27T13:06:02+00:00', 0, 0, 1], ['2016-03-27T13:07:02+00:00', 0, 0, 1], ['2016-03-27T13:08:02+00:00', 0, 0, 1]]

When I try to use this variable for Google Chart data, it comes in typeof string var data3 = ('%=@systemsLoadStatisticsChart1%'); try { var data4 = JSON.parse(data3);

} catch(err) { document.getElementById("error").innerHTML = err.message; } I am getting below error during JSON.parse due to datetime value filed in String array: JSON.parse: unexpected character at line 1 column 3 of the JSON data

Please let me know how to convert below String to Array object

[[new Date('2016-03-27T13:05:03+00:00'), 1, 1, 1], [new Date('2016-03-27T13:06:02+00:00'), 0, 0, 1], [new Date('2016-03-27T13:07:02+00:00'), 0, 0, 1], [new Date('2016-03-27T13:08:02+00:00'), 0, 0, 1]] Thanks in advance,

  • Ramesh

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/google/google-visualization-issues/issues/2224

Daniel LaLiberte https://plus.google.com/100631381223468223275?prsrc=2 dlaliberte@Google.com dlaliberte@google.com 5CC, Cambridge MA

rcmeena commented 8 years ago

Hi Daniel, Thanks for your response.  My first requirement is to convert string array to array object and then google chart format comes in picture.  Is there a way to remove key names fromRuby on Rails to_json output? Fromvar data = ([["val1": 1, "val2": 2],["val1": 3, "val2":4]]);

Todata = ([[1,2], [3,4]]);For graph addrows. Thanks, -Ramesh

_____________________________

From: Daniel LaLiberte notifications@github.com Sent: Monday, March 28, 2016 7:06 AM Subject: Re: [google-visualization-issues] JSON.parse for string array to object conversion when date filed is there (#2224) To: google/google-visualization-issues google-visualization-issues@noreply.github.com Cc: rcmeena rcmeena@gmail.com

Hi Ramesh,  

You should use the string formatted dates, as described here:
https://developers.google.com/chart/interactive/docs/datesandtimes#dates-and-times-using-the-date-string-representation

On Mon, Mar 28, 2016 at 3:37 AM, rcmeena notifications@github.com wrote:

Hi All,
I have below output from Ruby Active record to a
@systemsLoadStatisticsChart1 variable.
output:
[['2016-03-27T13:05:03+00:00', 1, 1, 1], ['2016-03-27T13:06:02+00:00', 0,
0, 1], ['2016-03-27T13:07:02+00:00', 0, 0, 1],
['2016-03-27T13:08:02+00:00', 0, 0, 1]]

When I try to use this variable for Google Chart data, it comes in typeof
string
var data3 = ('%=@systemsLoadStatisticsChart1%');
try {
var data4 = JSON.parse(data3);

}
catch(err) {
document.getElementById("error").innerHTML = err.message;
}
I am getting below error during JSON.parse due to datetime value filed in
String array:
JSON.parse: unexpected character at line 1 column 3 of the JSON data

Please let me know how to convert below String to Array object

[[new Date('2016-03-27T13:05:03+00:00'), 1, 1, 1], [new
Date('2016-03-27T13:06:02+00:00'), 0, 0, 1], [new
Date('2016-03-27T13:07:02+00:00'), 0, 0, 1], [new
Date('2016-03-27T13:08:02+00:00'), 0, 0, 1]]
Thanks in advance,

  • Ramesh


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
https://github.com/google/google-visualization-issues/issues/2224

--
Daniel LaLiberte https://plus.google.com/100631381223468223275?prsrc=2
dlaliberte@Google.com dlaliberte@google.com 5CC, Cambridge MA

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub

dlaliberte commented 8 years ago

That sounds like a question for a Ruby or Rails expert, which we are not.

On Mon, Mar 28, 2016 at 10:37 AM, rcmeena notifications@github.com wrote:

Hi Daniel, Thanks for your response. My first requirement is to convert string array to array object and then google chart format comes in picture. Is there a way to remove key names fromRuby on Rails to_json output? Fromvar data = ([["val1": 1, "val2": 2],["val1": 3, "val2":4]]);

Todata = ([[1,2], [3,4]]);For graph addrows. Thanks, -Ramesh


From: Daniel LaLiberte notifications@github.com Sent: Monday, March 28, 2016 7:06 AM Subject: Re: [google-visualization-issues] JSON.parse for string array to object conversion when date filed is there (#2224) To: google/google-visualization-issues < google-visualization-issues@noreply.github.com> Cc: rcmeena rcmeena@gmail.com

Hi Ramesh,

You should use the string formatted dates, as described here:

https://developers.google.com/chart/interactive/docs/datesandtimes#dates-and-times-using-the-date-string-representation

On Mon, Mar 28, 2016 at 3:37 AM, rcmeena notifications@github.com wrote:

Hi All, I have below output from Ruby Active record to a @systemsLoadStatisticsChart1 variable. output: [['2016-03-27T13:05:03+00:00', 1, 1, 1], ['2016-03-27T13:06:02+00:00', 0, 0, 1], ['2016-03-27T13:07:02+00:00', 0, 0, 1], ['2016-03-27T13:08:02+00:00', 0, 0, 1]]

When I try to use this variable for Google Chart data, it comes in typeof string var data3 = ('%=@systemsLoadStatisticsChart1%'); try { var data4 = JSON.parse(data3);

} catch(err) { document.getElementById("error").innerHTML = err.message; } I am getting below error during JSON.parse due to datetime value filed in String array: JSON.parse: unexpected character at line 1 column 3 of the JSON data

Please let me know how to convert below String to Array object

[[new Date('2016-03-27T13:05:03+00:00'), 1, 1, 1], [new Date('2016-03-27T13:06:02+00:00'), 0, 0, 1], [new Date('2016-03-27T13:07:02+00:00'), 0, 0, 1], [new Date('2016-03-27T13:08:02+00:00'), 0, 0, 1]] Thanks in advance,

  • Ramesh

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/google/google-visualization-issues/issues/2224

Daniel LaLiberte https://plus.google.com/100631381223468223275?prsrc=2 dlaliberte@Google.com dlaliberte@google.com 5CC, Cambridge MA

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/google/google-visualization-issues/issues/2224#issuecomment-202420495

Daniel LaLiberte https://plus.google.com/100631381223468223275?prsrc=2 dlaliberte@Google.com dlaliberte@google.com 5CC, Cambridge MA

rcmeena commented 8 years ago

Ok, Let me make it more simple question:

My code work as below except date type field HTML Code

  <p id="error"></p>
  <p id="data"></p>

JAVASCRIPT Code - 1 (works fine with string to object conversion) google.charts.load('current', {packages: ['corechart', 'line']}); google.charts.setOnLoadCallback(drawBasic);

function drawBasic() { var data1 = '[[1, 1], [2, 2]]';

 alert (typeof (data1));
 try {
     data1 = JSON.parse(data1); 
 }
 catch(err) {
    document.getElementById("error").innerHTML = err.message;
}
alert (typeof (data1));

 document.getElementById("data").innerHTML = data1;
  var data = new google.visualization.DataTable();
  data.addColumn('number', 'X');
  data.addColumn('number', 'Dogs');

  data.addRows(data1);

  var options = {
    hAxis: {
      title: 'Time'
    },
    vAxis: {
      title: 'Popularity'
    }
  };

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

  chart.draw(data, options);
}

JAVASCRIPT Code - 2 (works fine with string to object conversion) google.charts.load('current', {packages: ['corechart', 'line']}); google.charts.setOnLoadCallback(drawBasic);

function drawBasic() { var data1 = '[["1", 1], ["2", 2]]';

 alert (typeof (data1));
 try {
     data1 = JSON.parse(data1); 
 }
 catch(err) {
    document.getElementById("error").innerHTML = err.message;
}
alert (typeof (data1));

 document.getElementById("data").innerHTML = data1;
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'X');
  data.addColumn('number', 'Dogs');

  data.addRows(data1);

  var options = {
    hAxis: {
      title: 'Time'
    },
    vAxis: {
      title: 'Popularity'
    }
  };

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

  chart.draw(data, options);
}

JAVASCRIPT Code - 3 (doesn't work with string to object conversion and I have tried many combinations)

google.charts.load('current', {packages: ['corechart', 'line']}); google.charts.setOnLoadCallback(drawBasic);

function drawBasic() { var data1 = '[["2016-03-27T13:05:03+00:00", 1], ["2016-03-28T13:05:03+00:00", 2]]';

 alert (typeof (data1));
 try {
     data1 = JSON.parse(data1); 
 }
 catch(err) {
    document.getElementById("error").innerHTML = err.message;
}
alert (typeof (data1));

 document.getElementById("data").innerHTML = data1;
  var data = new google.visualization.DataTable();
  data.addColumn('date', 'X');
  data.addColumn('number', 'Dogs');

  data.addRows(data1);

  var options = {
    hAxis: {
      title: 'Time'
    },
    vAxis: {
      title: 'Popularity'
    }
  };

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

  chart.draw(data, options);
}
dlaliberte commented 8 years ago

Your dates should be actual JS dates constructed with new Date(...) or if you need to use strings, use "Date(yyyy, MM, dd, ...)". See https://developers.google.com/chart/interactive/docs/datesandtimes#dates-and-times-using-the-date-string-representation

On Mon, Mar 28, 2016 at 3:06 PM, rcmeena notifications@github.com wrote:

Ok, Let me make it more simple question:

My code work as below except date type field HTML Code

JAVASCRIPT Code - 1 (works file with string to object conversion) google.charts.load('current', {packages: ['corechart', 'line']}); google.charts.setOnLoadCallback(drawBasic);

function drawBasic() { var data1 = '[[1, 1], [2, 2]]';

alert (typeof (data1)); try { data1 = JSON.parse(data1); } catch(err) { document.getElementById("error").innerHTML = err.message; } alert (typeof (data1));

document.getElementById("data").innerHTML = data1; var data = new google.visualization.DataTable(); data.addColumn('number', 'X'); data.addColumn('number', 'Dogs');

data.addRows(data1);

var options = { hAxis: { title: 'Time' }, vAxis: { title: 'Popularity' } };

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

chart.draw(data, options); }

JAVASCRIPT Code - 2 (works file with string to object conversion) google.charts.load('current', {packages: ['corechart', 'line']}); google.charts.setOnLoadCallback(drawBasic);

function drawBasic() { var data1 = '[["1", 1], ["2", 2]]';

alert (typeof (data1)); try { data1 = JSON.parse(data1); } catch(err) { document.getElementById("error").innerHTML = err.message; } alert (typeof (data1));

document.getElementById("data").innerHTML = data1; var data = new google.visualization.DataTable(); data.addColumn('string', 'X'); data.addColumn('number', 'Dogs');

data.addRows(data1);

var options = { hAxis: { title: 'Time' }, vAxis: { title: 'Popularity' } };

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

chart.draw(data, options); }

JAVASCRIPT Code - 3 (doesn't work with string to object conversion and I have tried many combinations)

google.charts.load('current', {packages: ['corechart', 'line']}); google.charts.setOnLoadCallback(drawBasic);

function drawBasic() { var data1 = '[["2016-03-27T13:05:03+00:00", 1], ["2016-03-28T13:05:03+00:00", 2]]';

alert (typeof (data1)); try { data1 = JSON.parse(data1); } catch(err) { document.getElementById("error").innerHTML = err.message; } alert (typeof (data1));

document.getElementById("data").innerHTML = data1; var data = new google.visualization.DataTable(); data.addColumn('date', 'X'); data.addColumn('number', 'Dogs');

data.addRows(data1);

var options = { hAxis: { title: 'Time' }, vAxis: { title: 'Popularity' } };

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

chart.draw(data, options); }

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/google/google-visualization-issues/issues/2224#issuecomment-202532882

Daniel LaLiberte https://plus.google.com/100631381223468223275?prsrc=2 dlaliberte@Google.com dlaliberte@google.com 5CC, Cambridge MA