livebook-dev / vega_lite

Elixir bindings for Vega-Lite

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for conditionals?

cigrainger opened this issue · comments

Hi there! I'm trying to conditionally set the stroke_width by following the general structure here.

I've tried Vl.encode(spec, :stroke_width, value: 1) and that works great. It's just unclear to me as to how to add the condition clause. I've tried adding a condition keyword like Vl.encode(spec, :stroke_width, value: 1, condition: %{field: "field_name", type: :nominal, value: 5}) but no luck. I've tried a few permutations (with and without the type, with and without the value).

Any tips or direction would be greatly appreciated. 🙏

Hey @cigrainger! The condition needs either "param" (user-driven, like area selection) or "test" (an actual condition, like checking if value is in some range). Then you specify what value to bind to the given channel, which can be "field" or a constant value with "value", not both.

For example

# Use stroke_width 5 for points with height > 100
Vl.encode(spec, :stroke_width, value: 1, condition: %{test: "datum.height > 100", value: 5})

Vl.param(spec, "brush", select: [type: :interval])
# Use stroke_width 5 for points within the given selection
Vl.encode(spec, :stroke_width, value: 1, condition: %{param: "brush", value: 5})

Let me know if this does what you need :)

Thanks so much @jonatanklosko! This makes sense now and it works. 🙏