Search code examples
vega-lite

sorting on field with an additional filter in Vega-lite


This might be a somewhat obscure use case.

As you can see below, I have the bar (count) overlaid. I want to sort the bars in the background (where is_overview set to 1), but currently, the filtering is set to all of count, which includes is_overview being set to 0.

I need the sort to be on a filtered field.

I went through the sorting documentation but I cannot figure out a way to support this use case. If you might have ideas, I would really appreciate the help!

enter image description here

Editor code


Solution

  • If you want custom sort behavior, often the best approach is to use a calculate transform, which makes available all of the vega expression syntax, and define a new custom field on which to sort.

    In your example, you could do something like this:

      "transform": [
        {"calculate": "datum.is_overview ? datum.count : null", "as": "order"}
      ],
    

    and then sort on the order property.

    The result looks like this (vega editor):

    enter image description here