如何在 vega 中访问嵌套的 json
How to access nested json in vega
我在 kibana 中使用 vega。我尝试访问嵌套的 json 字段,但我无法访问该字段,谁能帮我解决这个问题。我附上我的 vega json 和索引名称是 pie
索引数据样本:
{
"_index": "pie",
"_type": "flare",
"_id": "2",
“_版本”:1,
“_分数”:1,
“_资源”: {
"id": 2,
"parent": 1,
"data" :{
"memory": 50
}
}
}
{
"$schema": "https://vega.github.io/schema/vega/v3.json",
"data": [
{
"name": "table",
"url": {
"%context%": "true",
"index": "pie*",
"body": {
"size": "10000"
}
},
"format": { "property": "hits.hits" },
"transform": [
{
"type": "formula",
"expr": "toNumber(datum[\"_source\"][\"data\"][\"memory\"])",
"as": "memory"
}
],
"transform": [{"type": "pie", "field": "memory"}]
} ],
"scales": [
{
"name": "r",
"type": "sqrt",
"domain": {"data": "table", "field": "memory"},
"zero": true,
"range": [20, 100]
}
],
"marks": [
{
"type": "arc",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"startAngle": {"field": "startAngle"},
"endAngle": {"field": "endAngle"},
"innerRadius": {"value": 20},
"outerRadius": {"scale": "r", "field": "memory"},
"stroke": {"value": "#fff"}
},
"update": {
"fill": {"value": "#ccc"}
},
"hover": {
"fill": {"value": "skyblue"}
}
}
},
{
"type": "text",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"radius": {"scale": "r", "field": "memory", "offset": 8},
"theta": {"signal": "(datum.startAngle + datum.endAngle)/2"},
"fill": {"value": "#000"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"text": {"field": "memory"}
}
}
}
]
}
此问题已在 https://github.com/vega/vega/issues/1371
中得到解答
您在数据变量中定义了一个 "format": { "property": "hits.hits" }
,我在您的数据中看不到它。
即使是,在我看来 property
不接受双重嵌套参数(例如 varx.vary
),但我可能错了。
接下来您在单个 data
规范中有两个 transform
定义。
改变这个,给我:
{
"$schema": "https://vega.github.io/schema/vega/v4.json",
"data": [
{
"name": "table",
"values": [
{
"_index": "pie",
"_type": "flare",
"_id": "2",
"_version": 1,
"_score": 1,
"_source": {"id": 2, "parent": 1, "data": {"memory": 50}}
}
],
"transform": [
{
"type": "formula",
"expr": "toNumber(datum[\"_source\"][\"data\"][\"memory\"])",
"as": "memory"
},
{"type": "pie", "field": "memory"}
]
}
],
"scales": [
{
"name": "r",
"type": "sqrt",
"domain": {"data": "table", "field": "memory"},
"zero": true,
"range": [20, 100]
}
],
"marks": [
{
"type": "arc",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"startAngle": {"field": "startAngle"},
"endAngle": {"field": "endAngle"},
"innerRadius": {"value": 20},
"outerRadius": {"scale": "r", "field": "memory"},
"stroke": {"value": "#fff"}
},
"update": {"fill": {"value": "#ccc"}},
"hover": {"fill": {"value": "skyblue"}}
}
},
{
"type": "text",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"radius": {"scale": "r", "field": "memory", "offset": 8},
"theta": {"signal": "(datum.startAngle + datum.endAngle)/2"},
"fill": {"value": "#000"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"text": {"field": "memory"}
}
}
}
]
}
我在 kibana 中使用 vega。我尝试访问嵌套的 json 字段,但我无法访问该字段,谁能帮我解决这个问题。我附上我的 vega json 和索引名称是 pie
索引数据样本:
{ "_index": "pie", "_type": "flare", "_id": "2", “_版本”:1, “_分数”:1, “_资源”: { "id": 2, "parent": 1, "data" :{ "memory": 50 } } }
{
"$schema": "https://vega.github.io/schema/vega/v3.json",
"data": [
{
"name": "table",
"url": {
"%context%": "true",
"index": "pie*",
"body": {
"size": "10000"
}
},
"format": { "property": "hits.hits" },
"transform": [
{
"type": "formula",
"expr": "toNumber(datum[\"_source\"][\"data\"][\"memory\"])",
"as": "memory"
}
],
"transform": [{"type": "pie", "field": "memory"}]
} ],
"scales": [
{
"name": "r",
"type": "sqrt",
"domain": {"data": "table", "field": "memory"},
"zero": true,
"range": [20, 100]
}
],
"marks": [
{
"type": "arc",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"startAngle": {"field": "startAngle"},
"endAngle": {"field": "endAngle"},
"innerRadius": {"value": 20},
"outerRadius": {"scale": "r", "field": "memory"},
"stroke": {"value": "#fff"}
},
"update": {
"fill": {"value": "#ccc"}
},
"hover": {
"fill": {"value": "skyblue"}
}
}
},
{
"type": "text",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"radius": {"scale": "r", "field": "memory", "offset": 8},
"theta": {"signal": "(datum.startAngle + datum.endAngle)/2"},
"fill": {"value": "#000"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"text": {"field": "memory"}
}
}
}
]
}
此问题已在 https://github.com/vega/vega/issues/1371
中得到解答您在数据变量中定义了一个 "format": { "property": "hits.hits" }
,我在您的数据中看不到它。
即使是,在我看来 property
不接受双重嵌套参数(例如 varx.vary
),但我可能错了。
接下来您在单个 data
规范中有两个 transform
定义。
改变这个,给我:
{
"$schema": "https://vega.github.io/schema/vega/v4.json",
"data": [
{
"name": "table",
"values": [
{
"_index": "pie",
"_type": "flare",
"_id": "2",
"_version": 1,
"_score": 1,
"_source": {"id": 2, "parent": 1, "data": {"memory": 50}}
}
],
"transform": [
{
"type": "formula",
"expr": "toNumber(datum[\"_source\"][\"data\"][\"memory\"])",
"as": "memory"
},
{"type": "pie", "field": "memory"}
]
}
],
"scales": [
{
"name": "r",
"type": "sqrt",
"domain": {"data": "table", "field": "memory"},
"zero": true,
"range": [20, 100]
}
],
"marks": [
{
"type": "arc",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"startAngle": {"field": "startAngle"},
"endAngle": {"field": "endAngle"},
"innerRadius": {"value": 20},
"outerRadius": {"scale": "r", "field": "memory"},
"stroke": {"value": "#fff"}
},
"update": {"fill": {"value": "#ccc"}},
"hover": {"fill": {"value": "skyblue"}}
}
},
{
"type": "text",
"from": {"data": "table"},
"encode": {
"enter": {
"x": {"field": {"group": "width"}, "mult": 0.5},
"y": {"field": {"group": "height"}, "mult": 0.5},
"radius": {"scale": "r", "field": "memory", "offset": 8},
"theta": {"signal": "(datum.startAngle + datum.endAngle)/2"},
"fill": {"value": "#000"},
"align": {"value": "center"},
"baseline": {"value": "middle"},
"text": {"field": "memory"}
}
}
}
]
}