在Vega Lite中进行筛选和求和

huangapple go评论57阅读模式
英文:

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:

&quot;transform&quot;: [
{&quot;filter&quot;: &quot;isValid(datum.date)&quot;}
.....

在Vega Lite中进行筛选和求和

{
&quot;$schema&quot;: &quot;https://vega.github.io/schema/vega-lite/v5.json&quot;,
&quot;description&quot;: &quot;Two lines: one cumulated using window transform and totals, with blank dates filtered out.&quot;,
&quot;data&quot;: {
&quot;values&quot;: [
{&quot;date&quot;: &quot;2023-01-01&quot;, &quot;total&quot;: 10},
{&quot;date&quot;: &quot;2023-01-02&quot;, &quot;total&quot;: 20},
{&quot;date&quot;: &quot;&quot;, &quot;total&quot;: 30},
{&quot;date&quot;: &quot;2023-01-04&quot;, &quot;total&quot;: 40},
{&quot;date&quot;: &quot;2023-01-05&quot;, &quot;total&quot;: 50},
{&quot;date&quot;: &quot;&quot;, &quot;total&quot;: 60},
{&quot;date&quot;: &quot;2023-01-07&quot;, &quot;total&quot;: 50}
]
},
&quot;transform&quot;: [
{&quot;filter&quot;: &quot;isValid(datum.date)&quot;},
{
&quot;window&quot;: [{&quot;op&quot;: &quot;sum&quot;, &quot;field&quot;: &quot;total&quot;, &quot;as&quot;: &quot;cumulative_total&quot;}],
&quot;sort&quot;: [{&quot;field&quot;: &quot;date&quot;, &quot;order&quot;: &quot;ascending&quot;}],
&quot;frame&quot;: [null, 0]
}
],
&quot;layer&quot;: [
{
&quot;mark&quot;: &quot;circle&quot;,
&quot;encoding&quot;: {
&quot;x&quot;: {&quot;field&quot;: &quot;date&quot;, &quot;type&quot;: &quot;temporal&quot;, &quot;title&quot;: null},
&quot;y&quot;: {
&quot;field&quot;: &quot;cumulative_total&quot;,
&quot;type&quot;: &quot;quantitative&quot;,
&quot;title&quot;: &quot;Cumulative Total&quot;
},
&quot;tooltip&quot;: {&quot;field&quot;: &quot;cumulative_total&quot;, &quot;type&quot;: &quot;quantitative&quot;},
&quot;color&quot;: {&quot;value&quot;: &quot;blue&quot;}
}
},
{
&quot;mark&quot;: &quot;line&quot;,
&quot;encoding&quot;: {
&quot;x&quot;: {&quot;field&quot;: &quot;date&quot;, &quot;type&quot;: &quot;temporal&quot;, &quot;title&quot;: null},
&quot;y&quot;: {
&quot;field&quot;: &quot;cumulative_total&quot;,
&quot;type&quot;: &quot;quantitative&quot;,
&quot;title&quot;: &quot;Cumulative Total&quot;
},
&quot;tooltip&quot;: {&quot;field&quot;: &quot;cumulative_total&quot;, &quot;type&quot;: &quot;quantitative&quot;},
&quot;color&quot;: {&quot;value&quot;: &quot;blue&quot;}
}
},
{
&quot;mark&quot;: &quot;circle&quot;,
&quot;encoding&quot;: {
&quot;x&quot;: {&quot;field&quot;: &quot;date&quot;, &quot;type&quot;: &quot;temporal&quot;, &quot;title&quot;: null},
&quot;y&quot;: {&quot;field&quot;: &quot;total&quot;, &quot;type&quot;: &quot;quantitative&quot;, &quot;title&quot;: &quot;total&quot;},
&quot;tooltip&quot;: {&quot;field&quot;: &quot;total&quot;, &quot;type&quot;: &quot;quantitative&quot;},
&quot;color&quot;: {&quot;value&quot;: &quot;red&quot;}
}
},
{
&quot;mark&quot;: &quot;line&quot;,
&quot;encoding&quot;: {
&quot;x&quot;: {&quot;field&quot;: &quot;date&quot;, &quot;type&quot;: &quot;temporal&quot;, &quot;title&quot;: null},
&quot;y&quot;: {&quot;field&quot;: &quot;total&quot;, &quot;type&quot;: &quot;quantitative&quot;, &quot;title&quot;: &quot;Total&quot;},
&quot;tooltip&quot;: {&quot;field&quot;: &quot;total&quot;, &quot;type&quot;: &quot;quantitative&quot;},
&quot;color&quot;: {&quot;value&quot;: &quot;red&quot;}
}
}
],
&quot;config&quot;: {}
}

Adam

huangapple
  • 本文由 发表于 2023年4月17日 18:06:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定