英文:
Can I get a simpler explanation of how array hierarchy of items in a treemap using plotly.js works?
问题
我想使用plotly.js的treemap功能,阅读了文档后仍然不明白如何自定义项目层次。
在我的想象中,我将代码表达的树形层次结构如下:
{
parent: { "topItem" },
topItem: { "one", "two", "three" }, /* top-item的子项 */
one: { "something" }, /* "one"的子项 */
two: { "thing", "whatever" } /* "two"的一些子项 */
}
我知道有更好的方法,但我的观点是这种风格映射到我对数据的嵌套关系的思考方式。
在plotly.js treemap示例代码中,数据和渲染输出之间的关系让我困扰,我不明白如何思考它。
示例使用了一些数组来表达嵌套项之间的关系,通常是这样的:
data = [{
type: "treemap",
labels: ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents: ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ]
}]
Plotly.newPlot('myDiv', data)
这个示例渲染了此处显示的层次结构:链接
如果我想向"Cain"块添加三个子项(或者进行任何其他更改),我不知道该如何做,我想学习。
谢谢。
英文:
I want to use the treemap feature of plotly.js and after reading the documentation I still don't understand how to customize item hierarchies.
In my mind , I imagine a tree hierarchy expressed in code as something like this:
{
parent:{ "topItem"},
topItem: {"one","two","three"}, /* children of top-item */
one:{"something"}, /* child of "one" */
two: {"thing", "whatever"} /* a couple of children of "two" */
}
I know there are better ways to do this, but my point is that this style maps to how I think about nested relationships of data.
In the plotly.js treemap example code, the relationship between the data and the rendered output confuses me and I don't understand how to think about it.
The example(s) use a couple of arrays to express nested item relationships and they typically look like this:
data = [{
type: "treemap",
labels: ["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
parents: ["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ]
}]
Plotly.newPlot('myDiv', data)
This example renders the hierarchy displayed here : https://plotly.com/javascript/treemaps/
If I wanted to add three children to the "Cain" block (or any other change for that matter), I have no idea how to do that and I would like to learn.
Thank you.
答案1
得分: 2
I'm going to take a JSON hierarchy like yours as an example:
{
"root": { "topItem" },
"topItem": { "one", "two", "three" },
"one": { "something" },
"two": { "thing", "whatever" }
}
First, you take all of the elements you want to render and put them in the labels array, just listing them one after the other:
labels: ["root", "topItem", "one", "two", "three", "something", "thing", "whatever"]
Then, for each of the elements in labels
, you have to specify its parent element in the hierarchy. labels[0]
is called root
and it does not have a parent, so we leave parents[0] = ""
. labels[1]
is topItem
, and according to the JSON, its parent is root
, so parents[1] = "root"
. labels[2]
, labels[3]
, and labels[4]
("one", "two", "three") all have the same parent, so parents[2] = parents[3] = parents[4] = "topItem"
, and so on.
You end up with:
parents: ["", "root", "topItem", "topItem", "topItem", "one", "two", "two"]
See the example below:
const data = [{
type: "treemap",
labels: ["root", "topItem", "one", "two", "three", "something", "thing", "whatever"],
parents: ["", "root", "topItem", "topItem", "topItem", "one", "two", "two"]
}]
Plotly.newPlot('chart', data)
<script src="https://cdn.plot.ly/plotly-2.20.0.min.js"></script>
<div id='chart'></div>
英文:
I'm going to take a JSON hierarchy like yours as an example:
{
"root": { "topItem" },
"topItem": { "one", "two", "three" },
"one": { "something" },
"two": { "thing", "whatever" }
}
First you take all of the elements you want to render and put them in the labels array, just listing them one after the other:
labels: ["root", "topItem", "one", "two", "three", "something", "thing", "whatever"]
Then for each of the elements in labels
, you have to specify its parent element in the hierarchy. labels[0]
is called root
and it does not have a parent, so we leave parents[0] = ""
. labels[1]
is topItem
and according to the JSON, its parent is root
, so parents[1] = "root"
. labels[2]
, labels[3]
and labels[4]
("one", "two", "three") all have the same parent, so parents[2] = parents[3] = parents [4] = "topItem"
, and so on.
You end up with
parents: ["", "root", "topItem", "topItem", "topItem", "one", "two", "two" ]
See the example below:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const data = [{
type: "treemap",
labels: ["root", "topItem", "one", "two", "three", "something", "thing", "whatever"],
parents: ["", "root", "topItem", "topItem", "topItem", "one", "two", "two" ]
}]
Plotly.newPlot('chart', data)
<!-- language: lang-html -->
<script src="https://cdn.plot.ly/plotly-2.20.0.min.js"></script>
<div id='chart'></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论