如何在向下钻取高级图中填充动态json值

时间:2015-09-24 06:26:05

标签: javascript jquery json highcharts drilldown

我正在尝试在向下钻取高级图表时动态填充json,但数据未按预期填充。

我想获取所有用户并将其放入图表中,并在每个用户的深入分析中显示成功,错误,取消,访问,注册,卸载

我使用静态数据创建了rill down highchart,请在下面找到以下链接:

此图表是我的预期结果:

http://jsfiddle.net/jqueryb/ndfk7vgd/2/

我的JSON:

var result=    [
      {
        "userId": "user1",
        "success": 1,
        "error": 0,
        "cancel": 0,
        "visit": 3,
        "enroll": 2,
        "uninstall": 0
      },
      {
        "userId": "user2",
        "success": 2,
        "error": 0,
        "cancel": 0,
        "visit": 4,
        "enroll": 2,
        "uninstall": 0
      },
      {
        "userId": "user3",
        "success": 1,
        "error": 0,
        "cancel": 0,
        "visit": 2,
        "enroll": 1,
        "uninstall": 0
      },
      {
        "userId": "user4",
        "success": 0,
        "error": 0,
        "cancel": 2,
        "visit": 4,
        "enroll": 2,
        "uninstall": 0
      }
    ]

我尝试使用以下代码在向下钻取时动态填充值,但没有运气:(

<script type="text/javascript">
    function loadChart(result) {
        var user_data = [];
        $.each(JSON.parse(JSON.stringify(result)), function(idx, obj) {
            user_data.push(['{ name:' + obj.userId + ',y:1}']);
        });
        var test_data = '[' + user_data + ']';
        $('#container').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: 'Basic drilldown'
            },
            xAxis: {
                type: 'category'
            },
            legend: {
                enabled: false
            },
            plotOptions: {
                series: {
                    borderWidth: 0,
                    dataLabels: {
                        enabled: true
                    }
                }
            },
            series: [{
                name: 'Things',
                colorByPoint: true,
                data: test_data
            }]
        });

    }
</script>

如果有人能帮我解决这个问题,我将不胜感激。 提前谢谢。

1 个答案:

答案 0 :(得分:4)

从php while循环开始,当var结果来自查询结果时,如何回显下面的var结果部分以在图表中使用?

var result = [
{
    "userId": "user1",
    "success": 1,
    "error": 0,
    "cancel": 0,
    "visit": 3,
    "enroll": 2,
    "uninstall": 0
},
{
    "userId": "user2",
    "success": 2,
    "error": 0,
    "cancel": 0,
    "visit": 4,
    "enroll": 2,
    "uninstall": 0
},

etc...

];

为highcharts正确构建数据:

当数据是json形式时,你可以使用js基本对象来构建它。

在此处查看:http://jsfiddle.net/x934L81p/1/

像这样构建你的系列数据:

    var user = {};
    user.name = obj.userId;
    user.y = 1;
    user.drilldown = obj.userId;
    user_data.push(user);

然后在同一循环中构建您的钻取:

    var drilldown_user = {};
    drilldown_user.id = obj.userId;

    drilldown_user.data = [];        
    drilldown_user.data.push(['success', obj.success]);
    drilldown_user.data.push(['error', obj.error]);
    drilldown_user.data.push(['cancel', obj.cancel]);
    drilldown_user.data.push(['visit', obj.visit]);
    drilldown_user.data.push(['enroll', obj.enroll]);
    drilldown_user.data.push(['uninstall', obj.uninstall]);

    drill_down_data.push(drilldown_user);

全部放在一起:

var user_data = [];
var drill_down_data = [];

$.each(JSON.parse(JSON.stringify(result)), function(idx, obj) {
    var user = {};
    user.name = obj.userId;
    user.y = 1;
    user.drilldown = obj.userId;

    user_data.push(user);

    var drilldown_user = {};
    drilldown_user.id = obj.userId;

    drilldown_user.data = [];
    drilldown_user.data.push(['success', obj.success]);
    drilldown_user.data.push(['error', obj.error]);
    drilldown_user.data.push(['cancel', obj.cancel]);
    drilldown_user.data.push(['visit', obj.visit]);
    drilldown_user.data.push(['enroll', obj.enroll]);
    drilldown_user.data.push(['uninstall', obj.uninstall]);

    drill_down_data.push(drilldown_user);

});