如何传递JSON数据以更新Highcharts饼图

时间:2022-12-03 17:19:45

I am trying to make an AJAX call to my server and pass in a year value. The server then fetches from a database information about different countries' economies of that year. I am then using that data to update the pie chart from Highcharts.

我正在尝试对我的服务器进行AJAX调用并传入一年的值。然后,服务器从数据库中提取有关该年度不同国家经济的信息。然后我使用该数据从Highcharts更新饼图。

So my AJAX call is like so

所以我的AJAX调用是这样的

$('#one').change(function() {
var val = $("#one option:selected").text();
var id = 'one';
$.ajax({
    type: "POST",
    url: "/sss",
    data: {val, id},
    dataType: 'JSON',
    success: function () { 
        console.log('success');
    },
    error: function (ts) {
        alert(ts.responseText);
        alert(val);
    }
  })
  .done(function (results){
    results = JSON.stringify(results);
    var chart = $('#char1').highcharts();
    chart.series[0].setData(results);
    /* chart.series[0].setData([ { name: 'USA', y: 19386200 },
    { name: 'China', y: 12059889 },
    { name: 'Japan', y: 4867528 },
    { name: 'Germany', y: 3753700 }]); */
    alert(results);
  })
});

Then my route for when I POST to /sss is

然后,当我发布到/ sss时,我的路线是

router.post('/sss', function(req, res, next){
   console.log("sss");
   var yr = req.body.val;
   console.log(yr);

   var funnn = function(year, callback){
       uploadController.getGDPfunc(year, callback);
   }

   funnn(Number(yr), function(err,results){
      if(err) console.log("err");
      else{
          //console.log(results);
          res.send(results);
      }
   });
});

So I am calling my controller function from the route and it is returning the results in the correct format. Even in AJAX, I can log the results and it looks in the correct format. If I was to copy those outputed results and paste them into chart.series[0].setData(HERE), then it would work. But only if I leave the chart.series[0].setData(results) does it not work correctly.

所以我从路由调用我的控制器函数,它以正确的格式返回结果。即使在AJAX中,我也可以记录结果,并且它看起来格式正确。如果我要复制那些输出的结果并将它们粘贴到chart.series [0] .setData(HERE)中,那么它会起作用。但是,只有当我离开chart.series [0] .setData(结果)时,它才能正常工作。

Any ideas why this can be? If I hardcode the data in the AJAX "done" part, then it updates properly so I do not believe that the chart rendering is the problem. I feel like it is the format of the data, but like I said it works if I was to hardcode it into the setData...So I am lost. Any ideas?

任何想法为什么会这样?如果我在AJAX“完成”部分硬编码数据,那么它会正确更新,所以我不相信图表渲染是问题所在。我觉得这是数据的格式,但就像我说的那样,如果我将它硬编码到setData中就行了......所以我迷失了。有任何想法吗?

Thanks

谢谢

1 个解决方案

#1


2  

You start with an object parsed from JSON, you shouldn't stringify it into a string again like you do here:

您从一个从JSON解析的对象开始,您不应该像在此处一样将其再次串行化为字符串:

results = JSON.stringify(results);

results = JSON.stringify(results);

Assuming that results gives you the sort of object you would expect, just remove that line so that you call setData with the proper object (array), rather than a string.

假设结果为您提供了您期望的对象类型,只需删除该行,以便使用正确的对象(数组)而不是字符串调用setData。

#1


2  

You start with an object parsed from JSON, you shouldn't stringify it into a string again like you do here:

您从一个从JSON解析的对象开始,您不应该像在此处一样将其再次串行化为字符串:

results = JSON.stringify(results);

results = JSON.stringify(results);

Assuming that results gives you the sort of object you would expect, just remove that line so that you call setData with the proper object (array), rather than a string.

假设结果为您提供了您期望的对象类型,只需删除该行,以便使用正确的对象(数组)而不是字符串调用setData。