将json变量传递给Amchart dataProvider

时间:2017-10-11 09:13:43

标签: jquery ajax amcharts

我试着在春天做一个小项目。所以我使用ajax调用并成功获取json字符串。在成功获得ajax请求后,我想将变量传递给 dataProvider 。获得ajax响应后,当我在控制台中打印时,它的工作正常。它显示[{"year" : 2017,"average" : 0.0}]。但无法传递给dataProvider这是我的代码。

$.ajax({
    //other stuffs
    success:function(data){         
        console.log(data)   // out put : [{"year" : 2017,"average" : 0.0}]

        chart = AmCharts.makeChart("yearAverage", {
            "type": "serial",
            "theme": "light",
            "marginRight": 60,
            "autoMarginOffset": 20,
            "marginTop": 7,
            "dataDateFormat": "YYYY",
            "color":"#9b1148",

            "dataProvider": data, // data is a string json variable                 
            //otherstuffs


    }
})

当我提到文档时,它说,我必须使用var chart = new AmCharts.AmSerialChart();chart.dataProvider = chartData;。但我无法理解如何将其添加到我的代码中。

控制台错误为Cannot read property 'toString' of undefined并显示 dataProvider

我这么久都在尝试。请帮助我,提前谢谢。

修改 控制台错误

Uncaught TypeError: Cannot read property 'toString' of undefined
    at b.parsePartSerialData (amcharts.js:37)
    at b.parseSerialData (amcharts.js:37)
    at b.parseData (serial.js:7)
    at b.initChart (serial.js:2)
    at b.afterWrite (amcharts.js:18)
    at b.write (amcharts.js:18)
    at Object.d.makeChart (amcharts.js:1)
    at Object.success (homeDashboard:1718)
    at i (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)

1 个答案:

答案 0 :(得分:0)

我排除了这个,因为数据是json字符串,我必须将其转换为数组格式,所以我使用了JSON.parse(data)