Requirements
Ubidots account: Professional plan or above.
1. Introduction
The HTML canvas is a powerful widget that allows Ubidots developers to create specialized visualizations using Ubidots API REST. Users can interact with their account data and listen to the events made on the dashboard to make the widget respond as a default Ubidots widget would.
How does the HTML Canvas widget work?
The HTML Canvas widget works with the same HTML/CSS/JS you would code when creating a simple website. It also considers the case when you may need to use a 3rd library (e.g. jQuery), so you can add these by inserting an URL containing the library.
Using this guide and the HTML Canvas Widget you will be able to:
Securely retrieve your account's Token, Dashboard and Device information.
Listen to navigation bar buttons on the Dashboard side.
IMPORTANT NOTE: This article is intended for developers with foundational knowledge of HTML, JS and CSS.
2. Creating and HTML Canvas
Step 1:Click on the top-right "+" button. Drawer menu with widgets options opens.
Step 2:Click on HTML canvas. Drawer menu displays setup options.
Step 3: Name your widget.
Step 4: Click on "Open editor" button.
Step 5: Enter your HTML, CSS and JS code in the respective tab.
Step 6: Add libraries by entering its URL in the "3rd party libraries section".
Step 7: Save the changes.
3. Methods and available properties
Ubidots support the below methods to be used by developers inside the widget:
Listening events:
Event | Description | Result |
| Default account token |
|
| Actual selected date range in the dynamic dashboard (timestamp in milliseconds). |
|
| This event is triggered every time the Real Time button in the Dashboard is clicked. |
|
| This event is activated when a user clicks the refresh button in the dashboard. |
|
| Returns a list containing the ids of the selected devices in a dynamic dashboard |
|
| Returns a list containing the device objects of the selected devices in a dynamic dashboard |
|
| [DEPRECATED] Use
Returns the id of the currently selected device in a dynamic dashboard. |
|
| [DEPRECATED] Use
Returns the device object of the currently selected device in a dynamic dashboard. |
|
| Return the whole object of a Dashboard as received when loading the dashboard |
|
| Returns an object with the information of all the other events. See example. |
|
Setter events:
Event | Description | Arguments |
|
|
|
|
|
|
|
|
|
| Ability to refresh the dashboard data in all widget. |
|
|
Enable, Disable, or Toggle the Dashboard into Fullscreen |
|
4. Examples
Interacting with dashboard data
The following example uses Ubidots' "listener events" on an HTML Canvas in order to extract information from Ubidots and display it on the dashboard:
HTML
<div>
<p id="token"></p>
<p id="dashboard"></p>
<p id="dateRange"></p>
<p id="deviceSelected"></p>
<p id="device"></p>
<p id="isRealTimeActive"></p>
<p id="dashboardRefreshed"></p>
<p id="eventToken"></p>
<p id="eventDashboard"></p>
<p id="dashboardDateRange"></p>
<p id="eventDeviceSelected"></p>
<p id="eventDevice"></p>
<p id="eventRefresh"></p>
<p id="eventisRealTimeActive"></p>
</div>
JavaScript
var ubidots = new Ubidots();
ubidots.on('receivedToken', function (data) {
document.getElementById('eventToken').innerText = "TOKEN: " + data;
});
ubidots.on('selectedDashboardObject', function (data) {
document.getElementById('eventDashboard').innerText = "Dashboard Object: " + JSON.stringify(data)
});
ubidots.on('selectedDashboardDateRange', function (data) {
document.getElementById('dashboardDateRange').innerText = "Dashboard date range: " + JSON.stringify(data);
});
ubidots.on('selectedDevices', function(data) {
let listOfIds = data.join(', ');
document.getElementById('eventDeviceSelected').innerText = "Selected Device ids: " + listOfIds;
});
ubidots.on('selectedDeviceObject', function (data) {
let formattedData = JSON.stringify(data); // 2-space indentation
document.getElementById('eventDevice').innerText = "Device Object: \n" + formattedData;
});
ubidots.on('isRealTimeActive', function (data) {
document.getElementById('eventisRealTimeActive').innerText = "Real time button: " + JSON.stringify(data);
});
ubidots.on('dashboardRefreshed', function (data) {
document.getElementById('eventRefresh').innerText = "The Dashboard refresh button was pressed."
});
IMPORTANT NOTE: There is no need to add any external library to use these methods and properties, Ubidots adds libraries to do this by default once you've created the HTML Canvas widget.
Set a device in a Dynamic Dashboard
HTML
<div class="set-device--container">
<p>Device set successfully</p>
<input type="text" placeholder="Enter Device id" class="set-device--input" id="device" />
<button type="button" class="set-device--button" id="send-value">Set device</button>
</div>
CSS
.set-device--container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.set-device--input {
display: block;
margin: 0 auto;
border: none;
border-bottom: 2px solid #28AECD;
font-size: 14px;
outline: none;
}
.set-device--button {
background: #FFFFFF;
border: 2px solid #28AECD;
border-radius: 20px;
color: #28AECD;
font-size: 14px;
margin-top: 10px;
width: 100%;
}
.set-device--button:hover {
background: #28AECD;
color: #FFFFFF;
cursor: pointer;
}
.notification--container {
display: none;
}
JS
var $setDevice = $('#send-value');
var $notification = $('#notification');
var ubidots = new Ubidots();
$setDevice.on('click', function () {
var $device = $('#device'); // Ensuring declaration of the variable
ubidots.setDashboardDevice($device.val());
$notification.show();
});
3rd party library
https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
Turn on/off Dashboard Real Time
HTML
<div class="notification--container" id="notification">
<p>Dashboard Real Time</p>
</div>
<div class="real-time--container">
<button type="button" class="real-time--button" id="real-time">
Enable real time
</button>
</div>
CSS
.real-time--container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.real-time--button {
background: #FFFFFF;
border: 2px solid #28AECD;
border-radius: 20px;
color: #28AECD;
font-size: 20px;
margin-top: 10px;
width: 100%;
}
.real-time--button:hover {
background: #28AECD;
color: #FFFFFF;
cursor: pointer;
}
.notification--container {
display: none;
}
JS
var $realTime = $('#real-time');
var $notification = $('#notification');
var ubidots = new Ubidots();
$realTime.on('click', function () {
ubidots.setRealTime(true)
$notification.show();
});
3rd party library
https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js
Activate Dashboard Refresh button from your HTML Canvas
HTML
<div class="notification--container" id="notification">
<p>Dashboard successfully refreshed</p>
</div>
<div class="refresh-dashboard--container">
<button type="button" class="refresh-dashboard--button" id="refresh-dashboard">
Refresh Dashboard
</button>
</div>
CSS
.refresh-dashboard--container {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
.refresh-dashboard--button {
background: #FFFFFF;
border: 2px solid #28AECD;
border-radius: 20px;
color: #28AECD;
font-size: 14px;
margin-top: 10px;
width: 100%;
}
.refresh-dashboard--button:hover {
background: #28AECD;
color: #FFFFFF;
cursor: pointer;
}
.notification--container {
display: none;
}
JS
var $refreshDashboard = $('#refresh-dashboard');
var $notification = $('#notification');
var ubidots = new Ubidots();
$refreshDashboard.on('click', function () {
ubidots.refreshDashboard()
$notification.show();
});
3rd party library
https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js