英文:
Filter and sum in Vega lite
问题
我正在尝试使用Vega-Lite(在Airtable中)绘制折线图,该图根据日期绘制字段的累积和。我想要过滤掉日期为空的数据点。
我需要将“valid”操作参数和“sum”操作参数结合起来,或者添加一个过滤器。我不能只在Airtable内部过滤,因为我有3条不同的线要叠加。
英文:
I am trying to plot a line graph using Vega-Lite (in Airtable) which plots cumulative sum of a field by date.
I want to filter out data points with empty dates.
I somehow need to combine the "valid" OP argument and the "sum" OP argument - OR add a filter. I can't just filter within airtable as a I have 3 different lines to overlay
` "transform": [
{
"sort": [{"field": "Latest plan date"}],
"window": [{
"op": "sum",
"field": "Impact to baseline",
"as": "planned_cumul"}
]
}
],
"layer": [
{
"mark": {
"type": "line",
"stroke": "maroon"
},
"encoding":
{
"x": {
"field": "Latest plan date",
"type": "temporal",
"title": ""
},
"y": {
"field": "planned_cumul",
"type": "quantitative"
}
}
}
],
``'
</details>
# 答案1
**得分**: 1
以下是您要翻译的内容:
您可以使用以下方法过滤掉缺失的日期:
```json
"transform": [
{"filter": "isValid(datum.date)"}
// 其他操作...
]
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "两条线:一条使用窗口转换和总计进行累积,过滤掉空日期。",
"data": {
"values": [
{"date": "2023-01-01", "total": 10},
{"date": "2023-01-02", "total": 20},
{"date": "", "total": 30},
{"date": "2023-01-04", "total": 40},
{"date": "2023-01-05", "total": 50},
{"date": "", "total": 60},
{"date": "2023-01-07", "total": 50}
]
},
"transform": [
{"filter": "isValid(datum.date)"},
{
"window": [{"op": "sum", "field": "total", "as": "cumulative_total"}],
"sort": [{"field": "date", "order": "ascending"}],
"frame": [null, 0]
}
],
"layer": [
{
"mark": "circle",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {
"field": "cumulative_total",
"type": "quantitative",
"title": "累积总数"
},
"tooltip": {"field": "cumulative_total", "type": "quantitative"},
"color": {"value": "blue"}
}
},
{
"mark": "line",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {
"field": "cumulative_total",
"type": "quantitative",
"title": "累积总数"
},
"tooltip": {"field": "cumulative_total", "type": "quantitative"},
"color": {"value": "blue"}
}
},
{
"mark": "circle",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {"field": "total", "type": "quantitative", "title": "总数"},
"tooltip": {"field": "total", "type": "quantitative"},
"color": {"value": "red"}
}
},
{
"mark": "line",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {"field": "total", "type": "quantitative", "title": "总数"},
"tooltip": {"field": "total", "type": "quantitative"},
"color": {"value": "red"}
}
}
],
"config": {}
}
亚当
英文:
You can filter out missing dates with a transform:
"transform": [
{"filter": "isValid(datum.date)"}
.....
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"description": "Two lines: one cumulated using window transform and totals, with blank dates filtered out.",
"data": {
"values": [
{"date": "2023-01-01", "total": 10},
{"date": "2023-01-02", "total": 20},
{"date": "", "total": 30},
{"date": "2023-01-04", "total": 40},
{"date": "2023-01-05", "total": 50},
{"date": "", "total": 60},
{"date": "2023-01-07", "total": 50}
]
},
"transform": [
{"filter": "isValid(datum.date)"},
{
"window": [{"op": "sum", "field": "total", "as": "cumulative_total"}],
"sort": [{"field": "date", "order": "ascending"}],
"frame": [null, 0]
}
],
"layer": [
{
"mark": "circle",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {
"field": "cumulative_total",
"type": "quantitative",
"title": "Cumulative Total"
},
"tooltip": {"field": "cumulative_total", "type": "quantitative"},
"color": {"value": "blue"}
}
},
{
"mark": "line",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {
"field": "cumulative_total",
"type": "quantitative",
"title": "Cumulative Total"
},
"tooltip": {"field": "cumulative_total", "type": "quantitative"},
"color": {"value": "blue"}
}
},
{
"mark": "circle",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {"field": "total", "type": "quantitative", "title": "total"},
"tooltip": {"field": "total", "type": "quantitative"},
"color": {"value": "red"}
}
},
{
"mark": "line",
"encoding": {
"x": {"field": "date", "type": "temporal", "title": null},
"y": {"field": "total", "type": "quantitative", "title": "Total"},
"tooltip": {"field": "total", "type": "quantitative"},
"color": {"value": "red"}
}
}
],
"config": {}
}
Adam
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论