Vega-lite 数据转换为 un-nest objects
Vega-lite data transformation to un-nest objects
数据来自 elasticsearch url 并具有以下形式:
{
"took": 44,
"timed_out": false,
"hits": {
"total": 11,
"max_score": 0,
"hits": [
{
"_index": "dataindex",
"_type": "span",
"_id": "tKVUs3kBhoeKMUMeIwCv",
"_score": 0,
"_source": {
"fieldA": 272.2,
"fieldB": 73,
"fieldX": "event 1"
}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "iuVetHkBhoeKMUMe4O92",
"_score": 0,
"_source": {
"fieldA": 305.2,
"fieldB": 80,
"fieldX": "event 2"
}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "Yt-QwXkBhoeKMUMex3tp",
"_score": 0,
"_source": {
"fieldA": 281.8,
"fieldB": 73,
"fieldX": "event 3"
}
}
]
}
}
我想制作一个散点图矩阵。 hits.hits
下的数据数组可以通过format.property
配置访问。
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"repeat": {
"row": [
"_source.fieldA",
"_source.fieldB"
],
"column": [
"_source.fieldB",
"_source.fieldA"
]
},
"spec": {
"data": {
"url": "url/to/elastic/query",
"format": {"property": "hits.hits", "type": "json"}
},
"mark": "point",
"encoding": {
"x": {
"field": {"repeat": "column"},
"type": "quantitative"
},
"y": {
"field": {"repeat": "row"},
"type": "quantitative"
},
"color": {
"field": "_source.fieldX",
"type": "nominal"
},
"shape": {
"field": "_source.fieldX",
"type": "nominal"
}
}
}
}
但是还有一个额外的 _source
级别需要在任何地方指出(重复、颜色、形状),而且它在轴和图例标题中也是这样显示的:
是否有可以消除此 _source
级别的转换类型?因此,进入 encoding
阶段的数据可能就像源只是
[
{
"fieldA": 272.2,
"fieldB": 73,
"fieldX": "event 1"
},{
"fieldA": 305.2,
"fieldB": 80,
"fieldX": "event 2"
}
]
或者在重复矩阵中动态重命名轴的方法?
有一种方法,您必须提供一次字段,它会出现在单个级别而不是嵌套。按照下面或 editor:
中的方法执行 calculate
转换
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"transform": [
{"calculate": "datum._source.fieldA", "as": "fieldA"},
{"calculate": "datum._source.fieldB", "as": "fieldB"},
{"calculate": "datum._source.fieldX", "as": "fieldX"}
],
"repeat": {
"row": ["fieldA", "fieldB"],
"column": ["fieldB", "fieldA"]
},
"spec": {
"data": {
"values": {
"took": 44,
"timed_out": false,
"hits": {
"total": 11,
"max_score": 0,
"hits": [
{
"_index": "dataindex",
"_type": "span",
"_id": "tKVUs3kBhoeKMUMeIwCv",
"_score": 0,
"_source": {"fieldA": 272.2, "fieldB": 73, "fieldX": "event 1"}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "iuVetHkBhoeKMUMe4O92",
"_score": 0,
"_source": {"fieldA": 305.2, "fieldB": 80, "fieldX": "event 2"}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "Yt-QwXkBhoeKMUMex3tp",
"_score": 0,
"_source": {"fieldA": 281.8, "fieldB": 73, "fieldX": "event 3"}
}
]
}
},
"format": {"property": "hits.hits", "type": "json"}
},
"mark": "point",
"encoding": {
"x": {"field": {"repeat": "column"}, "type": "quantitative"},
"y": {"field": {"repeat": "row"}, "type": "quantitative"},
"color": {"field": "fieldX", "type": "nominal"},
"shape": {"field": "fieldX", "type": "nominal"}
}
}
}
数据来自 elasticsearch url 并具有以下形式:
{
"took": 44,
"timed_out": false,
"hits": {
"total": 11,
"max_score": 0,
"hits": [
{
"_index": "dataindex",
"_type": "span",
"_id": "tKVUs3kBhoeKMUMeIwCv",
"_score": 0,
"_source": {
"fieldA": 272.2,
"fieldB": 73,
"fieldX": "event 1"
}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "iuVetHkBhoeKMUMe4O92",
"_score": 0,
"_source": {
"fieldA": 305.2,
"fieldB": 80,
"fieldX": "event 2"
}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "Yt-QwXkBhoeKMUMex3tp",
"_score": 0,
"_source": {
"fieldA": 281.8,
"fieldB": 73,
"fieldX": "event 3"
}
}
]
}
}
我想制作一个散点图矩阵。 hits.hits
下的数据数组可以通过format.property
配置访问。
{
"$schema": "https://vega.github.io/schema/vega-lite/v4.json",
"repeat": {
"row": [
"_source.fieldA",
"_source.fieldB"
],
"column": [
"_source.fieldB",
"_source.fieldA"
]
},
"spec": {
"data": {
"url": "url/to/elastic/query",
"format": {"property": "hits.hits", "type": "json"}
},
"mark": "point",
"encoding": {
"x": {
"field": {"repeat": "column"},
"type": "quantitative"
},
"y": {
"field": {"repeat": "row"},
"type": "quantitative"
},
"color": {
"field": "_source.fieldX",
"type": "nominal"
},
"shape": {
"field": "_source.fieldX",
"type": "nominal"
}
}
}
}
但是还有一个额外的 _source
级别需要在任何地方指出(重复、颜色、形状),而且它在轴和图例标题中也是这样显示的:
是否有可以消除此 _source
级别的转换类型?因此,进入 encoding
阶段的数据可能就像源只是
[
{
"fieldA": 272.2,
"fieldB": 73,
"fieldX": "event 1"
},{
"fieldA": 305.2,
"fieldB": 80,
"fieldX": "event 2"
}
]
或者在重复矩阵中动态重命名轴的方法?
有一种方法,您必须提供一次字段,它会出现在单个级别而不是嵌套。按照下面或 editor:
中的方法执行calculate
转换
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"transform": [
{"calculate": "datum._source.fieldA", "as": "fieldA"},
{"calculate": "datum._source.fieldB", "as": "fieldB"},
{"calculate": "datum._source.fieldX", "as": "fieldX"}
],
"repeat": {
"row": ["fieldA", "fieldB"],
"column": ["fieldB", "fieldA"]
},
"spec": {
"data": {
"values": {
"took": 44,
"timed_out": false,
"hits": {
"total": 11,
"max_score": 0,
"hits": [
{
"_index": "dataindex",
"_type": "span",
"_id": "tKVUs3kBhoeKMUMeIwCv",
"_score": 0,
"_source": {"fieldA": 272.2, "fieldB": 73, "fieldX": "event 1"}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "iuVetHkBhoeKMUMe4O92",
"_score": 0,
"_source": {"fieldA": 305.2, "fieldB": 80, "fieldX": "event 2"}
},
{
"_index": "dataindex",
"_type": "span",
"_id": "Yt-QwXkBhoeKMUMex3tp",
"_score": 0,
"_source": {"fieldA": 281.8, "fieldB": 73, "fieldX": "event 3"}
}
]
}
},
"format": {"property": "hits.hits", "type": "json"}
},
"mark": "point",
"encoding": {
"x": {"field": {"repeat": "column"}, "type": "quantitative"},
"y": {"field": {"repeat": "row"}, "type": "quantitative"},
"color": {"field": "fieldX", "type": "nominal"},
"shape": {"field": "fieldX", "type": "nominal"}
}
}
}