gpt4 book ai didi

javascript - 如何将我的数据转换成适合 d3.js sunburst 的格式?

转载 作者:行者123 更新时间:2023-11-30 13:49:12 29 4
gpt4 key购买 nike

我有这样的数据:https://jsfiddle.net/L79wjn0x/ ,我想将其转换为这种格式 https://jsfiddle.net/49n1jmL5/ .我正在尝试使用 d3.nest 方法来更改格式但无法使其工作。在我的数据中, child 通过 parent 的 ID 与 parent 联系起来。

我的样本数据如下。 child 与 parent 联系在一起通过 parent 的 id 即 child 的 parent_id 键与 parent 的身份证 key 。

 [{
"id": 1,
"name": "ab",
"parent_id": null,
"value": 120,
"tree_level": 1
},
{
"id": 2,
"name": "ac",
"parent_id": null,
"value": 110,
"tree_level": 1
} {
"id": 3,
"name": "abc",
"parent_id": 1,
"value": 30,
"tree_level": 2
},
{
"id": 4,
"name": "abcd",
"parent_id": 1,
"value": 90,
"tree_level": 2
},
{
"id": 5,
"name": "abc",
"parent_id": 3,
"value": 10,
"tree_level": 3
},
{
"id": 6,
"name": "abc",
"parent_id": 3,
"value": 20,
"tree_level": 3
}
]

我希望将上面的 json 格式转换为如下格式。谁能带我走正确的路

{
"id": 1,
"name": "ab",
"parent_id": null,
"value": 120,
"tree_level": 1,
children: [{
"id": 3,
"name": "abc",
"parent_id": 1,
"value": 30,
"tree_level": 2
children: [{
"id": 5,
"name": "abc",
"parent_id": 3,
"value": 10,
"tree_level": 3
},
{
"id": 6,
"name": "abc",
"parent_id": 3,
"value": 20,
"tree_level": 3
}
]
},
{
"id": 4,
"name": "abcd",
"parent_id": 1,
"value": 90,
"tree_level": 2,
children: []
}
]
}, {
"id": 2,
"name": "ac",
"parent_id": null,
"value": 110,
"tree_level": 1,
children: []
}

下面是我为转换而编写的代码,但它对我不起作用。它不是在内部创建 child ,而是在外部创建它。

var nested = d3.nest()
.key(function(d) {
if (d.parent_id === null)
return d;
}).rollup(function(ele) {
return ele.map(function(c) {
return {
"id": c.id,
"parent_id": c.parent_id,
"name": c.name,
"logValue": c.logValue,
"tree_level": c.tree_level
};
});
}).entries(this.dataset);
console.log(nested)

d3.nest() 会工作还是我需要其他东西?

最佳答案

为了将该数据传递给 d3.partition(),您将使用它来创建旭日形图,您只需要 d3.stratify .在你的情况下:

const stratify = d3.stratify()
.parentId(function(d) {
return d.parent_id;
});

const hierarchicalData = stratify(data);

这是演示(使用浏览器的控制台查看对象,而不是 S.O. 代码段):

