简体   繁体   中英

Is it possible to achieve this aggregation using vega/vega-lite?

I have a data list of this format

[
    {"id": 100, "y": 28, "c":0},
    {"id": 1, "y": 20, "c":1},
    {"id": 2, "y": 43, "c":0},
    {"id": 3, "y": 35, "c":1},
    {"id": 4, "y": 81, "c":0},
    {"id": 5, "y": 10, "c":1},
    {"id": 6, "y": 19, "c":0},
    {"id": 7, "y": 15, "c":1},
    {"id": 8, "y": 52, "c":0},
    {"id": 9, "y": 48, "c":1}
]

My goal is to achieve sum aggregation of x and y fields for all documents excluding id=100, then to subtract this result of aggregation from x and y values of document with id=100, and display this result as text type mark. I've tried the following:

{
  $schema: https://vega.github.io/schema/vega/v3.0.json
  title: Sum amount Per id
  data: [
    {
      "name": "table",
      "values": [
    {"id": 100, "y": 28, "c":0},
    {"id": 1, "y": 20, "c":1},
    {"id": 2, "y": 43, "c":0},
    {"id": 3, "y": 35, "c":1},
    {"id": 4, "y": 81, "c":0},
    {"id": 5, "y": 10, "c":1},
    {"id": 6, "y": 19, "c":0},
    {"id": 7, "y": 15, "c":1},
    {"id": 8, "y": 52, "c":0},
    {"id": 9, "y": 48, "c":1}
]
      transform: [
        {
          type: aggregate
          ops: ["sum","sum"]
          fields: ["c", "y"]
          as: ["sumc","sumy"]
        }
        
      ]
    }
  ]
  marks: [
    {
      type: text
      from: {data: "table"}
      encode: {
        update: {
          text: {signal: "datum.sumc"}
          align: {value: "center"}
          baseline: {value: "middle"}
          xc: {signal: "width/4"}
          yc: {signal: "height/2"}
          fontSize: {signal: "min(width/10, height)/1.3"}
        }
      }
    }
    {
      type: text
      from: {data: "table"}
      encode: {
        update: {
          text: {signal: "datum.sumy"}
          align: {value: "center"}
          baseline: {value: "middle"}
          xc: {signal: "width*3/4"}
          yc: {signal: "height/2"}
          fontSize: {signal: "min(width/10, height)/1.3"}
        }
      }
    }
  ]
}

Please help me with how to achieve the subtraction from id=100

I was able to solve this using the JoinAggregate transform of Vega by adding the aggregation values as additional columns to the data set and then filtering to get a single row with the desired values!

{
  "$schema": "https://vega.github.io/schema/vega/v3.0.json",
  "title": "Sum amount Per id",
  "data": [
    {
      "name": "table",
      "values": [
        {"id": 100, "y": 2800, "c": 1000},
        {"id": 1, "y": 20, "c": 1},
        {"id": 2, "y": 43, "c": 0},
        {"id": 3, "y": 35, "c": 1},
        {"id": 4, "y": 81, "c": 0},
        {"id": 5, "y": 10, "c": 1},
        {"id": 6, "y": 19, "c": 0},
        {"id": 7, "y": 15, "c": 1},
        {"id": 8, "y": 52, "c": 0},
        {"id": 9, "y": 48, "c": 1}
      ],
      "transform": [
        {
          "type": "joinaggregate",
          "ops": ["sum", "sum"],
          "fields": ["c", "y"],
          "as": ["sumc", "sumy"]
        },{
        "type":"filter"
        "expr":"datum.id==100"
        }
      ]
    }
  ],
  "marks": [
    {
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "update": {
          "text": {"signal": "-datum.sumc+datum.c*2"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "xc": {"signal": "width/4"},
          "yc": {"signal": "height/2"},
          "fontSize": {"signal": "min(width/10, height)/1.3"}
        }
      }
    },
    {
      "type": "text",
      "from": {"data": "table"},
      "encode": {
        "update": {
          "text": {"signal": "datum.y-datum.sumy+datum.y"},
          "align": {"value": "center"},
          "baseline": {"value": "middle"},
          "xc": {"signal": "width*3/4"},
          "yc": {"signal": "height/2"},
          "fontSize": {"signal": "min(width/10, height)/1.3"}
        }
      }
    }
  ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM