Such a problem arose:

From the server via ajax I get data, in the js cycle I form json in order to substitute the function that builds the graphics. Faced a striking thing: if you display the contents of a variable, for example through document.write on the screen, copy and substitute, everything works, and if you directly substitute, then it writes

Uncaught TypeError: Cannot call method 'match' of undefined

alert produces quite a valid code ...

JSON of this type:

[{"period":"2014-5-5","ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹":"1000","ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ":"500"},{"period":"2014-5-5","ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹":"1000","ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ":"500"}..... ΠΈ Ρ‚.Π΄. ] 

Formation in a cycle:

 to_chart += "{"+"\"period\":"+"\""+chart_sub[0]+"-"+chart_sub[1]+"-"+chart_sub[2]+"\""+","+"\"ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹\":"+"\""+chart_sub[3]+"\""+","+"\"ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ\":"+"\""+chart_sub[4]+"\""+"}"+","; 

Substitute the function:

 var tc = "["+to_chart+"]"; Morris.Line({ element: 'staff_chart_container', **data: tc,** xkey: 'period', ykeys: ['ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹', 'ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ'], labels: ['ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹', 'ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ'], smooth: true, lineColors: ["#336699","#009245"], hideHover:true }); 

Tell me the decision, friends)

  • one
    The code in the studio - ikhmelinina
  • one
    For debugging, it is more convenient to view console.log (data) - naym 5:34 pm

1 answer 1

@RomaRio, you understand that the tc variable contains a string with a JSON expression, and Morris.Line as the value of the data parameter expects an array of JS objects.

that is, roughly speaking, instead

 to_chart += "{"+"\"period\":"+"\""+chart_sub[0]+"-"+chart_sub[1]+"-"+chart_sub[2]+"\""+","+"\"ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹\":"+"\""+chart_sub[3]+"\""+","+"\"ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ\":"+"\""+chart_sub[4]+"\""+"}"+","; 

you need something like:

 // Π² Ρ†ΠΈΠΊΠ»Π΅ заполняСм to_chart[i] = {period: chart_sub[0] + "-" + chart_sub[1] + "-" + chart_sub[2], clients: chart_sub[3], sales: chart_sub[4]}; ..... Morris.Line({ element: 'staff_chart_container', data: to_chart, xkey: 'period', ykeys: ['ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹', 'ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ'], labels: ['ΠšΠ»ΠΈΠ΅Π½Ρ‚Ρ‹', 'ΠŸΡ€ΠΎΠ΄Π°ΠΆΠΈ'], smooth: true, lineColors: ["#336699","#009245"], 

hideHover: true});

Well, or the second option to parse the JSON glued by you:

 var tc = JSON.parse("[" + to_chart + "]"); 
  • Thank you very much, it started, zaurchalo, earned !!! - RomaRio