If you have read some of my articles, you might have noticed that I’m a big fan of SAS Visual Analytics object called Data-Driven Content, or DDC for short. This is because you can use it for a variety of things, such as extend SAS Visual Analytics ability to visualize data, execute complex calculations, load data in memory on demand, etc. The nature of DDC, which leverages Web technology such as HTML, CSS, and JavaScript, associated with the ability to exchange information with SAS Visual Analytics and its objects is what allows this great flexibility. This article series will focus on a couple of things that I often get asked for: embed Web pages and images that dynamically change to react to actions performed in the SAS Visual Analytics report.
If you are new to DDC, I recommend checking Data-Driven Content: leveraging third-party visualizations in SAS Visual Analytics before continuing. Also, if you want more advanced information about usage of DDC for more complex needs, this series might help as well.
What we are discussing here is not that complex at all. Actually, the solution is pretty simple. Let’s start with dynamic Web pages. For this, we need a Web page that takes parameters as a search key for example, and something like Wikipedia works just fine.
The Wikipedia URL with a search key in English takes this format:
https://en.wikipedia.org/wiki/<search_key>
In fact, you can choose the language currently being used on the search key and the resulting page by replacing the en portion of the URL with the language of your preference, which gives you some extra flexibility.
You may be asking yourself why we don’t use the SAS Visual Analytics object called Web Content, which allows you to display a Web page inside a report. The answer is simple: you cannot dynamically pass parameters to URLs used in Web Content objects and this is why the DDC object comes in handy.
The data used in this example consists of a table with some animal names and their corresponding image file name. The image file name is not necessary for this Web page example, but it is used in the next article, so just ignore it for now:
Animal |
Image |
Bear |
icon-bear.png |
Cat |
icon-cat.png |
Chicken |
icon-chicken.png |
Cow |
icon-cow.png |
Deer |
icon-deer.png |
Dog |
icon-dog.png |
Duck |
icon-duck.png |
Elephant |
icon-elephant.png |
Frog |
icon-frog.png |
Giraffe |
icon-giraffe.png |
Goat |
icon-goat.png |
Horse |
icon-horse.png |
Koala |
icon-koala.png |
Lion |
icon-lion.png |
Monkey |
icon-monkey.png |
Mouse |
icon-mouse.png |
Panda |
icon-panda.png |
Pig |
icon-pig.png |
Rhino |
icon-rhino.png |
The idea is to select an animal name from the SAS Visual Analytics report and pass it to the DDC, which in turn will pass it as a parameter to the Wikipedia URL. The resulting Web page should be displayed embedded in the report.
There are a few different ways you can do that. For example, you can use a filter action having a visualization object (bar chart, list table, pie chart, etc.) or a control object (dropdown, list control, button bar, etc.) as the source of the action, where the animal is selected, and the DDC object as the target. Or you can use a control object to set a parameter in the report and pass that parameter containing the selected animal to the DDC object.
Those examples are available in GitHub, under samples/DynamicWebPagesAndImages. There you will find the following:
Let’s explore each one of those examples next.
Using Filter Action to Pass Search Key to the Web Page
In this example you will need two objects in SAS Visual Analytics:
Settings:
By assigning the column “animal” to the DDC data role and defining the filter action, when the animal is selected in the source object, the data passed to the DDC object is filtered and the selected animal is sent to the DDC implementation code in the data portion of the JSON message. For example, if Bear is selected from the list, this is the message sent to the DDC implementation:
The HTML portion of the DDC implementation is also simple. You only need an iframe where the source is the parameterized Wikipedia URL. In this example, you are surrounding the iframe with a div tag just for clarity, but it’s not really necessary:
<div>
<iframe id="target_url" src=""></iframe>
</div>
Observe that the src attribute is initially empty. It will be dynamically modified via JavaScript every time the DDC receives a new message from VA. The JavaScript portion is also very straight forward:
va.messagingUtil.setOnDataReceivedCallback(updateURL);
function updateURL(vaMsgObj)
{
if (vaMsgObj && vaMsgObj.data && vaMsgObj.rowCount === 1) {
document.getElementById("target_url").src="https://en.wikipedia.org/wiki/"+vaMsgObj.data[0][0];
}
else {
document.getElementById("target_url").src="";
}
}
The first line leverages one of the utility functions setOnDataReceivedCallback provided in GitHub. This function sets the callback function to be executed every time a new message is received from VA. In this example, the callback function was called updateURL and the JSON message received from VA can be retrieved from the function’s argument vaMsgObj. Every time you interact with the report and change the content of the data assigned to the DDC, like selecting a different animal from the list, the updateURL function gets called. This function performs some basic validations on the JSON message, such as checking if there is one and only one animal selected. If it passes the validation, the animal name is appended to the Wikipedia search URL and that value becomes the source of the iframe, otherwise the source is empty, and nothing is displayed.
You can make small design changes, such as display the main Wikipedia page by default when more than one animal is selected, or a custom page featuring a message, etc., but this is the base template that you need. The overall HTML code is a bit more than that, as it takes care or styling the output via CSS and includes the GitHub utilities library:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://<your.web.server.host>/github/util/messagingUtil.js"></script>
<style>
html, body {
margin: 0px;
padding: 0px;
height: 100%;
overflow: hidden;
}
iframe {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 100%;
width: 100%;
border: none;
}
</style>
</head>
<body>
<div>
<iframe id="target_url" src=""></iframe>
</div>
<script>
"use strict";
va.messagingUtil.setOnDataReceivedCallback(updateURL);
function updateURL(vaMsgObj)
{
if (vaMsgObj && vaMsgObj.data && vaMsgObj.rowCount === 1) {
document.getElementById("target_url").src="https://en.wikipedia.org/wiki/"+vaMsgObj.data[0][0];
}
else {
document.getElementById("target_url").src="";
}
}
</script>
</body>
</html>
Once you have the HTML file deployed in your Web Server, just add its URL in the Options pane of the DDC object. This HTML file is called ddc_ParameterizedWikipediaURL.html in GitHub.
Using VA Parameter to Pass Search Key to the Web Page
This implementation is very similar to the previous. You still need two objects in SAS Visual Analytics like before and a parameter, but because you are using a parameter, the source must be a control object:
Settings:
This is an example of a JSON message sent to the DDC when Chicken is selected:
The DDC HTML implementation is very similar to the previous one. You only need to include an additional utility library and modify the callback function to read and validate the parameter, instead of the information passed in the data portion of the JSON message:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://<your.web.server.host>/github/util/messagingUtil.js"></script>
<script type="text/javascript" src="http://<your.web.server.host>/github/util/contentUtil.js"></script>
<style>
html, body {
margin: 0px;
padding: 0px;
height: 100%;
overflow: hidden;
}
iframe {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 100%;
width: 100%;
border: none;
}
</style>
</head>
<body>
<div>
<iframe id="target_url" src=""></iframe>
</div>
<script>
"use strict";
va.messagingUtil.setOnDataReceivedCallback(updateURL);
function updateURL(vaMsgObj)
{
// Function getVAParameters returns the object: {<param_label_1>:<param_value_1>, ... , <param_label_n>:<param_value_n>}
// If <parameter_label> has multiple values, <param_value> is an array
var vaParameters = va.contentUtil.getVAParameters(vaMsgObj);
var searchKey = vaParameters._search_key; // it will return undefined if the specific parameter doesn't exist
if (searchKey && !Array.isArray(searchKey)) {
document.getElementById("target_url").src="https://en.wikipedia.org/wiki/"+searchKey;
}
else {
document.getElementById("target_url").src="";
}
}
</script>
</body>
</html>
This HTML file is called ddc_ParameterizedWikipediaURL_WithVAParameter.html in GitHub.
Using VA parameters may have some advantages specially if you have several multi-value parameters to be passed to the DDC.
Cheat Sheet
The table below summarizes the different report object settings for the selected information to be passed to DDC:
In the next article you will be creating a DDC to dynamically embed images in SAS Visual Analytics.
References
Learn More…
Github content is being updated. I'll let you know when it's ready.
GitHub content is ready!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.