可能是因为您在绘制树状图时未正确配置数据源和字段。您可以尝试使用以下代码示例来解决此问题:
from superset.utils import core as utils
from sqlalchemy import func
session = utils.get_session()
query = (
session.query(
Table.parent_id,
Table.child_id,
func.sum(Table.field_to_show).label("field_to_show"),
)
.group_by(Table.parent_id, Table.child_id)
.all()
)
tree = {
"name": "root",
"children": [],
}
for row in query:
parent_id, child_id, field_to_show = row
parent_node = None
child_node = None
for node in tree["children"]:
if node["id"] == parent_id:
parent_node = node
if node["id"] == child_id:
child_node = node
if parent_node is None:
parent_node = {
"id": parent_id,
"name": "Parent node name",
"children": []
}
tree["children"].append(parent_node)
if child_node is None:
child_node = {
"id": child_id,
"name": "Child node name",
"value": 0
}
parent_node["children"].append(child_node)
child_node["value"] += field_to_show
此代码示例适用于使用SQLAlchemy ORM从数据库中获取数据的情况。您需要将代码中的Table
替换为您的数据库表名称,将parent_id
和child_id
替换为相应的字段名称。此外,您还需要将"Parent node name"
和"Child node name"
替换为您的实际节点名称。