# Interactivity with Variables

Embeddable’s **variables** make dashboards interactive - so when you change something in one part of the dashboard, it can automatically update another.

> Building [as code](https://docs.embeddable.com/dashboards/dashboards-as-code)? Variables work exactly the same way — define them in the [`variables`](https://docs.embeddable.com/dashboards/dashboards-as-code#variables) block of your `.embeddable.yml` and bind them to inputs and datasets just as you would in the builder.

## How it works

### A user performs an action  
   - e.g. type in a text box, click a button, or select a value from a dropdown.

### A variable gets updated  
   - The dashboard saves what you typed or clicked inside a variable. 
   - Some components [create variables automatically](https://docs.embeddable.com/component-libraries/build-components/interactivity#usage-flow-1) when you add them to the Canvas, but you can also create variables manually. 

### Any connected components and datasets update too  
   - If a variable is connected to a dataset filter, or an input within a component, it will update, causing the data to reload or the component to change in some way. 

That’s the core model: an action updates a variable, and everything connected to it reacts.

> Want to see variables in action, check out our [Quick Hits video tutorial on Variables](https://docs.embeddable.com/video-tutorials/quick-hits/variables) for a step-by-step walkthrough.

## Example: A simple search input

Here's how to create a simple search input that filters a dataset like so:

Video: https://docs.embeddable.com/video/simple-text-filter.mp4

### Add a Text Input component

- Optionally, in the Text Input's Edit panel, set a **placeholder** text like “Type here to search.”

### A "Text Value" variable is created automatically

- Each time the user types, the component fires an event that updates this variable.

### Connect the variable to the dataset

1. Open the settings of the **dataset** you wish to filter (in the bottom right).
2. Click **Add filter**.  
3. Select the dimension or field you want to filter on (e.g. “Country”).  
4. Choose **“equals”** or **“contains”** and link it to the variable by clicking the **link** icon on the right.

Video: https://docs.embeddable.com/video/how-to-add-simple-filter.mp4

### See It in Action

- Switch to **Preview** mode by switching the preview toggle in the top menu.  
- Type a **keyword** into the text input.  
- Embeddable **updates the variable**, which automatically **refreshes the dataset**, showing only matching rows.  
- Any chart or table connected to that dataset now displays filtered data in real time!

**Result**: A quick, code-free **search** feature across your dashboard, powered by a single text input and a variable-based filter.

## Example: Click a pie chart 'segment' to filter data

Here's how to filter data when a user clicks the segment of a pie chart.

Video: https://docs.embeddable.com/video/4-preview-interaction.mp4

### Create a variable  
   - Since adding the pie chart doesn’t automatically generate a variable for this type of interaction, you’ll add one yourself. 
   - Click the **+ icon** on the right, give your variable a **name** (e.g. **country**), and select a **type** (simply `text` in this case). 

Video: https://docs.embeddable.com/video/1-create-variable.mp4

### Set up the pie chart interaction  
   - Click edit on the pie chart.  
   - Under **Interactions**, connect the click event to your new variable and then click Done.
   - Now, whenever someone clicks a slice, the chart fires an event, and Embeddable updates the **country** variable. 

Video: https://docs.embeddable.com/video/2-create-interaction.mp4

### Apply the variable to a dataset filter  
   - In your dataset settings, add a new filter:  
     - Choose the dimension (e.g., Country) you want to match.  
     - Set it to “equals” (or “contains,” depending on your need).  
     - Link it to the **country** variable.  
   - This way, the dataset only loads rows that match the user’s chosen slice.

Video: https://docs.embeddable.com/video/3-link-dataset-filter.mp4

### Test the flow  
   - Switch to **Preview** mode.  
   - Click any pie slice: **country** updates, causing the dataset to reload with that slice’s data.  
   - Any chart or table using this dataset also updates automatically.

## Example: Switch Date Grouping with a Granularity Picker

Here's how to let viewers toggle a chart between **daily**, **weekly**, or **monthly** views.

Video: https://docs.embeddable.com/video/granularity-preview.mp4

### Add a granularity picker component

    - Drop the **Daterange Picker** onto your canvas. You could also add the **Granularity Picker**.
    - A "granularity" variable is created automatically. Each time the user selects a granularity, it will update the **granularity** variable. 

Video: https://docs.embeddable.com/video/add-granularity-picker.mp4

### Link the variable to the granularity input in your chart

- In your time-series chart, you'll find a **granularity** input.  
- This should already have a value selected in the dropdown (e.g. "week").
- Click the link icon on the right, select your **granularity** variable, and click **Done**. 

Video: https://docs.embeddable.com/video/hook-up-granularity.mp4

### Sit back and enjoy the ride

- Switch to **Preview** mode and click different granularity options.
- Watch your chart instantly switch between day, week, or month. 

## Example: Change a Pie Chart's Metrics With a Picker

You can also use **dimensions** and **measures** as variables. This is useful for components that need to filter data based on user selections, like a dropdown that lets users pick a dimension or measure to display.

In order to facilitate this behavior, we provide six predefined picker components in [our starter library](https://docs.embeddable.com/component-libraries/remarkable-pro/introduction) that can be accessed in the no-code builder. These components automatically create variables, allowing users to select dimensions or measures that can be used to filter datasets or drive other components.

Dimensions and measures as variables are powerful because they let users dynamically change the data being displayed in charts or tables without needing to write any code. This adds a significant level of interactivity to your dashboards without requiring additional code on your part.

Video: https://docs.embeddable.com/video/dim-meas-vars-pie-example.mp4

### Add a Measure Picker Component

- Drop the **Measures single select** component onto your canvas. This will create a variable called **Measure choice**. You can also choose which measures you want to include from the **Choices** multiselect dropdown.
- In the right-hand panel, you can set the **default value** to one of your measures (e.g. "Orders"). This will be the initial measure displayed in the pie chart.

Video: https://docs.embeddable.com/video/dim-meas-vars-pie-picker.mp4

### Add a Pie Chart and Connect it to the Picker Variable

- Add a **Pie Chart** component to your canvas.
- Set your **Slice** value to whatever dimension you want to group by (e.g. "Country of Origin").
- For the **Metric**, click the link icon and select the **Measure choice** variable you created earlier. This will allow the pie chart to dynamically change based on the user's selection in the picker.
- Save your changes.

Video: https://docs.embeddable.com/video/dim-meas-vars-pie-chart.mp4

### Test the Interaction

- Switch to **Preview** mode.
- Note that by default the pie chart will show the measure you set as the default in the picker (e.g. "# of Orders").
- Click the dropdown in the **Measure choice** picker and select a different measure (e.g "# of Customers").
- Watch the pie chart update to reflect the new measure, showing the number of customers by country instead of orders.

Video: https://docs.embeddable.com/video/dim-meas-vars-pie-preview.mp4

> It's important to make sure that you use a picker component that matches the type of value your chart is expecting. For example, the Bar Chart's "metric" input expects an array of measures (a list of measures), as it can support displaying multiple measures at once. This means you should use the **MultiSelectorMeasure** component to select measures for a Bar Chart, rather than the **SelectorMeasure** which only allows single selections (and may cause errors).

## Example: Select Bar Chart x-Axis Values With a Picker

[As mentioned above](#example-change-a-pie-charts-metrics-with-a-picker), you can also use **Dimensions** as variables. This example shows how to use a select component to let users choose which dimension to display on the bar chart's x-axis.

Video: https://docs.embeddable.com/video/dim-meas-vars-bar-example.mp4

### Add a Dimension Picker Component

- Drop the **Dimensions select** component onto your canvas. This will create a variable called **Dimension choice**. You can also choose which dimensions you want to include from the **Choices** multiselect dropdown.
- In the right-hand panel, you can set the **default value** to one of your dimensions (e.g. "Country of Origin"). This will be the initial dimension displayed in the bar chart.

Video: https://docs.embeddable.com/video/dim-meas-vars-bar-picker.mp4

### Add a Bar Chart and Connect it to the Picker Variable

- Add a **Bar Chart** component to your canvas.
- For the **x-Axis**, click the link icon and select the **Dimension choice** variable you created earlier. This will allow the bar chart to dynamically change based on the user's selection in the picker.
- For the **metric**, choose a measure (e.g. "# of Orders") that you want to display.
- Save your changes.

Video: https://docs.embeddable.com/video/dim-meas-vars-bar-chart.mp4

### Test the Interaction

- Switch to **Preview** mode.
- Note that by default the bar chart will show the dimension you set as the default in the picker (e.g. "Country of Origin").
- Click the dropdown in the **Dimension choice** picker and select a different dimension (e.g "Product Name").
- Watch the bar chart update to reflect the new dimension, showing the number of orders by product instead of country.

Video: https://docs.embeddable.com/video/dim-meas-vars-bar-preview.mp4

## Setting Default Values

You can set a default variable value that applies when the dashboard first loads. Simple:

1. **Open the Variable Builder**  
   - In the right-hand panel of the builder, select (or create) a variable.
2. **Assign a Default**  
   - In the “Value” field, pick or type the initial value (e.g., “USA” for a Country variable, or “week” for a granularity).

### Passing Variables via Code

If certain values are **customer-specific** (like “United States” for one client and “Germany” for another), you can also [**provide variables programmatically**](https://docs.embeddable.com/deployment/interactivity) when embedding the dashboard in your site. This ensures each user sees the right default context without manually picking it.

## Variable types

Each variable is defined with a **type** - like `text`, `time`, or `number`. When you link a variable to an input or dataset filter, both sides must share a **matching type**. For instance, you can’t connect a **number** input to a **text** variable.

**Note**: In the future, we plan to offer more flexibility around how different types can interact.

## Filters as variables

Some components — like a filter-builder UI — don't just capture a single value, they capture a whole filter expression, with its own `AND`/`OR` logic and nested groups. For this, Embeddable has a native **`filters`** variable type: connect one to a component's filter-building event, then apply it to a dataset (via the dataset settings modal or a component's dataset filters) just like any other variable, and it's added on top of your existing filters using `AND`.

See [`filters`: a native variable type](https://docs.embeddable.com/component-libraries/build-components/interactivity#filters-a-native-variable-type) for the full details, including how to wire this up in a custom component.

---
Source: https://docs.embeddable.com/dashboards/interactivity
