我想建立一个互动网络: -
http://bl.ocks.org/mbostock/1062288
Class, Subclass, Company, Product,
Chocolate, Wafer chocolate, Nestle, KitKat,
Chocolate, White chocolate, Nestle, Milkybar,
Chocolate, White chocolate, Nestle, Milkybar,
Chocolate, Caramel chocolate, Nestle, BarOne,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Nestle, Nestle Milk chocolate,
Chocolate, Milk chocolate, Cadbury, Dairy milk,
父节点将是“巧克力”,其他子节点将是: -
巧克力 - >子类 - >公司--->的产品。 (每个都应该是点击 - 展开 - 折叠)
每个节点的大小应取决于孩子的数量。
此外,如果您单击一个节点,它应该展开/折叠以显示其子节点。 有什么方法可以在python中做到这一点。我看了d3,但它太复杂了,无法理解。是否有更简单的pythonic方式在网络浏览器中以交互方式执行此操作?
答案 0 :(得分:2)
您可以使用d3.js来执行此操作。 check有一个很好的例子。如果您不想使用图表,也可以使用collapse Tree。
使用教程中的脚本,您的示例应该像this一样工作。如果您不打算使用collaspe / expand结构。您可以使用d3.js构建图形,它看起来像this。
root = {
"name": "Chocolate", "tag":"class",
"children": [
{
"name": "Wafer", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "KitKat", "tag":"product"}
]
}
]
},
{
"name": "White", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "Milkybar", "tag":"product"}
]
}
]
},
{
"name": "Caramel", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "BarOne", "tag":"product"}
]
}
]
},
{
"name": "Milk", "tag":"subclass",
"children": [
{
"name": "Nestle", "tag":"company",
"children": [
{"name": "Nestle Milk", "tag":"product"}
]
}, {
"name": "Cadbury", "tag":"company",
"children": [
{"name": "Dairy Milk", "tag":"product"}
]
}
]
}
]
};
答案 1 :(得分:1)
您可以使用mpld3,这是一个工具包,可通过d3在浏览器中显示matplotlib图形。它的互动性和支持您的要求。 http://mpld3.github.io/