Select to view content in your preferred language

Dynamically filter data shown in a Chart widget

1276
2
Jump to solution
12-25-2024 02:42 AM
JAYAKUMARKUMAR
Emerging Contributor

Hi,

 

How can I use a custom SQL query with a Feature Service data source in Experience Builder to dynamically filter data shown in a Chart widget based on user input?

1 Solution

Accepted Solutions
SumitMishra_016
Frequent Contributor

To filter data in your Experience Builder chart:

  1. Find the data source: Figure out where your chart gets its information (it's likely a "Feature Service").
  2. Let users choose filters: Use widgets like text boxes or dropdowns to let users select how they want to filter the data.
  3. Write a smart SQL query: Create an SQL query that changes based on what the user selects.
  4. Tell the chart to use the new query: Apply the query to your chart's data source.
  5. Refresh the chart: Make the chart display the filtered data.

 

const textBox = app.getWidgetById('textbox1');
const chartWidget = app.getWidgetById('chart1');
const featureServiceDataSource = chartWidget.dataSource;
textBox.on('change', (event) => {
  const userInput = event.target.value; 

  
  const sqlQuery = `YourFieldName = '${userInput}'`;

  
  featureServiceDataSource.setQuery({
    where: sqlQuery,
    outFields: ['*'],
  });

  
  chartWidget.refresh();

  console.log(`Applied SQL query: ${sqlQuery}`);
});

 

View solution in original post

2 Replies
SumitMishra_016
Frequent Contributor

To filter data in your Experience Builder chart:

  1. Find the data source: Figure out where your chart gets its information (it's likely a "Feature Service").
  2. Let users choose filters: Use widgets like text boxes or dropdowns to let users select how they want to filter the data.
  3. Write a smart SQL query: Create an SQL query that changes based on what the user selects.
  4. Tell the chart to use the new query: Apply the query to your chart's data source.
  5. Refresh the chart: Make the chart display the filtered data.

 

const textBox = app.getWidgetById('textbox1');
const chartWidget = app.getWidgetById('chart1');
const featureServiceDataSource = chartWidget.dataSource;
textBox.on('change', (event) => {
  const userInput = event.target.value; 

  
  const sqlQuery = `YourFieldName = '${userInput}'`;

  
  featureServiceDataSource.setQuery({
    where: sqlQuery,
    outFields: ['*'],
  });

  
  chartWidget.refresh();

  console.log(`Applied SQL query: ${sqlQuery}`);
});

 

JanetSilb_Spike
Frequent Contributor

@SumitMishra_016 can you please explain more about this?  Are you suggesting to add text or drop down widgets in addition to the chart widget, or to configure the query within the chart widget?  thanks!

Janet Silbernagel, PhD, ASLA, SITES AP, WBE
Silvernail Studio for Geodesign, LLC
https://www.linkedin.com/in/jsilb/
0 Kudos