Can I get a simpler explanation of how array hierarchy of items in a treemap using plotly.js works?

huangapple go评论68阅读模式
英文:

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:

{
	&quot;root&quot;: { &quot;topItem&quot; },
	&quot;topItem&quot;: { &quot;one&quot;, &quot;two&quot;, &quot;three&quot; },
   	&quot;one&quot;: { &quot;something&quot; },
	&quot;two&quot;: { &quot;thing&quot;, &quot;whatever&quot; }
}

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: [&quot;root&quot;, &quot;topItem&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;something&quot;, &quot;thing&quot;, &quot;whatever&quot;]

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] = &quot;&quot;. labels[1] is topItem and according to the JSON, its parent is root, so parents[1] = &quot;root&quot;. labels[2], labels[3] and labels[4] ("one", "two", "three") all have the same parent, so parents[2] = parents[3] = parents [4] = &quot;topItem&quot;, and so on.
You end up with

parents: [&quot;&quot;, &quot;root&quot;, &quot;topItem&quot;, &quot;topItem&quot;, &quot;topItem&quot;, &quot;one&quot;, &quot;two&quot;, &quot;two&quot; ]

See the example below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const data = [{
      type: &quot;treemap&quot;,
      labels: [&quot;root&quot;, &quot;topItem&quot;, &quot;one&quot;, &quot;two&quot;, &quot;three&quot;, &quot;something&quot;, &quot;thing&quot;, &quot;whatever&quot;],
      parents: [&quot;&quot;, &quot;root&quot;, &quot;topItem&quot;, &quot;topItem&quot;, &quot;topItem&quot;, &quot;one&quot;, &quot;two&quot;, &quot;two&quot; ]
}]

Plotly.newPlot(&#39;chart&#39;, data)

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.plot.ly/plotly-2.20.0.min.js&quot;&gt;&lt;/script&gt;

&lt;div id=&#39;chart&#39;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 02:14:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定