Is there a way to read the value of a context variable in the Custom Chart dashboard using the Vega-lite library?
“data”: [
{
“name”: “table”,
“values”: [
{“key”: “Eixo X”, “value”: {{ctx.variable}}, “category”: 0},
{“key”: “Eixo Y”, “value”: 22, “category”: 0},
{“key”: “Eixo Z”, “value”: 14, “category”: 0},
]
}
1 Like
@lucasbtsuchiya ,
Welcome to the Losant Forums!
Given that you have a context variable called variable
, yes. The Custom Chart Block fully supports context variables .
I’m trying to use it as follows:
However, it does not accept this configuration. Would there be another way to do the reading?
1 Like
@lucasbtsuchiya ,
I think the issue is missing quotes. Templating does not automatically insert the quotes for you. You must do either:
"{{ctx.variable}}"
Or, the jsonEncode
helper will also put in the quotes for you:
{{ jsonEncode ctx.variable}}
lucasbtsuchiya:
variable
I had already made this configuration. But it does not return the defined value, it returns the name of the variable.
I need him to return the stored value to me.
@lucasbtsuchiya ,
Thank you for the follow-up.
It looks like your value is a number, not a string. My comments above were for strings. But, since you have a number here, the quotes are not needed.
What’s being returned isn’t the name of the variable. It’s the default value . While you’re previewing blocks, the context variables use the default values (since there is no context yet).
Would the default of this context variable happen to be “axialVibration”?
The default value of the context variable is a Device attribute.
What I’m trying to do is to enter a value of the context variable in the “value” field. For example, I am reading the width value in my field. Only in place of “width” would be the value of the attribute.
Would it be possible to take the value of a query and insert it in my “value” field?
“data”: [
{
“name”: “table”,
“values”: [
{“key”: “Eixo X”, “value”: {{query_name}}, “category”: 0},
}
]
To answer your first question
You can simply access your tag value like so:
{{ctx.tag.value}}
The queries are only added using the datasets property.
However, you can request data using the URL property. Like so:
{
"width": {{block.width}},
"height": {{block.height}},
"autosize": {
"type": "fit",
"contains": "padding"
},
"data": {"url": "https://vega.github.io/editor/data/cars.json"},
"mark": "point",
"encoding": {
"x": {"field": "Horsepower", "type": "quantitative"},
"y": {"field": "Miles_per_Gallon", "type": "quantitative"}
}
}
For you, your endpoint could be an Experience Endpoint or Webhook .
Context variable of the tag type {{ctx.tag.value}} and numeric {{ctx.variable}} works. Thank you!
Is there a way to store the value of my query and insert it in the “value” field?
Example
“data”: {“name”: “gauge_query”},
“data”: [
{
“name”: “table”,
“values”: [
{“key”: “X”, “value”: (query_result), “category”: 0},
In the example of your answer, you use the query value to plot the graph.
@lucasbtsuchiya ,
Unfortunately, no. For the Custom Chart Block, the only way to access your query data is through the datasets property.
If you’re looking for more flexibility and you’re comfortable with HTML/CSS/JS, you should check out the Custom HTML Block .