How To Perform Certain Operations If Filter Is Not Exist in Vega Visualisation

Is it possible to code Vega in such a way as to perform certain operations if filter is not applied?

objective: if the filter is applied, then perform code given below, if filter is not applied then perform sum of a different field value

Here is my Vega code for reference :

{
  $schema: https://vega.github.io/schema/vega/v3.0.json
  data: [
    {
      name: latestTotalBalance
      url: {
        %context%: true
        %timefield%: @timestamp
        index: wallet-info*
        body: {
          aggs: {
            label: {
              terms: {
                field: info.displayName.keyword
                size: 30
                order: {_key: "desc"}
              }
              aggs: {
                balance: {
                  top_hits: {
                    _source: confirmedBalanceUnitAmount
                    size: 2
                    sort: [
                      {
                        @timestamp: {order: "desc"}
                      }
                    ]
                  }
                }
              }
            }
          }
          size: 0
        }
      }
      format: {property: "aggregations.label.buckets"}
      transform: [
        {
          type: aggregate
          ops: ["sum"]
          fields: ["balance.hits.hits[0]._source.confirmedBalanceUnitAmount"]
          as: ["totalUnitAmount"]
        }
      ]
    }
  ]
  marks: [
    {
      type: text
      from: {data: "latestTotalBalance"}
      encode: {
        update: {
          text: {signal: "format(datum.totalUnitAmount, ',')"}
          align: {value: "center"}
          baseline: {value: "middle"}
          xc: {signal: "width/2"}
          yc: {signal: "height/2"}
          fontSize: {signal: "33"}
          "fill": {"value": "white"}
          "fontWeight":{"value": "bold"}
          text: {signal: "format(datum.totalUnitAmount, ',.5f')"}
        }
      }
    }
  ]
}

it would be a great assistance if you could give your inputs on this @wylie !!

If I am understanding you right, then you are looking at having valueFiltered show if filter is set or just value if filter is not set. If that is the case, then you would need two data calls; one with your end result that listens to filters and one that does not. So let's have data set name valuesFiltered (listens to %context%) with whatever your value is named as "value" and data set name values with whatever your value is named as "value" (does NOT listen to %context%). Then, in your text mark, you would have "text": {"signal": "data('valuesFiltered')[0].value == data('values')[0].value ? data('values')[0].value : data('valuesFiltered')[0].value"}. In other words, if the filtered value and the non-filtered value equal, then show the non-filtered value. If they differ, then show the filtered value.

In your current code, you have totalUnitAmount as "value" which would be fine, just make sure both data calls are the same input/output, just one has %context% and one does not. And then you update that text code above to .totalUnitAmount instead of .value.

Does that help?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.