Modifying Simply Ask Natural Language Query Generated Widgets with Scripting
dashboard.on('domready', function () {
// Listen for clicks on the Simply Ask button
$('button.nlq-main-button').click(function () {
setTimeout(function () {
// Select the widget container area of the Simply Ask modal
const elementToObserve = $('div.nlq-widget__main-content')[0];
// Create a MutationObserver instance
const observer = new MutationObserver(function () {
// Call your custom function when a change in DOM is detected
customizeNLQWidget();
});
// Start observing the DOM for mutations
observer.observe(elementToObserve, { subtree: true, childList: true });
}, 1000); // Delay to ensure the modal is fully rendered
});
});
Explanation:
- Event Listener: The script runs on the click event using an event listener on the Simply Ask UI button (`nlq-main-button`) which opens the Simply Ask NLQ modal.
- Delay with setTimeout: A short delay ensures that the Simply Ask modal is fully rendered before setting up the observer.
- MutationObserver: Observes the NLQ-generated widget HTML element using the selector `nlq-widget__main-content` for any changes, specifically when new child nodes are added (i.e., when a new widget is generated on an NLQ query).
- subtree: Ensures that mutations to child elements of the main content area are also detected.
- childList: Watches for changes in the direct children of the observed element (e.g., when new widgets are added).
- Callback Function: When a mutation is observed, the `customizeNLQWidget` function is called to apply custom modifications. This can be any function or series of functions.
Example Customization Function
The function that applies the desired modifications to the NLQ widget can be defined with any valid JavaScript. In this example, the function will modify the pivot header cells' styling.
const customizeNLQWidget = () => {
const NLQ_MAIN_CONTENT_SELECTOR = 'div.nlq-widget__main-content';
const HEADER_CELL_SELECTOR = 'td.table-grid__cell--row-0';
// Apply CSS styles to the pivot table header cells
$(`${NLQ_MAIN_CONTENT_SELECTOR} ${HEADER_CELL_SELECTOR}`).css({
'background-color': 'blue',
'color': 'white',
});
};
Explanation:
- Selectors: The function targets the pivot table header cells within the NLQ modal. These can be any valid CSS selector, in this example they are:
- `NLQ_MAIN_CONTENT_SELECTOR`: Selects the main content area of the NLQ widget.
- `HEADER_CELL_SELECTOR`: Selects the header cells of a pivot table type widget.
- CSS Styling: Applies custom CSS styling to change the background color to blue and the text color to white.
Combining the Code
For clarity, here's the combined code that brings together the observer setup and the example custom code customization function:
dashboard.on('domready', function () {
$('button.nlq-main-button').click(function () {
setTimeout(function () {
const elementToObserve = $('div.nlq-widget__main-content')[0];
const observer = new MutationObserver(function () {
customizeNLQWidget();
});
observer.observe(elementToObserve, { subtree: true, childList: true });
}, 1000);
});
});
const customizeNLQWidget = () => {
const NLQ_MAIN_CONTENT_SELECTOR = 'div.nlq-widget__main-content';
const HEADER_CELL_SELECTOR = 'td.table-grid__cell--row-0';
$(`${NLQ_MAIN_CONTENT_SELECTOR} ${HEADER_CELL_SELECTOR}`).css({
'background-color': 'blue',
'color': 'white',
});
};
Customization Examples
The example above modifies the header cells of a pivot table. However, you can extend the `customizeNLQWidget` function to perform various other customizations, such as:
- Adding Icons or Indicators: Inject visual cues for critical data points or thresholds directly into cells.
- Conditional Formatting: Automatically highlight data cells based on values (e.g., flagging negative values in red).
- Interactive Elements: Add clickable elements, like links or buttons, to enable dynamic user interactions, such as drilling into specific data.
- Content Modification: Alter or append units, currency symbols, or other data-specific indicators to ensure clarity and consistency in data presentation.
Example: Conditional Formatting Based on Values
const customizeNLQWidget = () => {
const NLQ_MAIN_CONTENT_SELECTOR = 'div.nlq-widget__main-content';
const DATA_CELL_SELECTOR = 'td.table-grid__cell--data';
$(`${NLQ_MAIN_CONTENT_SELECTOR} ${DATA_CELL_SELECTOR}`).each(function () {
const cellValue = parseFloat($(this).text());
if (cellValue < 0) {
$(this).css('color', 'red');
} else if (cellValue > 0) {
$(this).css('color', 'green');
}
});
};
Explanation:
- Data Cells Selector: Targets all data cells in the pivot table.
- Value Parsing: Converts the cell text to a numeric value.
- Conditional Styling: Applies red color to negative values and green color to positive values.
Considerations and Best Practices
- Scope: Ensure your selectors are specific enough to avoid unintended modifications to other parts of the dashboard.
- Compatibility Testing: Regularly test your customizations across different Sisense versions to ensure that changes in CSS classes or HTML structures don't break the functionality.
- CSS Class Updates: CSS class names can change between Sisense versions; keeping CSS selector strings as variables allows for easy updating if needed.
Conclusion
While NLQ-generated widgets in Sisense's Simply Ask feature do not support standard widget scripting events, the ability to observe DOM changes and manipulate HTML allows for extensive customizations. With JavaScript, you can enhance widget appearance, add interactive functionality, and more—all within the Simply Ask modal, providing a tailored user experience.
This approach allows maintaining consistency in dashboards and provides a more customizable experience for users interacting with NLQ-generated visualizations.
Further Reading
Team Lead, Software Engineering of FES SWE at Sisense
0 comments