0

maybe you could help me.

My jquery ajax call result is splitted with "||" and it creates an array of values. One of these values is a string like this:

[['2012-11-18', 33, 2], ['2012-11-19', 162, 11], ['2012-11-20', 140, 13]]

I need to make this as a viable javascript array to pass it to google chart drawchart(myarray) function to use it with data.addRows(myarray);

Can anyone help me to achieve it?

I can make this ajax returned array to look like whatever, what is important is that i could use this array to update google chart.

OKay, heres my ajax success code:

success: function(response)
     {
        response_d = response.split("||");
        response_message = response_d[0];

        if(response_message == 'ok') {
           $("#contr_count").html(response_d[1]); 
           $("#contr_average").html(contr_time(response_d[2])); 
           $("#contr_space_average").html(contr_time(response_d[3]));
           var chartdata = $.parseJSON(response_d[4]);
           drawChart(chartdata);
           $("#contr_list").html(response_d[5]); 
        }
        else {

        }
     }

And here is the drawchart function

function drawChart(chartdata) {
 var data = new google.visualization.DataTable();
  data.addColumn('string', 'Minutit tagasi');
  data.addColumn('number', 'T pikkus');
  data.addColumn('number', 'T vahe pikkus');

  data.addRows(chartdata);

data.addRows(dateArray);
 var options = {
   title:                 'T pikkuse ja vahe graafik (viimane 60 min)',
   backgroundColor:          '#fdf3e9', 
   colors:                ['#FF7F00','#437C17'],
   curveType:             'function',
   enableInteractivity:      true, 
   legend:                {position: 'bottom'},
   vAxis:                 {direction: -1},
   hAxis:                 {direction: -1},
   chartArea:             {width: '85%'}
 };

 var chart = new google.visualization.LineChart(document.getElementById('contr_graph'));
 chart.draw(data, options);

}

The code gives json error "SyntaxError: JSON.parse: unexpected character" ar firebug.

3
  • its an array of arrays. better make it an array of objects... Commented Jan 3, 2013 at 8:40
  • OKay, i added the code. Maybe you can help me now :) Commented Jan 3, 2013 at 8:47
  • Try replacing the single quotes with double quotes. Your array should be [["2012-11-18", 33, 2], ["2012-11-19", 162, 11], ["2012-11-20", 140, 13]] Commented Jan 3, 2013 at 8:52

1 Answer 1

0

You need to replace ' with " first. Try with:

var s = "[['2012-11-18', 33, 2], ['2012-11-19', 162, 11], ['2012-11-20', 140, 13]]";
var arr = $.parseJSON(s.replace(/'/g,'"'))

Here a jsfiddle with a working example. Open the javascript console to see the results.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.