I am trying to create a toggle button using Vega viz to add/remove a Dashboard filter. Everything works fine except when I toggle it ON, the filter gets added but the toggle button jumps back to the OFF state automatically. I have to click for the second time to get the button to stay in the ON state. However, toggling OFF works fine with one click. Debugging shows that the value of the toggle signal resets to the initial value in the first click.
Please find the code below.
{
"$schema": "https://vega.github.io/schema/vega/v5.json",
"width": 350,
"height": 350,
"autosize": "none",
"description": "Toggle Button",
"signals": [
{
"name": "toggle",
"value": false,
"on": [
{
"events": {"type": "click", "markname": "circle"},
"update": "toggle ? false : true"
}
]
},
{
"name": "addFilter",
"on": [
{
"events": {"type": "click", "markname": "circle"},
"update": "toggle ? kibanaAddFilter({ match_phrase: { \"machine.os.keyword\": 'osx' }}) : kibanaRemoveAllFilters()"
}
]
}
],
"marks": [
{
"name": "circle",
"type": "symbol",
"zindex": 1,
"encode": {
"enter": {
"y": {"signal": "height/2"},
"angle": {"value": 0},
"size": {"value": 400},
"shape": {"value": "circle"},
"fill": {"value": "white"},
"stroke": {"value": "white"},
"strokeWidth": {"value": 2},
"cursor": {"value": "pointer"},
"tooltip": {"signal": "{Tip: 'Click to add filter'}"}
},
"update": {"x": {"signal": "toggle === true ? 190 : 165"}}
}
},
{
"name": "rectangle",
"type": "rect",
"zindex": 0,
"encode": {
"enter": {
"x": {"value": 152},
"y": {"value": 162.5},
"width": {"value": 50},
"height": {"value": 25},
"cornerRadius": {"value": 20}
},
"update": {
"fill": {"signal": "toggle === true ? '#006BB4' : '#939597'"}
}
}
}
]
}
What am I doing wrong? Your kind help would be appreciated.
Note: I am using Elastic v 7.9.3