Build a Custom Widget in SAP Analytics Cloud, Anal...
Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
In this tutorial, we will build a simple custom widget in SAP Analytics Cloud, Analytics Application to show the gauge chart as shown below.
A custom widgets are implemented as Web Components. The Web Components are made of HTML, CSS and JavaScript.
Custom Widgets
Custom widget consists of the following files:
Custom Widget JSON
The custom widget JSON file specifies the custom widget properties like id, version, name, description and so on.
Web Component JavaScript A custom widget is composed of one or more Web Components.
Each Web Component is implemented in a Web Component JavaScript file, which defines and registers a custom element and implements the custom element’s JavaScript API. It has the lifecyles: constructor(), onCustomWidgetBeforeUpdate(), onCustomWidgetAfterUpdate(), connectedCallback().
Web Component JavaScript of Styling Panel (optional) The Styling Panel of a custom widget is an area in analytics designer where you can set property values of the custom widget at design time. It is implemented as a Web Component.
Web Component JavaScript of Builder Panel (optional) The Builder Panel of a custom widget is an area in analytics designer where you can set property values of the custom widget at design time. It is implemented as a Web Component.
Icon file
Any icon file image in 16x16 pixels.
Prerequisites
You need a web server that hosts the resources of the custom widget files (JavaScript files, CSS files, images, and so on). Assume that your web server address is :
The Gauge Box custom widget consist of three Web Components: the actual Gauge Box, the Styling Panel and the Builder Panel of the Gauge Box and it consist the following files:
box.js
Web Component JavaScript file of the Gauge Box.
box_sps.js
Web Component JavaScript file of the Styling Panel of the Gauge Box.
box_bps.js Web Component JavaScript file of the Builder Panel of the Gauge Box.
icon.png
Icon of the Gauge Box in any 16x16 pixel icon.
1. Custom Widget JSON of Gauge Box (box.json)
The Gauge Box custom widget has the unique ID, version, and the name, which is displayed in the analytics designer, Styling Panel.
The Gauge Box custom widget is composed of the following three Web Components:
The first Web Component is the actual Gauge Box as indicated by the kind of "main". The second Web Component is the Styling Panel of the Gauge Box as indicated by the kind of "styling". The third Web Component is the Builder Panel of the Gauge Box as indicated by the kind of "builder".
Moving on, these are the properties of the Gauge Box custom widget: value, info, color, width and height.
The property value represents the value in percentage of the Gauge Box. The property info represents the title of the Gauge Box. The property color represents the color of the Gauge Box. And the properties width and height represent the initial width and height of the custom widget.
And then the script methods of the Gauge Box are defined:
The function setValue takes three parameters, newValue, newInfo and newColor. The body property contains the script code which sets the passed all the parameters to the respective Gauge Box's properties.
Function getValue takes no parameter and returns the percentage value of the Gauge Box.
Finally, an onClick event is defined:
Note that the event has no parameters.
2. Web Components JavaScript (box.js)
This section shows the Web Component JavaScript of the Gauge Box (box.js).
The following code creates a template HTML element:
The template element represents the gauge chart.
2.2 JavaSript API of the Custom Element
Constructor The first function in the JavaScript API is the constructor.
The super() function is called, then the shadow DOM root element is created. The copy of the template element is added as a child element to the shadow DOM root element. An element style and svg is selected by using querySelector where shadowRoot is a reference to the document fragment. Finally, an event listener is attached to the custom element, listening for click events. Lastly, to make managing the properties of the Web Component easier, an empty _props object is initialized.
Handling Custom Widget Updates
In the onCustomWidgetBeforeUpdate() function, the properties in the changedProperties are merged with the properties of the _props object. The _props contains the state of all Gauge Box properties before the Gauge Box is updated.
In the onCustomWidgetAfterUpdate() function, the properties in the passed changedProperties object is used to directly set the gauge value, info(text information) and color of Gauge Box.
And finally call the render() function to update the chart.
3. Web Components JavaScript of the Styling Panel (box_sps.js)
The Styling Panel lets you change the background color of the Gauge chart in analytics designer.
This following code shows the Web Component JavaScript of the Styling Panel (box_sps.js).
The Web Component JavaScript defines a new custom element com-demo-box-sps. The JavaScript API of the new custom element is implemented in the BoxSps class which extends the JavaScript API of the HTMLElement class.
3.1 Template Object
The following code creates a template HTML element:
This template HTML element is a template for the shadow DOM HTML element that represents the HTML DOM of the Styling Panel of the Gauge Box.
3.2 JavaSript API of the Custom Element
Constructor The first function in the JavaScript API is the constructor.
The super() function is called, then the shadow DOM root element is created. The copy of the template element is added as a child element to the shadow DOM root element.
Finally, an event listener is attached to form, listening for submit events. If one such event occurs, the event handler function _submit() is called. Calling bind() and passing this to _submit() ensures that in _submit() the keyword this references the custom element.The submit() function is implemented as follows:
The _submit() function calls function preventDefault() on the passed event object, which prevents submitting the form to the server.
Then, a custom event propertiesChanged is created, indicates a change of properties to the Custom Widget SDK framework. This custom event contains a JSON payload which is the color property of the custom widget.
Getters and Setters Property The following code shows the implementation of color setter and getter functions.
The color setter function places a text representation of the new color into the input field of the Gauge Box’s Styling Panel.
The color getter function returns the text of the input field (color value) of the Gauge Box’s Styling Panel.
4. Web Components JavaScript of the Builder Panel (box_bps.js)
This Builder Panel lets you change the text color of the Gauge Box in analytics designer.
The code is very similar to the Web Components JavaScript of the Styling Panel. The following code shows the Web Component JavaScript of the Builder Panel (box_bps.js).
The Web Component JavaScript defines a new custom element com-demo-box-bps. The JavaScript API of the new custom element is implemented in the BoxSps class which extends the JavaScript API of the HTMLElement class.
4.1 Template Object
The following code creates a template HTML element:
This template HTML element is a template for the shadow DOM HTML element that represents the HTML DOM of the Builder Panel of the Gauge Box.
4.2 JavaSript API of the Custom Element
Constructor The first function in the JavaScript API is the constructor.
The super() function is called, then the shadow DOM root element is created. The copy of the template element is added as a child element to the shadow DOM root element.
Finally, an event listener is attached to form, listening for submit events. If one such event occurs, the event handler function _submit() is called. Calling bind() and passing this to _submit() ensures that in _submit() the keyword this references the custom element.The submit() function is implemented as follows:
The _submit() function calls function preventDefault() on the passed event object, which prevents submitting the form to the server.
Then, a custom event propertiesChanged is created, indicates a change of properties to the Custom Widget SDK framework. This custom event contains a JSON payload which is the color property of the custom widget.
Getters and Setters Property The following code shows the implementation of color setter and getter functions.
The color setter function places a text representation of the new color into the input field of the Gauge Box’s Builder Panel.
The color getter function returns the text of the input field (color value) of the Gauge Box’s Builder Panel.
Construct All Files Together
Once we have created all the required files, we need to organize in the following structure:
Create a folder called customwidgets and put box.json into that folder.
Create another folder box inside customwidgets folder and put the rest of the files inside box folder.
Upload all files to the web server.
Adding the Custom Widget to Analytics Designer
In the Analytics Designer, navigate to Main Menu > Browse > Custom Widgets. If you don't see this option, check your security roles.
Click the Create toolbar icon.
In the Upload File dialog, click the Select File button.
Select the custom widget JSON file box.json.
Create a new analytic application. The custom widget is listed in the widget list of the dropdown menu of the Add menu.
Using the Custom Widget in Analytics Designer
Once you have added the Gauge custom widget, you can see the following functions.
The below code illustrates who to define the parameters for the setValue function. And how to call the getValue function.
A quick demo video to control the SAP Analytics Cloud Custom Widget with web Bluetooth and micro:bit. User presses the button A and B on micro:bit to fill in and out the cylinder gauge.
Control two Google Gauges widgets with two micro:bits and Web Bluetooth: