-2

Sory my bad english.
I'm trying make a grafic with Morris.js, i'm using the line Chart and wanna make it with dynamic data, but when I will convert my array to a JsonString, it's broke my aplication, I already try some sollutions on internet like this and others, but not sucess

when I convert the Array to Json the console show :

VM13133:1 Uncaught SyntaxError: Unexpected end of JSONinput(…)
      executeMorrisGrafics @ 
      dashboard_1Mensal.controller.js:44initController @ 
      dashboard_1Mensal.controller.js:26(anonymous function) @ 
      dashboard_1Mensal.controller.js:16

my code is this

[...]
function executeMorrisGrafics(startDate, endDate){

  RequestService.getReturnMalingResult(startDate, endDate).then(function(data){

        var array = [];
        for(var i=0;i<data.listLineChartEmailSms.length;i++){
          array.push({ 
            y: data.listLineChartEmailSms[i.toString()].y, 
            a: data.listLineChartEmailSms[i.toString()].a, 
            b: data.listLineChartEmailSms[i.toString()].b 
          });
        }
        dataJson = array; //dataJson is a global Var
  });
  console.log(dataJson);//working until here
  var result = JSON.parse(dataJson.toString());//here is the problem

  Morris.Line({
    element: 'grafic-LineChart1',
    data: result,
    xkey: 'y',
    ykeys: ['a', 'b'],
    labels: ['Series A', 'Series B']
  });
[...]

Thank you

Community
  • 1
  • 1
Italo José
  • 1,558
  • 1
  • 17
  • 50
  • 3
    `JSON.parse(dataJson.toString())` wtf. Why. – Kevin B Nov 03 '16 at 21:22
  • 1
    If you've already got an object, you don't need to parse it. Just use `dataJson` directly. – Pointy Nov 03 '16 at 21:23
  • `dataJson.toString()` DOES NOT WORK! Why would you do it this way? You obviously know about the `JSON` object, you should be aware of `JSON.stringify()` – VLAZ Nov 03 '16 at 21:23
  • Also, yeah, if you've got the object, _why_ parse it? – VLAZ Nov 03 '16 at 21:23
  • 1
    Also, possible duplicate-ish? http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323?s=1|0.0000#14220323 `RequestService.getReturnMalingResult` appears to be asyncronous, and you're trying to get data from it to the outside, that doesn't work. – Kevin B Nov 03 '16 at 21:23
  • 2
    Your `getReturnMalingResult` seems to be asynchronous, and you can't use the data outside the callback – adeneo Nov 03 '16 at 21:27

2 Answers2

1

From the Morris.js page about line charts, the data parameter is described as

The data to plot. This is an array of objects, containing x and y attributes as described by the xkey and ykeys options.

So in your code you do not need to use the dataJson.toString() function. Your code should be as below.

[...]
function executeMorrisGrafics(startDate, endDate){

  RequestService.getReturnMalingResult(startDate, endDate).then(function(data){

        var array = [];
        for(var i=0;i<data.listLineChartEmailSms.length;i++){
          array.push({ 
            y: data.listLineChartEmailSms[i.toString()].y, 
            a: data.listLineChartEmailSms[i.toString()].a, 
            b: data.listLineChartEmailSms[i.toString()].b 
          });
        }
        dataJson = array; //dataJson is a global Var
  });
  console.log(dataJson);//working until here
  //var result = JSON.parse(dataJson.toString());//here is the problem

  Morris.Line({
    element: 'grafic-LineChart1',
    data: dataJson,
    xkey: 'y',
    ykeys: ['a', 'b'],
    labels: ['Series A', 'Series B']
  });
[...]

A longer explanation is that JSON is a string format for objects that are intended to be used in JavaScript. But your code is not doing anything that needs JSON. See http://www.w3schools.com/js/js_json_intro.asp for a JSON primer.

Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67
0

In simple terms, JSON is a text representation of an object; objects are data structures.

There is no need to JSON.parse() something that is already an object in your program.

[…]
var result = dataJson;

Morris.Line({
    element: 'grafic-LineChart1',
    data: result,
    xkey: 'y',
    ykeys: ['a', 'b'],
    labels: ['Series A', 'Series B']
});

Also, like @adeneo mentions, it appears that dataJson is set by a Promise callback asynchronously, but your code using dataJson is synchronous.

tmslnz
  • 1,801
  • 2
  • 15
  • 24