Highcharts:如何使用setData与ajax获得的字符串

Highcharts : How to use setData with a string obtained by ajax?

本文关键字:字符串 ajax 何使用 setData Highcharts      更新时间:2023-09-26

我想使用ajax更新我的图表,但setData方法需要一个数组,我只有一个字符串,所以它不起作用。

这是我的代码

$(".chooseService a").click(function() {
                            $("span.currentService").html($(this).html());
                            $.get('http://localhost:8080/dashboard/ws/charge/repartition/jour/'+$(this).html(), 
                                function(data) {
                                    // setData (Array<Mixed> data, [Boolean redraw])
                                    chartDay.series[0].setData(data);
                            });
                        });

data是格式化字符串,如

 [[1356995280000,183.0],[1356995520000,573.0],[1356995760000,243.0]]

有人知道吗?

您可以将JSON字符串解析为变量data。

data = JSON.parse(data);

如果您在使用JSON方法时遇到问题:http://caniuse.com/json

当您从服务器端返回结果时,您可以将内容格式化为JSON类型

header ('Content-type: text / json');
header ('Content-type: application / json');

然后你可以从客户端转换这个结果来正确地评估它。

jQuery.parseJSON (this.responseString);

如果你想要一个这样的数组可以使用这个引用