# Context variables in the Custom Chart

**URL:** <https://forums.losant.com/t/context-variables-in-the-custom-chart/2950>\
**Category:** Help\
**Tags:** custom-chart-block, dashboard, context-variables\
**Created:** [May 18, 2020, 6:17pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950 "2020-05-18T18:17:32Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucasbtsuchiya](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/lucasbtsuchiya/32/2621_2.png) [@lucasbtsuchiya](https://forums.losant.com/u/lucasbtsuchiya)\
**Post date:** [May 18, 2020, 6:17pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/1 "2020-05-18T18:17:33Z")

</div>

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},  
]  
}

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [May 18, 2020, 6:21pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/2 "2020-05-18T18:21:49Z")

</div>

@lucasbtsuchiya,

Welcome to the Losant Forums! 👋

Given that you have a context variable called `variable`, yes. The Custom Chart Block fully supports [context variables](https://docs.losant.com/dashboards/context-variables/).

---

<div class="post-metadata">

**Author:** ![lucasbtsuchiya](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/lucasbtsuchiya/32/2621_2.png) [@lucasbtsuchiya](https://forums.losant.com/u/lucasbtsuchiya)\
**Post date:** [May 18, 2020, 6:29pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/3 "2020-05-18T18:29:22Z")

</div>

I’m trying to use it as follows:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/f/f85cb9da43e43ec50e93c66275c06539b63ae58c.png)

However, it does not accept this configuration. Would there be another way to do the reading?

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [May 18, 2020, 6:33pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/6 "2020-05-18T18:33:20Z")

</div>

@lucasbtsuchiya,

I think the issue is missing quotes. Templating does not automatically insert the quotes for you. You must do either:

```auto
"{{ctx.variable}}" 

```

Or, the `jsonEncode` helper will also put in the quotes for you:

```auto
{{ jsonEncode ctx.variable}} 

```

---

<div class="post-metadata">

**Author:** ![lucasbtsuchiya](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/lucasbtsuchiya/32/2621_2.png) [@lucasbtsuchiya](https://forums.losant.com/u/lucasbtsuchiya)\
**Post date:** [May 18, 2020, 6:39pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/7 "2020-05-18T18:39:05Z")

</div>

> [@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.

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [May 18, 2020, 6:53pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/10 "2020-05-18T18:53:59Z")

</div>

@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](https://docs.losant.com/dashboards/context-variables/#numbers). 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”?

---

<div class="post-metadata">

**Author:** ![lucasbtsuchiya](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/lucasbtsuchiya/32/2621_2.png) [@lucasbtsuchiya](https://forums.losant.com/u/lucasbtsuchiya)\
**Post date:** [May 18, 2020, 7:12pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/11 "2020-05-18T19:12:42Z")

</div>

The default value of the context variable is a Device attribute.

![forum3](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/f/f10574aa6fa0e0a7f2f59a12711c522ad7e81030.png)

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.

---

<div class="post-metadata">

**Author:** ![lucasbtsuchiya](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/lucasbtsuchiya/32/2621_2.png) [@lucasbtsuchiya](https://forums.losant.com/u/lucasbtsuchiya)\
**Post date:** [May 18, 2020, 7:44pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/13 "2020-05-18T19:44:17Z")

</div>

Would it be possible to take the value of a query and insert it in my “value” field?

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/a/aa4e4f49c870208f08a74e1ec4369b11c6ec2df3.png)

“data”: [  
{  
“name”: “table”,  
“values”: [  
{“key”: “Eixo X”, “value”: {{query\_name}}, “category”: 0},  
}  
]

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [May 19, 2020, 2:05pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/15 "2020-05-19T14:05:11Z")

</div>

To answer your first question

1. You can simply access your tag value like so:

`{{ctx.tag.value}}`

1. The queries are only added using the [datasets](https://vega.github.io/vega-lite/docs/data.html#datasets) property.

However, you can request data using the [URL](https://vega.github.io/vega-lite/docs/data.html#url) property. Like so:

```auto
{
  "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](https://docs.losant.com/experiences/endpoints/) or [Webhook](https://docs.losant.com/applications/overview/#webhooks).

---

<div class="post-metadata">

**Author:** ![lucasbtsuchiya](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/lucasbtsuchiya/32/2621_2.png) [@lucasbtsuchiya](https://forums.losant.com/u/lucasbtsuchiya)\
**Post date:** [May 19, 2020, 5:07pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/16 "2020-05-19T17:07:17Z")

</div>

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  
 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/3/3a58d36ae118efdcc9f4d244e089165ec61cd17f.png)

“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.

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [May 19, 2020, 9:18pm UTC](https://forums.losant.com/t/context-variables-in-the-custom-chart/2950/19 "2020-05-19T21:18:56Z")

</div>

@lucasbtsuchiya,

Unfortunately, no. For the Custom Chart Block, the only way to access your query data is through the [datasets](https://vega.github.io/vega-lite/docs/data.html#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](https://docs.losant.com/dashboards/custom-html/).
