gpt4 book ai didi

javascript - 使用深度嵌套的 json 在 dc.js 中动态创建数据表

转载 作者:行者123 更新时间:2023-11-30 10:01:43 24 4
gpt4 key购买 nike

我想使用 dc.js 创建一个数据表。我的javascript代码如下:

dataTable.width(800).height(800)
.dimension(collegeDimension)
.group(function(d) { return "List of all Selected Students"
})
.size(100)
.columns([
function(d) { return d.student_name; },
function(d) { return d.student_grade; }
])
.sortBy(function(d){ return d.order-details.order-id; })
// (optional) sort order, :default ascending
.order(d3.ascending);

我有一个 json 文件,看起来像这样:

[{  
"college_name":"abc",
"college_students":[
{
"student_name":"xyz1",
"student_grade":"A"
},
{
"student_name":"xyz2",
"student_grade":"B"
}
]},{
"college_name":"abc1",
"college_students":[
{
"student_name":"xyz3",
"student_grade":"A"
},
{
"student_name":"xyz4",
"student_grade":"B"
}
]},{
"college_name":"abc2",
"college_students":[
{
"student_name":"xyz5",
"student_grade":"A"
},
{
"student_name":"xyz6",
"student_grade":"B"
}
]}]

我有一个要求,我不能更改 json 的结构。

是否可以使用dataTable解析数据并创建具有以下结构的数据表?

enter image description here

最佳答案

如果你将数据展平,你使用交叉过滤器的生活会容易得多,但你可以在预处理步骤中很容易地做到这一点,而无需更改源数据:

var dd = /* your data */;
var all_students = [];
dd.forEach(function(college) {
college.college_students.forEach(function(student) {
all_students.push({college_name: college.college_name, student_name: student.student_name, student_grade: student.student_grade});
});
});

现在使用 all_students 作为您的交叉过滤器,您将不必为复杂的访问器等而烦恼。

关于javascript - 使用深度嵌套的 json 在 dc.js 中动态创建数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31280312/

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