高图未使用返回的Ajax数据对象进行更新

时间:2013-03-28 11:20:49

标签: ajax highcharts

我最近开始使用Highchards这真的令人印象深刻,但我在使用jQuery Ajax请求返回的数据更新图表时遇到了问题。

我的代码如下:

$.ajax({
type: "POST",
url: "scripts/ajax_testDetails.php",
async: true,
cache: false,
dataType: 'json',
timeout: 30000,
data: "TestID=" + TestID,
success: function(data) {

if(data.Status == "Success"){

    document.title = data.FirstName + " " + data.LastName;  // This works fine
    $('input[name=TestDate]').val(data.TestDateFormat); // This works fine too

    console.log("Accuracy2: " + data.Accuracy2);        // This works fine
    console.log("TPR2: " + data.TPR2);          // This works fine too

    var Accur1 = 6;                 // Used to debug, works fine in chart.series[0].setData()
    var NewStuff = new Array(25,15,8,18,5);     // Used to debug, works fine in chart.series[3].setData()
    var Accuracy3 = data.Accuracy2          // Used to debug, returns 'undefined' when used in chart.series[0].setData()

    var chart = $('#HeartGraph').highcharts();

    // Anything that use the data object here shows as 'undefined' in yData and doesn't change the graph
    chart.series[0].setData([randomBetween(1,10),Accur1,data.Accuracy2,Accuracy3,randomBetween(1,10)],false);
    chart.series[3].setData([NewStuff[0],NewStuff[1],randomBetween(1,10),data.TPR2,data.TPR3],false);

    chart.redraw();                 // When redraws, values change except for the ones that use the data. object!

}

});

正如您所看到的,我使用成功数据对象并确认一切正常,但是当我尝试使用chart.series [] .setData()函数中的数据时,它不起作用。测试值似乎工作正常。非常奇怪而且非常混乱!

非常感谢任何建议。

1 个答案:

答案 0 :(得分:1)

一种可能性是你的json数据包含字符串而不是数字。这可能会导致数据系列出现问题。尝试在服务器或javascript中转换为数字:

chart.series[0].setData([
   randomBetween(1,10),
   Accur1 * 1,
   data.Accuracy2 * 1,
   Accuracy3 * 1,
   randomBetween(1,10)],false);

'* 1'将字符串值转换为数字值。

相关问题