gpt4 book ai didi

javascript - 如何在向下钻取 Highcharts 中填充动态 json 值

转载 作者:行者123 更新时间:2023-11-29 18:03:59 24 4
gpt4 key购买 nike

我试图在向下钻取 Highcharts 中动态填充 json 下方,但数据未按预期填充。

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

我用静态数据创建了细化 Highcharts ,请在下面找到以下链接:

这张图是我的预期结果:

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>

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

最佳答案

在 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);

});

关于javascript - 如何在向下钻取 Highcharts 中填充动态 json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com