# Coupling Sliders in GLMakie

**URL:** <https://discourse.julialang.org/t/coupling-sliders-in-glmakie/88130>\
**Category:** Visualization\
**Tags:** makie, glmakie\
**Created:** [October 2, 2022, 5:53pm UTC](https://discourse.julialang.org/t/coupling-sliders-in-glmakie/88130 "2022-10-02T17:53:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lassepe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lassepe/32/10050_2.png) [@lassepe](https://discourse.julialang.org/u/lassepe)\
**Post date:** [October 2, 2022, 5:53pm UTC](https://discourse.julialang.org/t/coupling-sliders-in-glmakie/88130/1 "2022-10-02T17:53:53Z")

</div>

I would like to be able to “couple” two sliders of a `Makie.SliderGrid` so that their value always adds to 1. That is, if I move the first slider I would like to `Makie.set_close_to!(slider2, 1-slider1.value)` and vice versa. However, I cannot figure out a way how to make this work. All of the ways that I can think of would introduce circular dependencies of `Observable`s.

**Edit:** I should also say that I am looking for a solution that generalizes to n \> 2 sliders.

Here’s a sub of an example which has everything except for the coupling of the sliders

```julia
using GLMakie: GLMakie
using Makie: Makie

figure = Makie.Figure()

# create two sliders
slider1, slider2 =
    Makie.SliderGrid(
        figure[2, 1],
        (; label = "Slider 1", range = -1:0.01:1),
        (; label = "Slider 2", range = -1:0.01:1),
    ).sliders

# create a slimple scatter point based on the slider (just a dummy for the purpose of this example)
data = Makie.@lift [Makie.Point2f($(slider1.value), $(slider2.value))]
Makie.scatter(figure[1, 1], data)

display(figure)

```

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 2, 2022, 7:41pm UTC](https://discourse.julialang.org/t/coupling-sliders-in-glmakie/88130/2 "2022-10-02T19:41:07Z")

</div>

Mathematically, with some equations. How would you solve it for n=3?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 2, 2022, 8:26pm UTC](https://discourse.julialang.org/t/coupling-sliders-in-glmakie/88130/3 "2022-10-02T20:26:09Z")

</div>

I think you need to make an observable with a tuple of the n values. Then for each slider value observable, make it so changing the value computes the n new values for the tuple and set that tuple. When that observable triggers, set all the sliders to the new values. This would be cyclical normally. But you can set it up so that the slider observable functions only change the tuple value when a boolean state variable is false. So when the tuple observable function triggers, set this bool or these bools to true first. The sliders will be updated but the tuple observable won’t fire again.

The values you need downstream you’ll take from the single tuple observable, not each slider.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 2, 2022, 9:23pm UTC](https://discourse.julialang.org/t/coupling-sliders-in-glmakie/88130/4 "2022-10-02T21:23:35Z")

</div>

Essentially, you have the solution, and the only thing left is to break the circularity. This can be done by triggering a change only when slider state is out of the ‘good’ set of states. And to make sure the changes bring you back into the ‘good’ set of states (otherwise a “doom loop” can occur, like you suggested).

So for the example in the post:

```julia
using Observables

obs_func = on(slider1.value) do val
    abs(val+slider2.value[])>0.01 && Makie.set_close_to!(slider2, -val)
end

obs_func2 = on(slider2.value) do val
    abs(slider1.value[]+val)>0.01 && Makie.set_close_to!(slider1, -val)
end

```

The code makes sure `slider1` and `slider2` sum to zero.  
When I ran it, it worked quite smoothly.

---

<div class="post-metadata">

**Author:** ![lassepe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lassepe/32/10050_2.png) [@lassepe](https://discourse.julialang.org/u/lassepe)\
**Post date:** [October 3, 2022, 9:31am UTC](https://discourse.julialang.org/t/coupling-sliders-in-glmakie/88130/5 "2022-10-03T09:31:34Z")

</div>

Nice. The last solution proposed by @Dan seems to be exactly what I want. This also avoids adding an extra boolean state for breaking the circular dependency (though that would also be fine in my case).

> [@lazarusA](#):
>
> Mathematically, with some equations. How would you solve it for n=3?

That is a good point. I have to think about what kind of “projection logic” I would want to have in that case. In worst case, I can always solve an LP but I’ll figure that out offline.

Thank you everyone!
