In this tutorial, we will guide you through the process of creating a
Ring Gauge custom widget, which enables you to display multiple variables simultaneously in a interesting format.
To achieve this, we will leverage the
ECharts library, a robust and highly customizable charting library that simplifies the development of interactive and visually appealing data visualizations. By utilizing ECharts, we can significantly speed up the development process and ensure that our custom widget is both efficient and aesthetically pleasing.
This tutorial is structured into three comprehensive sections to facilitate a smooth learning experience:
1. Installing Dependencies and Setting Up Your Development Environment: We will walk you through the initial setup, including the installation of necessary dependencies and configuring your development environment to ensure you have everything you need to get started.
2. Key Components of Your Custom Widget: `widget.tsx` and `widget.view.tsx`: We'll explore the two primary files that form the core of your custom widget: widget.tsx and widget.view.tsx. These files are crucial in determining the functionality and appearance of your widget.
3. Deploying Your Custom Widget Code to TagoIO Files: Finally, we will cover the deployment process, showing you how to upload your custom widget code to TagoIO and make it available for use within your dashboards.
Prerequisites:
Before you begin, ensure you have the following:
- A TagoIO account: You need an active TagoIO account to access the dashboard and deploy custom widgets. Click here to Create your account.
- Basic knowledge of TagoIO: Familiarize yourself with TagoIO's platform and understand the basics of creating and managing dashboards. Click here to Access our Learning Center and check out our training modules.
- Node.js and npm: Before you can create custom widgets, ensure you have Node.js and npm (Node Package Manager) installed on your machine.
- React: Basic knowledge of React is necessary as you will be using it to create custom widgets.
Our goal with this tutorial is to provide you with a solid foundation that you can build upon. The Ring Gauge custom widget example is designed to be a starting point—a kickstarter—that you can download, modify, and adapt to meet your specific requirements. We encourage you to experiment and enhance the widget to suit your unique use cases.
So let's get started!
Installing Dependencies and Setting Up your Development Environment
In this section, we will guide you through the essential steps to set up your development environment for creating custom widgets with TagoIO. By the end of this section, you will have downloaded the project repository, installed necessary dependencies, and configured your environment to view and develop your custom widget effectively.
Download the Project Repository
First, you need to download the project repository from GitHub. This repository contains the template and initial setup for creating custom widgets using react, html, and css.
- Custom Widget Repository
To download the repository, you can either clone it using Git or download it as a ZIP file and extract it to your desired location.
Install Dependencies
Next, we need to install all the dependencies used in the project. These dependencies are essential for the project to function correctly.
Using NPM, run the following command to install all dependencies:
Libraries Used:
- TagoIO Custom-widget:
- This library provides the necessary tools and functions to create custom widgets for TagoIO dashboards. It simplifies the process of integrating with TagoIO's API and displaying data.
- Luxon:
- Luxon is a library for working with dates and times in JavaScript. It provides a modern and easy-to-use API for handling date and time operations. The @types/luxon package provides TypeScript type definitions for Luxon.
- Echarts:
- Echarts is a powerful and flexible charting library for creating interactive charts. It supports a wide range of chart types and is highly customizable, making it ideal for creating visually appealing data visualizations.
Port Forward to Allow TagoIO to Access your Code
To view your custom widget in the TagoIO dashboard during development, you need to set up port forwarding. This allows TagoIO to access your local development server.
- Launch Visual Studio Code and open your project folder.
- Click on "Terminal" in the upper left corner.
- Select "New Terminal".
- In the terminal, type npm start and press Enter.
- This command will start the local server hosting your custom widget.
- Copy Port Number: Look for the server running message in the terminal.
- Forward Port:
- Above the terminal window, click on "Ports".
- Click on "Forward Port".
- Enter the port number you noted in step 3.
- Change Port Visibility:
- Right-click on the newly created port forward.
- Select "Change Visibility" and set it to "Public".
- Copy the URL provided, as you will need to add it to the custom widget in your TagoIO dashboard.
- Access the Copied URL to allow external access to access the local server.
- This will allow your TagoIO dashboard to communicate with your project folder.
Once you have set up port forwarding, you can create the custom widget so that you can see what you're developing.
- Go to your TagoIO admin panel.
- Select a dashboard where you would like to add the custom widget.
- Create a new widget and in the URL & Parameters section, add your local development server URL.
- Click on "Configure Parameters" and add show_time as enabled. When this is enabled, it will display the last time the variable was received.
By following these steps, you will have your custom widget up and running in your TagoIO dashboard.
Now we can dive into what truly matters: the code! In this custom widget tutorial, our focus will be on two key files: `widget.tsx` and `widget.view.tsx`. While these aren't the only important files in our source folder, they will be our primary focus since they decide the key parts regarding how the custom widget works and what it does.
The `widget.view.tsx` file handles two main tasks. First, it manages the state of the custom widget and keeps this internal state in sync with TagoIO, both in the Admin and RUN environments. Second, it provides functions, known as callbacks, that the presentational component can use from the Custom Widget Library. The callbacks are used in order to tell the custom widget when to start working, display data, send data and more.
The file contains 4 important callbacks:
- window.TagoIO.ready();
- Allows the custom widget to start communications with TagoIO.
- window.TagoIO.onStart();
- Creates the widgets structure and gets it ready to receive data.
- window.TagoIO.onRealtime();
- Receives the device data to display on the widget.
- window.TagoIO.onSyncUserInformation();
- Receives the user data to format the device data accordingly.
- Finally, at the end of the useEffect function, it returns all the properties to the widget itself located within the `widget.tsx`
The `widget.tsx` file is in charge of showing the custom widget's interface. It uses the widget's configuration settings and data from variables and resources. Additionally, it employs handlers provided by the view component to interact with both the Custom Widget and TagoIO's APIs.
- Define Widget Properties
- Here you need define the properties that your widget will use. These properties include:
- data: The data fetched from the API.
- showTime: A parameter to determine if the time should be displayed.
- userSettings: User-specific settings such as date and time format.
- Create the Widget Component
- The main component of the widget is responsible for handling the rendering and logic. Here’s an overview of the tasks performed inside the component:
- Extract Data: Extract data, showTime, and userSettings from the properties.
- Reference for Chart: Use useRef to create a reference for the chart container.
- Memoize Data: Use useMemo to filter and memoize the data to be used in the chart.
- Effect Hook: Use useEffect to manage the chart's rendering logic. This includes:
- Formatting the data according to user settings.
- Initializing the chart and setting its options.
- Handling window resize events to adjust the chart size.
- Handle Loading State
- If the data is not available, display a "loading" message to inform the user. This will display a "loading" string on the upper left corner of the widget.
- Render the Widget
- Finally, render the widget with the gauge chart, ensuring it occupies the full available space.
Make sure to take a look at the snippets showing examples on how to send data via a custom widget, its available in commented form within the `widget.view.tsx`.
In this section, we will guide you through the steps of deploying your custom widget to TagoIO. Deployment is a key stage in your development process, ensuring that your finalized widget is accessible and functional within the TagoIO platform. By the end of this section, you will have created a dedicated folder for your custom widget, obtained your Profile ID, and modified the necessary configuration files. You will then run the build command to generate the built code, which will be placed in the designated folder, making your widget ready for deployment on TagoIO.
Before deploying your custom widget on TagoIO, you need to set up a dedicated folder in your TagoIO Files and retrieve your profile ID. Follow these steps to ensure everything is in place:
Create a Folder in TagoIO Files
- Go to your TagoIO Admin panel.
- Navigate to the TagoIO Files module.
- Create a new folder and save it for future reference..
Retrieve Your Profile ID
- Click on your account image in the top right corner.
- Select "My Account."
- Click on the profile you are using.
- Click on "More" to find your profile ID and save it for future reference.
Modify the package.json file for your use case
In this section, we will be returning to the project folder to make essential modifications to the package.json file. These changes are crucial for ensuring that your custom widget functions correctly once deployed. To proceed, you will need access to the root folder where your custom widget is stored, as well as the profile ID obtained from the previous step.
Modifying the package.json file
- Go to your project folder and open the package.json file.
- Locate the build line.
- Replace the profile ID with your own profile ID.
- Replace the folder name with your folder name.
Now we can finally build the code. The built code is what you will deploy to TagoIO using the files module. To build the code, simply type "npm run build" in your terminal.
Finally, you can copy the built code and paste it into the folder you created earlier. To do this, open the `dist` folder and copy the `.js`, `.css`, and `.html` files, ignoring any files that end with `.map`. Then, paste the copied files into the folder you created.
All that remains is to paste the new URL into the custom widget, just as you did in the section "Create the Custom Widget" in your Dashboard. With this step, you have successfully deployed the custom widget and no longer need to keep your local development server port forwarded.