我一直致力于在 Kibana 中创建树形图。就像这里一样:https://vega.github.io/vega/examples/tree-layout/
我就快到了,有时我可以获得树形图,但有时我会收到错误“[Error] ambigously: efg”。其他时候它会工作并产生一棵树。
Vega 代码在这里:
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"title": "Event counts",
"signals": [
{
"name": "labels",
"value": true,
"bind": {
"input": "checkbox"
}
},
{
"name": "layout",
"value": "tidy",
"bind": {
"input": "radio",
"options": [
"tidy",
"cluster"
]
}
},
{
"name": "links",
"value": "diagonal",
"bind": {
"input": "select",
"options": [
"line",
"curve",
"diagonal",
"orthogonal"
]
}
},
{
"name": "separation",
"value": false,
"bind": {
"input": "checkbox"
}
}
],
"data": [
{
"name": "parent",
"values": [
{
"currentNode": "rootID",
"parent": null
}
]
},
{
"name": "tree",
"format": {
"property": "aggregations.table.buckets"
},
"transform": [
{
"type": "formula",
"expr": "datum.key.stk1",
"as": "stk1"
},
{
"type": "formula",
"expr": "datum.key.stk2",
"as": "stk2"
},
{
"type": "formula",
"expr": "datum.key.stk3",
"as": "stk3"
},
{
"type": "formula",
"expr": "datum.doc_count",
"as": "size"
}
],
"values": {
"took": 16,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 473,
"max_score": null,
"hits": []
},
"aggregations": {
"table": {
"after_key": {
"stk1": "1",
"stk2": "abc",
"stk3": "2"
},
"buckets": [
{
"key": {
"stk1": "3",
"stk2": "cde",
"stk3": "4"
},
"doc_count": 1,
"stk1": "3",
"stk2": "cde",
"stk3": "4",
"size": 1
},
{
"key": {
"stk1": "313",
"stk2": "efg",
"stk3": "5"
},
"doc_count": 3,
"stk1": "313",
"stk2": "efg",
"stk3": "5",
"size": 3
},
{
"key": {
"stk1": "313",
"stk2": "efg",
"stk3": "6"
},
"doc_count": 2,
"stk1": "313",
"stk2": "efg",
"stk3": "6",
"size": 2
},
{
"key": {
"stk1": "32",
"stk2": "hij",
"stk3": "7"
},
"doc_count": 1,
"stk1": "32",
"stk2": "hij",
"stk3": "7",
"size": 1
},
{
"key": {
"stk1": "32",
"stk2": "abc",
"stk3": "2"
},
"doc_count": 1,
"stk1": "32",
"stk2": "abc",
"stk3": "2",
"size": 1
},
{
"key": {
"stk1": "328",
"stk2": "efg",
"stk3": "5"
},
"doc_count": 2,
"stk1": "328",
"stk2": "efg",
"stk3": "5",
"size": 2
},
{
"key": {
"stk1": "328",
"stk2": "klm",
"stk3": "8"
},
"doc_count": 1,
"stk1": "328",
"stk2": "klm",
"stk3": "8",
"size": 1
}
]
}
}
}
},
{
"name": "isolatedlevel1",
"source": "tree",
"transform": [
{
"type": "project",
"fields": [
"stk1",
"currentNode",
"doc_count"
],
"as": [
"currentNode",
"parent",
"doc_count"
]
},
{
"type": "aggregate",
"groupby": [
"parent",
"currentNode"
],
"fields": [
"doc_count"
],
"ops": [
"sum"
],
"as": [
"doc_count"
]
},
{
"type": "formula",
"as": "parent",
"expr": "!isDefined(datum.parent) ? 'rootID':datum.parent"
},
{
"type": "project",
"fields": [
"currentNode",
"parent",
"doc_count"
],
"as": [
"currentNode",
"parent",
"doc_count"
]
},
{
"type": "filter",
"expr": "datum.currentNode"
}
]
},
{
"name": "isolatedlevel1MergedWithParent",
"source": [
"isolatedlevel1",
"parent"
]
},
{
"name": "isolatedlevel2",
"source": "tree",
"transform": [
{
"type": "project",
"fields": [
"stk1",
"stk2",
"doc_count"
],
"as": [
"parent",
"currentNode",
"doc_count"
]
},
{
"type": "aggregate",
"groupby": [
"parent",
"currentNode"
],
"fields": [
"doc_count"
],
"ops": [
"sum"
],
"as": [
"doc_count"
]
},
{
"type": "project",
"fields": [
"currentNode",
"parent",
"doc_count"
],
"as": [
"currentNode",
"parent",
"doc_count"
]
},
{
"type": "filter",
"expr": "datum.currentNode"
},
{
"type": "filter",
"expr": "datum.parent"
}
]
},
{
"name": "isolatedlevel3",
"source": "tree",
"transform": [
{
"type": "project",
"fields": [
"stk1",
"stk2",
"stk3",
"doc_count"
],
"as": [
"grandparent",
"parent",
"currentNode",
"doc_count"
]
},
{
"type": "aggregate",
"groupby": [
"parent",
"currentNode"
],
"fields": [
"doc_count"
],
"ops": [
"sum"
],
"as": [
"doc_count"
]
},
{
"type": "project",
"fields": [
"currentNode",
"parent",
"doc_count"
],
"as": [
"currentNode",
"parent",
"doc_count"
]
},
{
"type": "filter",
"expr": "datum.currentNode"
},
{
"type": "filter",
"expr": "datum.parent"
}
]
},
{
"name": "mergedlevels",
"source": [
"isolatedlevel1MergedWithParent",
"isolatedlevel2",
"isolatedlevel3"
]
},
{
"name": "parents",
"source": [
"mergedlevels"
],
"transform": [
{
"type": "project",
"fields": [
"parent"
]
}
]
},
{
"name": "currentParentNode",
"source": [
"mergedlevels"
],
"transform": [
{
"type": "project",
"fields": [
"currentNode",
"doc_count"
]
},
{
"type": "filter",
"expr": "indata('parents', 'parent', datum.currentNode)"
}
]
},
{
"name": "currentLeaveNode",
"source": [
"mergedlevels"
],
"transform": [
{
"type": "project",
"fields": [
"currentNode",
"doc_count"
]
},
{
"type": "filter",
"expr": "!indata('parents', 'parent', datum.currentNode)"
}
]
},
{
"name": "mergedlevelsfinal",
"source": [
"isolatedlevel1MergedWithParent",
"isolatedlevel2",
"isolatedlevel3"
]
},
{
"name": "vistree",
"source": [
"mergedlevelsfinal"
],
"transform": [
{
"type": "stratify",
"key": "currentNode",
"parentKey": "parent"
},
{
"type": "tree",
"method": {
"signal": "layout"
},
"size": [
{
"signal": "height"
},
{
"signal": "width - 100"
}
],
"separation": {
"signal": "separation"
},
"as": [
"y",
"x",
"depth",
"children"
]
}
]
},
{
"name": "links",
"source": "vistree",
"transform": [
{
"type": "treelinks"
},
{
"type": "linkpath",
"orient": "horizontal",
"shape": {
"signal": "links"
}
}
]
}
],
"scales": [
{
"name": "color",
"type": "linear",
"range": {
"scheme": "magma"
},
"domain": {
"data": "vistree",
"field": "depth"
},
"zero": true
}
],
"marks": [
{
"type": "path",
"from": {
"data": "links"
},
"encode": {
"update": {
"path": {
"field": "path"
},
"stroke": {
"value": "#ccc"
}
}
}
},
{
"type": "symbol",
"from": {
"data": "vistree"
},
"encode": {
"enter": {
"size": {
"value": 100
},
"stroke": {
"value": "#fff"
}
},
"update": {
"x": {
"field": "x"
},
"y": {
"field": "y"
},
"fill": {
"scale": "color",
"field": "depth"
}
}
}
},
{
"type": "text",
"from": {
"data": "vistree"
},
"encode": {
"enter": {
"text": {
"field": "currentNode"
},
"fontSize": {
"value": 9
},
"baseline": {
"value": "middle"
}
},
"update": {
"x": {
"field": "x"
},
"y": {
"field": "y"
},
"dx": {
"signal": "datum.children ? -7 : 7"
},
"align": {
"signal": "datum.children ? 'right' : 'left'"
},
"opacity": {
"signal": "labels ? 1 : 0"
}
}
}
}
]
}
我很确定“分层”变换会引发错误,但我似乎无法弄清楚原因。任何帮助将不胜感激
我相信这个记录导致了问题:
这是因为efg混淆了去祖父母328或313。
{
"key": {
"stk1": "328",
"stk2": "efg",
"stk3": "5"
},
"doc_count": 2,
"stk1": "328",
"stk2": "efg",
"stk3": "5",
"size": 2
},