const data = [{
"id": 131567,
"name": "cellular organisms",
"tree_level": 1,
"display_order": 2,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": null
},
{
"id": 2759,
"name": "Eukaryota",
"tree_level": 2,
"display_order": 5,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 131567
},
{
"id": 33154,
"name": "Opisthokonta",
"tree_level": 3,
"display_order": 49,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 2759
}, {
"id": 33208,
"name": "Metazoa",
"tree_level": 4,
"display_order": 126,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 33154
}, {
"id": 6072,
"name": "Eumetazoa",
"tree_level": 5,
"display_order": 227,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 33208
}, {
"id": 33213,
"name": "Bilateria",
"tree_level": 6,
"display_order": 547,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 6072
}, {
"id": 33511,
"name": "Deuterostomia",
"tree_level": 7,
"display_order": 950,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 33213
}, {
"id": 7711,
"name": "Chordata",
"tree_level": 8,
"display_order": 1639,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 33511
}, {
"id": 89593,
"name": "Craniata",
"tree_level": 9,
"display_order": 3042,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 7711
}, {
"id": 7742,
"name": "Vertebrata",
"tree_level": 10,
"display_order": 3614,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 89593
}, {
"id": 7776,
"name": "Gnathostomata",
"tree_level": 11,
"display_order": 4245,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 7742
}, {
"id": 117570,
"name": "Teleostomi",
"tree_level": 12,
"display_order": 4684,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 7776
}, {
"id": 117571,
"name": "Euteleostomi",
"tree_level": 13,
"display_order": 4994,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 117570
}, {
"id": 8287,
"name": "Sarcopterygii",
"tree_level": 14,
"display_order": 5175,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 117571
}, {
"id": 1338369,
"name": "Dipnotetrapodomorpha",
"tree_level": 15,
"display_order": 5787,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 8287
}, {
"id": 32523,
"name": "Tetrapoda",
"tree_level": 16,
"display_order": 5945,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 1338369
}, {
"id": 32524,
"name": "Amniota",
"tree_level": 17,
"display_order": 6322,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 32523
}, {
"id": 40674,
"name": "Mammalia",
"tree_level": 18,
"display_order": 6556,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 32524
}, {
"id": 32525,
"name": "Theria",
"tree_level": 19,
"display_order": 6705,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 40674
}, {
"id": 9347,
"name": "Eutheria",
"tree_level": 20,
"display_order": 6882,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 32525
}, {
"id": 1437010,
"name": "Boreoeutheria",
"tree_level": 21,
"display_order": 7254,
"has_data": "N",
"is_leaf": "N",
"value": 18314,
"parent_id": 9347
}, {
"id": 314145,
"name": "Laurasiatheria",
"tree_level": 22,
"display_order": 7469,
"has_data": "N",
"is_leaf": "N",
"value": 48,
"parent_id": 1437010
}, {
"id": 314146,
"name": "Euarchontoglires",
"tree_level": 22,
"display_order": 7470,
"has_data": "N",
"is_leaf": "N",
"value": 18281,
"parent_id": 1437010
}, {
"id": 9443,
"name": "Primates",
"tree_level": 23,
"display_order": 7575,
"has_data": "N",
"is_leaf": "N",
"value": 17686,
"parent_id": 314146
}, {
"id": 33554,
"name": "Carnivora",
"tree_level": 23,
"display_order": 7598,
"has_data": "N",
"is_leaf": "N",
"value": 11,
"parent_id": 314145
}, {
"id": 91561,
"name": "Cetartiodactyla",
"tree_level": 23,
"display_order": 7629,
"has_data": "N",
"is_leaf": "N",
"value": 37,
"parent_id": 314145
}, {
"id": 314147,
"name": "Glires",
"tree_level": 23,
"display_order": 7675,
"has_data": "N",
"is_leaf": "N",
"value": 741,
"parent_id": 314146
}, {
"id": 9845,
"name": "Ruminantia",
"tree_level": 24,
"display_order": 7760,
"has_data": "N",
"is_leaf": "N",
"value": 16,
"parent_id": 91561
}, {
"id": 9989,
"name": "Rodentia",
"tree_level": 24,
"display_order": 7762,
"has_data": "N",
"is_leaf": "N",
"value": 741,
"parent_id": 314147
}, {
"id": 35497,
"name": "Suina",
"tree_level": 24,
"display_order": 7776,
"has_data": "N",
"is_leaf": "N",
"value": 21,
"parent_id": 91561
}, {
"id": 376913,
"name": "Haplorrhini",
"tree_level": 24,
"display_order": 7857,
"has_data": "N",
"is_leaf": "N",
"value": 17686,
"parent_id": 9443
}, {
"id": 379584,
"name": "Caniformia",
"tree_level": 24,
"display_order": 7859,
"has_data": "N",
"is_leaf": "N",
"value": 11,
"parent_id": 33554
}, {
"id": 9655,
"name": "Mustelidae",
"tree_level": 25,
"display_order": 7931,
"has_data": "N",
"is_leaf": "N",
"value": 11,
"parent_id": 379584
}, {
"id": 9821,
"name": "Suidae",
"tree_level": 25,
"display_order": 7938,
"has_data": "N",
"is_leaf": "N",
"value": 21,
"parent_id": 35497
}, {
"id": 35500,
"name": "Pecora",
"tree_level": 25,
"display_order": 7956,
"has_data": "N",
"is_leaf": "N",
"value": 16,
"parent_id": 9845
}, {
"id": 314293,
"name": "Simiiformes",
"tree_level": 25,
"display_order": 8029,
"has_data": "N",
"is_leaf": "N",
"value": 17686,
"parent_id": 376913
}, {
"id": 1963758,
"name": "Myomorpha",
"tree_level": 25,
"display_order": 8054,
"has_data": "N",
"is_leaf": "N",
"value": 741,
"parent_id": 9989
}, {
"id": 9479,
"name": "Platyrrhini",
"tree_level": 26,
"display_order": 8101,
"has_data": "N",
"is_leaf": "N",
"value": 251,
"parent_id": 314293
}, {
"id": 9526,
"name": "Catarrhini",
"tree_level": 26,
"display_order": 8102,
"has_data": "N",
"is_leaf": "N",
"value": 17435,
"parent_id": 314293
}, {
"id": 9822,
"name": "Sus",
"tree_level": 26,
"display_order": 8108,
"has_data": "N",
"is_leaf": "N",
"value": 21,
"parent_id": 9821
}, {
"id": 9895,
"name": "Bovidae",
"tree_level": 26,
"display_order": 8112,
"has_data": "N",
"is_leaf": "N",
"value": 16,
"parent_id": 35500
}, {
"id": 169418,
"name": "Mustelinae",
"tree_level": 26,
"display_order": 8190,
"has_data": "N",
"is_leaf": "N",
"value": 11,
"parent_id": 9655
}, {
"id": 337687,
"name": "Muroidea",
"tree_level": 26,
"display_order": 8202,
"has_data": "N",
"is_leaf": "N",
"value": 741,
"parent_id": 1963758
}, {
"id": 9665,
"name": "Mustela",
"tree_level": 27,
"display_order": 8256,
"has_data": "N",
"is_leaf": "N",
"value": 11,
"parent_id": 169418
}, {
"id": 9823,
"name": "Sus scrofa",
"tree_level": 27,
"display_order": 8261,
"has_data": "N",
"is_leaf": "N",
"value": 21,
"parent_id": 9822
}, {
"id": 10066,
"name": "Muridae",
"tree_level": 27,
"display_order": 8267,
"has_data": "N",
"is_leaf": "N",
"value": 741,
"parent_id": 337687
}, {
"id": 27592,
"name": "Bovinae",
"tree_level": 27,
"display_order": 8277,
"has_data": "N",
"is_leaf": "N",
"value": 16,
"parent_id": 9895
}, {
"id": 314295,
"name": "Hominoidea",
"tree_level": 27,
"display_order": 8339,
"has_data": "N",
"is_leaf": "N",
"value": 17435,
"parent_id": 9526
}, {
"id": 9604,
"name": "Hominidae",
"tree_level": 28,
"display_order": 8386,
"has_data": "N",
"is_leaf": "N",
"value": 17435,
"parent_id": 314295
}, {
"id": 9668,
"name": "Mustela putorius",
"tree_level": 28,
"display_order": 8389,
"has_data": "N",
"is_leaf": "N",
"value": 11,
"parent_id": 9665
}, {
"id": 9903,
"name": "Bos",
"tree_level": 28,
"display_order": 8395,
"has_data": "N",
"is_leaf": "N",
"value": 16,
"parent_id": 27592
}, {
"id": 39107,
"name": "Murinae",
"tree_level": 28,
"display_order": 8420,
"has_data": "N",
"is_leaf": "N",
"value": 741,
"parent_id": 10066
}, {
"id": 9669,
"name": "Mustela putorius furo",
"tree_level": 29,
"display_order": 8545,
"has_data": "N",
"is_leaf": "N",
"value": 11,
"parent_id": 9668
}, {
"id": 9913,
"name": "Bos taurus",
"tree_level": 29,
"display_order": 8547,
"has_data": "N",
"is_leaf": "N",
"value": 16,
"parent_id": 9903
}, {
"id": 10088,
"name": "Mus",
"tree_level": 29,
"display_order": 8556,
"has_data": "N",
"is_leaf": "N",
"value": 3,
"parent_id": 39107
}, {
"id": 10114,
"name": "Rattus",
"tree_level": 29,
"display_order": 8557,
"has_data": "N",
"is_leaf": "N",
"value": 740,
"parent_id": 39107
}, {
"id": 207598,
"name": "Homininae",
"tree_level": 29,
"display_order": 8606,
"has_data": "N",
"is_leaf": "N",
"value": 17435,
"parent_id": 9604
}, {
"id": 9605,
"name": "Homo",
"tree_level": 30,
"display_order": 8684,
"has_data": "N",
"is_leaf": "N",
"value": 17435,
"parent_id": 207598
}, {
"id": 9606,
"name": "Homo sapiens",
"tree_level": 31,
"display_order": 8808,
"has_data": "N",
"is_leaf": "N",
"value": 17435,
"parent_id": 9605
}
];

const stratify = d3.stratify()
.parentId(function(d) {
return d.parent_id;
});

const hierarchicalData = stratify(data);

console.log(hierarchicalData)
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>

关于javascript - 如何将我的数据转换成适合 d3.js sunburst 的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58623010/

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