【问题标题】:Ajax JSON response into HighChartsAjax JSON 响应到 HighCharts
【发布时间】:2014-07-25 15:27:29
【问题描述】:

我正在尝试将动态数据添加到直接来自 AJAX 请求的 HighCharts 图表中。

这是我的 AJAX 调用的 JSON 响应:

{
    "Data1":[17342,2424742,96496,13516,3262472,472722,278452],
    "Data2":[1213263,139300,21361,3724247,2474274,248835,135155],
    "Data3":[4727,427485,357,5383,483853,247472,247277]
}

这也是我将在其中传递 JSON 响应的函数:

function createGraph(response) {

        $('#container').highcharts({
                credits: {
                    enabled: false
                },
                chart: {
                    type: 'line'
                },
                title: {
                    text: 'Profit Graph'
                },
                subtitle: {
                    text: 'Data from the past week'
                },
                xAxis: {
                    type: 'datetime',
                    dateTimeLabelFormats: {
                        day: '%b %e',
                            week: '%b %e'
                    }
                },
                yAxis: {
                    title: {
                            text: 'Price'
                    },
                    min: 0
                },
                tooltip: {
                    formatter: function() {
                            return '<span style="color:#33333;">'+this.series.name +': '+ Highcharts.numberFormat(this.y,0);
                    }
                },
                series: [{
                    name: 'Data1',
                    data: /*show Data1 from AJAX Response*/,
                    pointStart: Date.UTC(2014, d.getUTCMonth(), weeks_ago(new Date())),
                    pointInterval: 24 * 3600 * 1000,
                    color: '#55CCA2',
                }, {
                    name: 'Data2',
                    data: /*show Data2 from AJAX Response*/,
                    pointStart: Date.UTC(2014, d.getUTCMonth(), weeks_ago(new Date())),
                    pointInterval: 24 * 3600 * 1000,
                    color: '#3498db',
                }, {
                    name: 'Data3',
                    data: /*show Data3 from AJAX Response*/,
                    pointStart: Date.UTC(2014, d.getUTCMonth(), weeks_ago(new Date())),
                    pointInterval: 24 * 3600 * 1000,
                    color: '#0B79C3',
                }]
        });

};

$.get(window.AjaxURL, {'action': 'graphData'}, function(response) {
        createGraph(response);
});

我怎样才能将这些数据解析成指定的系列?

【问题讨论】:

  • 对于Data1系列,数据将为Variable_Storing_Your_Ajax_Result.Data1...对于Data2系列,数据将为Variable_Storing_Your_Ajax_Result.Data2...对于Data3系列,数据将为Variable_Storing_Your_Ajax_Result.Data3...... . 你可能首先需要 jQuery.parseJSON(response);

标签: javascript jquery ajax json highcharts


【解决方案1】:

创建 json 对象:

var jsonObj=JSON.parse(response);

现在您可以使用 jsobjObj.Data1 、 jsonObj.Data2 和 jsonObj.Data3 获取 Data1、Data2、Data3 数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多