Build your own custom widgets with the HTML Canvas
Ubidots offers off-the-shelf widgets to cover most visualization needs. However, some projects may require more specific and custom widgets. That's why we offer the HTML Canvas widget, which supports your own code to deliver custom visualizations.
HTML Canvas is a widget that enables the creation of custom widgets. It accomplishes this by letting the user write their own custom code using the following languages (as well as some Ubidots' built-in methods and third-party libraries which will be described later):
HTML
CSS
JavaScript
This widget features a code editor composed of three tabs, one for each language.
Whenever the other available widgets don't meet a specific requirement in terms of functionality and/or style.
Go to a dashboard, click the Add new widget button (+), scroll down to the Advanced section and select HTML Canvas.
The widget offers the following settings:
Code editor
3rd party libraries
Enable lazy loading
Preload Dashboard data
The appearence of the widget can be further customized with these options:
Name
Custom Style
Check the Custom Style documentation here in order to know how to customize your HTML Canvas' styles.
The HTML Canvas widget supports importing third-party libraries from CDNs. By specifying the CDN URL, you can include third-party libraries written in Vanilla JavaScript or React components.
To import React libraries into the HTML Canvas widget, the CDN must provide the library in UMD (Universal Module Definition) format. You can easily identify UMD CDN links by looking for umd
in the URL path or name, often labeled as umd
on sources like unpkg or jsDelivr.
The following example depicts how to import the jQuery library to be used by the widget.
Third party libraries are imported by clicking the 3rd party libraries option and pasting the library's URL there, as shown below:
This feature makes the Dashboard data available for the user at load time.
This consist of a togglable button that, when activated, changes the Widget logic so that the Dashboard data gets preloaded, making it available for the user along with the dashboard loading.
Before this feature was developed, the user could only retrieve data from the Ubidots class by setting an event and capturing the required variable on the callback.
For example, If the user wanted to get his Ubidots Token and the ID of the currently selected device, it could be achieved as follows:
However, this requires that the user knows some advanced Javascript knowledge as well as the custom Ubidots events.
With the Preload dashboard data feature, the requirement above can be achieved as easy as:
Note that there is no need to instantiate the Ubidots class, since it already has.
By enabling this feature, the ready
event as shown above won't be available anymore to be invoked.
Upon disabling the preload dashboard data feature, some of the class' properties might appear as undefined
this is due to the fact that, the class' instance has not yet fully received the values for its properties. Consider the code snipet below:
Albeit ubidots.token
is a valid syntax, and indeed the Ubidots
class has that property, the first console.log prints undefined
however the other two properly print the token, this is because such property is only available after the event has set its value.
The HTML Canvas built-in library is the backbone that allows interacting with dashboard data and the Ubidots API via:
Accessing the class' properties to modify/retrieve dashboards settings.
Accessing the class methods to perform actions on the dashboard or retrieving information from it.
Using the on
event handler registration method for executing logic upon events that occur in the dashboard, a.k.a Listening events.
Directly interacting with the API via Ubidots' Javascript SDK
This page describes the HTML Canvas class properties.
dashboardDateRange
Object
Dashboard's time frame.
dashboardObject
Object
Dashboard's information.
deviceObject [DEPRECATED]
Object
Device object of the currently selected device in a dynamic dashboard.
selectedDevice [DEPRECATED]
string
ID of the currently selected device in a dynamic dashboard.
selectedDevices
string array
String array of Id's of the selected devices in a dynamic dashboard
selectedDeviceObjects
Object array
Device objects array of the selected devices in a dynamic dashboard
selectedFilters
Array array object
Array containing as many arrays as filters configured in the dashboard. Each nested array contains the filters objects for that particular filter.
token
string
Account's default token
realTime
bool
Status of the real time in the dashboard
dashboardDateRange
dashboardObject
selectedDevices
selectedDeviceObjects
selectedFilters
This page describes the available methods within the HTML Canvas class
getHeaders
Returns the available Authentication Headers accordingly if the dashboard is public or not.
openDrawer
Embed a url's content in a drawer and displays it.
setDashboardDevice
Changes the currently selected device in a dynamic dashboard.
setDashboardDateRange
Changes the selected time range in a dashboard.
setRealTime
Turn on or off the dashboard's real time update.
refreshDashboard
Refreshes the dashboard data in all widget.
getHeaders
Arguments: None
Signature: ubidots.getHeaders()
Return:
openDrawer
Arguments: options
: Object
[mandatory] => An object containing the content's url and the drawer's width in px:
Signature:
Return: None
setDashboardDevice
Arguments: deviceId
: string
[Mandatory] =>The ID of the device that you want to set.
Signature: ubidots.setDashboardDevice(<deviceId>)
Return: None
setDashboardDateRange
Arguments: timeframe
: Object
[Mandatory] => An object containing the start and end timestamps:
Copy
Signature: ubidots.setDashboardDateRange(<timeframe>)
Return: None
setRealTime
Arguments: rt
: Bool
[Mandatory] => A boolean indicating the real time update setting
Signature: ubidots.setRealTime(<rt>)
Return: None
refreshDashboard
Arguments: None
Signature: ubidots.refreshDashboard()
Return: None
setFullScreen
Arguments: screenSetting
: string(toggle | enable | disabled)
[Mandatory] => A string representing the full screen setting.
Signature: ubidots.setFullScreen(<screenSetting>)
Return: None
Access dashboard settings via event handlers
A event is an action that takes place in a dashboard at load time or triggered by other specific actions.
You can register an event handler for any of the events above in the following way:
or
You can register event handlers for the following events:
receivedToken
At dashboard load time, when the token information is being retrieved.
selectedDashboardDateRange
Whenever the dashboard's date range is changed via GUI or a calling to the corresponding method.
isRealTimeActive
Whenever the dashboard's real time setting is changed via GUI or a calling to the corresponding method
dashboardRefreshed
When a user clicks the refresh button in the dashboard.
selectedDevices
selectedDeviceObjects
selectedDevice [DEPRECATED]
selectedDeviceObject [DEPRECATED]
When the selected device in a dynamic dashboard is changed via GUI or a calling to the corresponding method.
selectedDashboardObject
At dashboard load time, when the dashboard information is being retrieved.
selectedFilters
After changing the dashboard's filters configuration
ready
At dashboard load time, after all other events have been triggered.
receivedToken
Handler argument: token
: string
⇒ The user's default token.
Signature
Important note: The retrieved token inherits the users pemissions, either if it's an account admin, in which case it will have all permissions over the account resources accesible through the API, or an End-user, where access will be limited to its configured permissions.
selectedDashboardDateRange
Handler argument: timeframe
: Object
⇒ The dashboard's timeframe.
Signature
isRealTimeActive
Handler argument: rt
: Bool
⇒ The dashboard's real time update setting.
Signature
dashboardRefreshed
Handler argument: None
Signature
selectedDevices
Handler argument: deviceIds
: [string]
⇒ String array of the id's of the currently selected devices in a dynamic dashboard
Signature
selectedDeviceObjects
Handler argument: deviceObjects
: [Objects]
⇒ Object array of the device objects of the currently selected devices in a dynamic dashboard
Signature
selectedDashboardObject
Handler argument: dashboardObject
: Object
⇒ dashboard object
Signature
selectedFilters
Handler argument: dashboardFilters
: [[Object]]
⇒ Array of arrays containing the filter objects. Each nested array corresponds to a filter configured on the dashboard.
Signature
ready
Handler argument: None
Signature
Each of the events mentioned above is related to a property within the Ubidots class that can be used to retrieve exactly the same data as an alternative to setting an event handler and waiting for the event:
dashboardDateRange
selectedDashboardDateRange
dashboardObject
selectedDashboardObject
deviceObject
selectedDeviceObject
selectedDevice
selectedDevice
selectedDeviceObjects
selectedDeviceObjects
selectedDevices
selectedDevices
token
receivedToken
realTime
isRealTimeActive
For example, you can configure an event to retrieve data from the selected device each time the dashboard's device picker is used, as shown below:
This way, everytime that the device picker is used, the logic within the event will be executed. As an alternative to this, if you don't require to perform an action upon the event triggering but only require the data of the currently selected device, you can just access the property related to the event as:
Access Ubidots Javascript SDK from the HTML canvas
The HTML Canvas' built-in library exposes Ubidots' Javascript SDK through ubidots.api
. This simplifies interacting with Ubidots API when developing in the HTML Canvas, hence avoiding making request by hand, or writing wrappers to generalize request making.
To access the SDK methods, follow the below syntax:
NO dashboard data preloaded:
This example creates a text box to set a variable's value.
HTML:
CSS:
JavaScript:
In the following code, the variable VARIABLE_ID must be set accordingly.
The next example shows how to get data from Ubidots and display it in a Highcharts chart:
HTML:
JavaScript:
In the following code, the variable VARIABLE_ID must be set accordingly.
Third party libraries:
This example is exactly the same as the one before, but it uses Plotly as a charting library instead of Highcharts:
HTML:
JavaScript:
Third party libraries:
This example uses the HTML Canvas widget to turn on/off a dashboard's "real time":
HTML:
CSS:
JavaScript:
This example shows how to create an LCD screen on a dashboard by using the HTML Canvas widget.
In the following code, the VAR_ID variable must be set according to the variable that should be displayed:
Once the settings are saved, it will look like this:
Modify the HTML to:
Modify the CSS code to:
Modify the JavaScript code to:
Lastly, add another 3rd party library needed for this implementation:
This is the result:
The following example uses Ubidots' "listener events" on an HTML Canvas in order to extract information from Ubidots and display it on the dashboard:
The following example uses "setter events" and a 3rd party library to set the device in a dynamic dashboard:
HTML:
CSS:
JavaScript:
Third party library:
The following example shows how to dynamically change a widget's header style upon clicking a button by using the HTML Canvas widget.
Create a button element using HTML. Paste the following code on the HTML code editor tab:
Add the logic to change the header's style upon clicking the button:
Save the widget's settings by clicking on the green check mark. The result is shown below:
Using the socket.IO library, create a simple real-time widget with Ubidots and the HTML Canvas.
Socket.IO is a JS library easy to implement that manages socket connections and messages, thus enabling a bi-directional real-time connection protocol. Ubidots has implemented this real-time protocol within its core servers to easily update devices, to retrieve and store data inside any JS variable, and without the need to implement any requests, using sockets.
The following shows how to create a HTML Canvas Widget which will display a variables' data in a real time fashion. This example relies on the previous Create an LCD screen with the HTML Canvas, so please head to said section first.
Create the HTML element in which the data will be displayed:
Add the logic of the widget by using the following JavaScript code:
The following 3rd party libraries will be needed:
The following is the result:
The following shows how to implement HTML Canvas Widget to delete variable data in a time window in Dynamic Dashboards
Create the structure. Paste the following in the HTML Code Editor Tab:
Paste the following in the CSS Code Editor Tab:
Paste the following in the Javascript Code Editor Tab:
The result is a Widget as shown below:
The user can use the dropdown menu to select a variable from the current device in the Dynamic Dashboard, then select a time windows using the date pickers. Upon clicking the Delete Values button, the data for that device's variable will in the time window selected will be deleted. Note that since Ubidots schedules asynchronous task, you might need to wait a few minutes to see the values deleted.
The following example shows HTML Canvas Widget implemented such that you can either, select or type a variable label, select a time window and then delete said data
Ubidots supports two types of grouping Devices: Device Types and Device Groups.
Create the structure. Paste the following in the HTML Code Editor Tab:
This will create the datetime selector, either the text-box or the drop-down menu to select the variable and the submit button.
Set the logic. Paste the following in the Javascript Tab:
This implementation will display a different UI depending if the current Dashboard is set by Device Type or Device Group. In the first case, the variables common to the Device Type will be displayed in a drop-down menu. In the later, a text-box will be displayed for the user to type-in the label of the variable that wants to delete. The following GIF shows the Widget being used in both categories of dashboards:
This example uses HTML Canvas newest feature "Preload dashboard data" to easily implement navigation through dashboards.
A drop-down menu will display all of the Dashboards present in the Ubidots account, upon selecting one, said dashboard will be opened in a new browser tab.
HTML:
Javascript:
The following is the resulting Widget:
This example showcases how to import and use MaterialUI.
Add the library's CDN to the Widget's 3rd party libraries:
Note that in the path, the umd
tells that this CDN is exposed through UMD format.
Now, in the code editor's HTML tab, put the following:
Change to the Javascript (JSX) tab and paste the following:
After saving the changes, the widget will be rendered as:
The code editor has 3 tabs:
HTML
CSS
JavaScript
Each of these tabs can be used to write code using the corresponding language.
All 3 of the previous mentioned languages can even be used within the same file, like this:
However, as best practices dictate, it is advisable to separate the design in 3 different files; one for each of the 3 different languages. This is the reason why the widget's code editor has a dedicated tab for each language.
HTML (Hyper Text Markup Language) is the standard markup language with which any web page is structured. By using HTML elements, the user can tell the browser where and how to display content.
<title> Page Title </title>
: This element tells the browser to display the "Page Title" message as the tittle for the web page.
<h1> This is a h1 header </h1>
: This element tells the browser to display the "This is a h1 header" message as an H1 header wherever it is placed in the HTML file.
<p> My first paragraph </p>
: This element tells the browser to display the "My first paragraph" message as a paragraph wherever it is placed in the HTML file.
In every HTML file the following structure is mandatory:
When using HTML elements on the HTML Canvas widget, using the following tags is optional:
<html></html>
<head></head>
<body></body>
This means that all the code that is placed in the HTML code editor tab will be interpreted as if it were inside an HTML body element on the widget:
<html>
<head>
</head>
<body>
<h1> This is a h1 header </h1>
<p> This is a paragraph </p>
<p> This is another paragraph </p>
</body>
</html>
<h1> This is a h1 header </h1>
<p> This is a paragraph </p>
<p> This is another paragraph </p>
Suppose the following web content has to be displayed:
This can be achieved with the following HTML code:
In order to achieve this same result on the HTML Canvas, the following code can be used on the HTML tab of the code editor:
The resulting HTML Canvas Widget is shown below on a dasboard:
CSS (Cascading Style Sheets) is used to define styles that can be applied to HTML elements in order to customize the way in which they are displayed in the web browser.
body {
background-color: lightblue;
}
Sets the style for the HTML body to have a lightblue background color
h1 {
color: white;
text-align: center;
}
Sets the style for the h1 headers to have their color white as well as to center the text
p {
font-family: verdana;
font-size: 20px;
}
Set the style for the paragraphs to have their font family set to verdana and the font-size as 20px
The following HTML + CSS file modifies the body, H1 header, and paragraph elements' style.
It Results in the following content being displayed in the browser:
To achieve this on the HTML Canvas, the following can be done:
Place the following code on the HTML code editor tab:
Place the following code on the CSS tab:
This is the resulting HTML Canvas Widget shown on a Dashboard:
JavaScript is a scripting language that allows the user to interact with the elements of a web page, define logic for web content, and dynamically modify/update the contents of the web page.
const Paragraph = document.querySelector("p");
Selects a text paragraph and stores an instance of it on the Paragraph variable
function updateName()
{
const name = prompt("Enter a new name");
para.textContent = Player 1:
${name};
}
Defines a function block that can be invoked by calling updateName()
Paragraph.addEventListener("click", updateName);
Attaches an event listener to the Paragraph instance such that when the paragraph is clicked, the updateName function gets executed
const para = document.getElementById("player");
Gets a reference to the element with ID "player" from the DOM
Let's assume the following requirement:
Creating a paragraph element that displays the message "PLAYER 1: name", where name is a value set by the user upon clicking on the paragraph element.
This can be accomplished using the following HTML + CSS + JavaScript recipe:
The same can be reproduced on Ubidots by using the HTML Canvas Widget in the following way:
Use the following HTML code on the widget's HTML code editor tab:
Now, add the style to the paragraph on the widget's CSS tab:
Lastly, add the following code to the widget's JavaScript tab:
The following is the result on a Ubidots Dashboard: