1

When I add:

    var data = google.visualization.arrayToDataTable
        ([
['Hora', 'Mínima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Média',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Máxima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}],['14:50', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',0.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','NE (0.8)', 4.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','4.0'],['15:00', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',2.6,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (2.6)', 6.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','6.7'],['15:10', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',5.5,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (5.5)', 10.2,'point { size: 3; shape-type: circle; fill-color: #FFCC00','10.2'],['15:20', 2.2,'point { size: 3; shape-type: circle; fill-color: #33CC00','2.2',73,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.3)', 11.8,'point { size: 3; shape-type: circle; fill-color: #FFCC00','11.8'],['15:30', 2.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','2.8',7.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.7)', 11.8,'point { size: 3; shape-type: circle; fill-color: #FFCC00','11.8'],['15:40', 3.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','3.8',7.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.0)', 10.2,'point { size: 3; shape-type: circle; fill-color: #FFCC00','10.2'],['15:50', 5.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','5.7',8.4,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (8.4)', 13.0,'point { size: 3; shape-type: circle; fill-color: #FFCC00','13.0']
        ]);

it works fine, but when i set this value into a variable doesn't work.

Like:

var vvalor_inserir = "['Hora', 'Mínima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Média',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Máxima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}],['14:50', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',0.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','NE (0.8)', 4.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','4.0'],['15:00', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',2.6,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (2.6)', 6.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','6.7'],['15:10', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',5.5,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (5.5)', 10.2,'point { size: 3; shape-type: circle; fill-color: #FFCC00','10.2'],['15:20', 2.2,'point { size: 3; shape-type: circle; fill-color: #33CC00','2.2',7   3,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.3)', 11.8,'point { size: 3; shape-type: circle; fill-color: #FFCC00','11.8'],['15:30', 2.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','2.8',7.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.7)', 11.8,'point { size: 3; shape-type: circle; fill-color: #FFCC00','11.8'],['15:40', 3.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','3.8',7.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.0)', 10.2,'point { size: 3; shape-type: circle; fill-color: #FFCC00','10.2'],['15:50', 5.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','5.7',8.4,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (8.4)', 13.0,'point { size: 3; shape-type: circle; fill-color: #FFCC00','13.0']";

    var data = google.visualization.arrayToDataTable
    ([
    vvalor_inserir
    ]);

Any idea?

1 Answers1

0

That's because you're saving the value as a string, when in fact you need to save it as an array. The method google.visualization.arrayToDataTable() receives an array of values, which is denoted by encapsulating said values in square brackets. When you wrap these values in quotes, it no longer treats them as a series of values, and instead just provides the string of characters.

Additionally,the outside pair of square brackets cannot be left in the method call, and must be kept as a part of the variable (to make it an array, not just a handful of comma-separated objects).

This should work:

var vvalor_inserir = [['Hora', 'Mínima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Média',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Máxima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}],['14:50', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',0.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','NE (0.8)', 4.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','4.0'],['15:00', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',2.6,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (2.6)', 6.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','6.7'],['15:10', 0.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','0.0',5.5,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (5.5)', 10.2,'point { size: 3; shape-type: circle; fill-color: #FFCC00','10.2'],['15:20', 2.2,'point { size: 3; shape-type: circle; fill-color: #33CC00','2.2',73,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.3)', 11.8,'point { size: 3; shape-type: circle; fill-color: #FFCC00','11.8'],['15:30', 2.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','2.8',7.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.7)', 11.8,'point { size: 3; shape-type: circle; fill-color: #FFCC00','11.8'],['15:40', 3.8,'point { size: 3; shape-type: circle; fill-color: #33CC00','3.8',7.0,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (7.0)', 10.2,'point { size: 3; shape-type: circle; fill-color: #FFCC00','10.2'],['15:50', 5.7,'point { size: 3; shape-type: circle; fill-color: #33CC00','5.7',8.4,'point { size: 3; shape-type: circle; fill-color: #33CC00','SE (8.4)', 13.0,'point { size: 3; shape-type: circle; fill-color: #FFCC00','13.0']];

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

Side Note:

On an unrelated note, it's not normally conventional to break a method call across multiple lines like this:

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

You normally see this done if you're passing a large data structure explicitly (as was the case in your initial example), but stylistically it's probably better practice to simply write it like this:

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

That is, however, only a nitpick, and irrelevant to the functionality of your code.

ConnorCMcKee
  • 1,625
  • 1
  • 11
  • 23
  • Hi... just did as you said but still errors... I think because multiple "[" and "]" at the same array on variable..? Maybe I'm passing to vvalor_inserir variable multiples "[" and "]" comma delimited? – Rodrigo Azevedo Dec 01 '15 at 19:35
  • @Rodrigo - Ah, my apologies, I didn't look closely enough at the variable itself. The second square brackets weren't redundant, and there was some slightly-incorrect syntax in the object itself. I have edited my answer above. Could you try again, and share your results? I have confirmed in my testing that the variable `vvalor_inserir` at least is now in a valid format. – ConnorCMcKee Dec 01 '15 at 19:44
  • Hi.. Absolutelly I will.... One last question.. how to convert to this array format from a Sring variable? Like... var string = "[['Hora', 'Mínima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Média',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}, 'Máxima',{type: 'string', role: 'style'},{type: 'string', role: 'annotation'}]"...... Need to convert a String to this array variable – Rodrigo Azevedo Dec 01 '15 at 22:00
  • There are quite a lot of questions about this sort of thing (such as http://stackoverflow.com/questions/3473639/best-way-to-convert-string-to-array-of-object-in-javascript), but in your case you're working with a fairly large set of data, and it's not properly formatted to be parsed via JSON. I think the best way to address this would be to open a question explaining the form in which you receive this information and the necessary output form, since now your problem is "how do I turn a string into an array of arrays and objects." – ConnorCMcKee Dec 01 '15 at 22:07