Global Variables in Canvas

Is it possible to define a global variable in Canvas. At the moment I am defining a target, but when I update this target I have to change queries of each individually. Thanks

@JamesNotJamez I don't think this is possible.

But perhaps @Joe_Fleming or @Catherine_Liu know more?

@JamesNotJamez There's not a way to create a "global" value directly, but you could perhaps use the urlparam function as a substitute here. That function allows you to read a value from query params in the URL.

When you have a workpad open, the URL in your browser should look something like this: http://localhost:5601/app/canvas#/workpad/...

You can add query params before the # in there, like so: http://localhost:5601/app/canvas?target=5000#/workpad/

So now you have a query param named target, which you can use with the following expression:

urlparam param=target default=1000

This will read the value of target in the url, and if it's not set, default the value to 1000. In the URL I used above there, the value would be 5000. You can use it as a subexpression to get the value of the upper bound. As a simple and somewhat contrived example:

escount index="your-index-or-index-pattern"
| math {string "value / " {urlparam param=target default=1000}}

Now when you want to adjust the goal value, you can just set a new value in the URL, and it will simply use 1000 by default.

The downside is that the value in the URL isn't preserved, if you just open the workpad like normal, the value won't be there, and it'll fall back to 1000. This really only works for situations where you are linking to a workpad directly somewhere.

You gave me an interesting idea for the application though; the ability to define arbitrary metadata on a workpad, which would persist, and would be easy enough to modify to update all elements. I've opened an enhancement issue to track that idea here, feel free to follow or comment:

1 Like

Oh, alternatively, you could read that goal value from some external source as well. It would be more work though, requiring you put that value somewhere you can modify it, and then write a function to go fetch it.

Amazing, thanks! I'll have a look into those suggestions. The URL params definitely seems like the best solution for now, but I'll follow the issue with interest to see if it leads to any new features.

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