Widget API

Live provides an API to register new charts implementations.

Standard API

Depends on Live 2.27.0

Widgets service

The widgetsService is available in live/services/widgets. It is javascript module that allows registering custom widget implementation to Live run-time. Every widget must have a unique string to represent its type. In this tutorial example we choose my-simple-chart as shown below:

// Widgets service usage:
import i18n from 'live/services/i18n'
import widgetsService from 'live/services/widgets'

// The widget implementation
import SimpleChart from './my-simple-chart'
const type = 'my-simple-chart'

// Examples on: "Registering the widget" sections
widgetService.register(...)

Creating the Widget types

First we need to create types. If you do not have all definitions in mind at first, don't worry, create some placeholder types and enrich them at a later point.

Creating the Data Reducer

To get from events a consolidated current state we offer the abstraction of a data reducer. It is as simple as a switch-case statement that will always return an updated state value from each message received.

On the reducer above we're getting fields fieldA and fieldB defined in ChartEvent to consolidate our state value on data events. All event types definitions.

Creating a pure Javascript Widget

Widget implementation

Create a simple class that implements ChartInterface

After this, let the IDE or Code editor help you:

Automatically implement ChartInterface

You should get something like this:

That is all on the chart implementation.

Registering the widget

Wrap your widget with the widgetWrapper decorator

You are all set. A new chart implementation has been added to Live and can now be chosen on the Pipes widget editor.

Complete example

Creating a React widget

Widget implementation

To create a React widget simply create a React component, like so:

React widget tip: split data and config components

By doing this you can restrict React updates to state and configs in different sub-trees.

eg: <ChartData data={state} /> and <ChartConfig config={widget.jsonConfig> width={width} height={height} />

Registering the widget

Wrap your widget with the reactWidgetWrapper decorator

You are all set. A new chart implementation has been added to Live and can now be chosen on the Pipes widget editor.

Complete example

Legacy API - deprecated

Live provides a factory function in live/widgets/helpersto proper setup the prototype to use a function constructor for the Widget instance creation.

Live is implemented using ReactJS, however the Live Widget API doesn't require widgets developers to write a React Component. The Widget implementation must provide the following methods:

Widget.setEl

setEl(element) is called right after the DOM element creation for the widget container. The elementparameter contains the divthat acts as container and the developer could save the elementreference at its instance for future usage. This method should return the current widget instance.

Widget.render

render() is called once at very first rendering of the Widget (be careful to avoid misunderstanding with render behavior of the React Components). This method is executed as a initialization phase at Widget Life-Cycle, it is commonly used to get the DOM ready for the events arrival. Besides the elementcontainer also receives a CSS class named using the -widget suffix over the widget type string, in our example: tutorial-widget. So the developer will be able to customize the container as necessary and append child elements as well, for example:

The `render` return types

Object with defined `options` property

If the render function returns a object with options it tells Live that the widget handles its own rendering process. Then you must handle by your own any dynamic change to your own properties. Of course, in event flow method (as we describe next) you can handle how to change anything in your widget since new events arrive.

The widgetFactory constructor assigns a default empty object to options. Keep that in mind if your render method returns this.

TheLiveWidgetImplOptions type is described in Live Widget Configuration section.

Function: considered to be React Component

If the render function returns a Function, the return value is considered to be a React Component. Keep in mind this function definition must not define options property. The component will be created like so:

Object with `portal` property: property considered to be a React Component

The portal property is also considered to be a React Component. It differs from returning a function because this PortalComponent will be rendered as a sibling of the widget's HTMLElement reference element.

The `onReady` function

The onReady function received on the Widget's constructor and assigned to this.options.onReady is a function that as soon as it is called it flags the widget as ready to receive data. That said, calling this.options.onReady is mandatory.

Widget.flow

flow(type, events, layer, baseline, preventRedraw) is called as soon as onReady Promise is resolved and will be executed every time the new data event or event control arrives. In strict sense of a Pipes Widgets, this is the most important method in widget implementation. It should be faster, be careful to avoid memory consumption and any delays on it will result in lower event processing throughput.

The event type string describes what kind of events is coming. Live organizes them in two categories data eventsandcontrol events. For further understanding what event exists and how they like, see Live Events Types section.

The batch of events is an array of objects and its format depends on Pipes Expression (see start event at Live Event Types). If only single events are emitted each time, events.length will be always 1. But Pipes has support to emit at every N items collected, so the event could arrive in batch mode.

Live Widget could have separated layersto execute different Pipes queries at the same time (the widget configuration could choose if multiple layers are supported). The layersparameter is a number with the layer index. The baseline and preventRedraw parameters are boolean.

An illustrative example how implement the flow function is given:

The initialize and destroy methods are optional. Initialize is called at instantiation time and destroy is the last call before the Live removes the container from DOM.

The screenshot below shown an example of a chart instance of Tutorial Pipes Widget using the Pipes Expression => random() every 5 sec that produces only real-time events that spawn every 5 seconds.

Example of the tutorial widget being used at creation of a new Pipes Chart at Dashboard Edit Mode

Widget Life Cycle

Live Widget instance is created at very first time when the user click in widget name at side bar of widget editor (as shown above). But at this point the widget isn't part of a dashboard yet, the user must save it. After saved, now the widget gains an unique ID in dashboard context and will be instantiate every time the dashboard be open (either in edit mode or view mode). Internally a Live Widget instance is named Chart.

Some basic examples of properties of a widget instance are shown below:

  • this.options.id : number that identifies the widget in dashboard

  • this.options.name : string that identifies the widget name in dashboard

  • this.options.mode : string that indicates one of:

    • editorwhen widget itself is being edited

    • edit when dashboard is being edited

    • viewwhen dashboard is opened in view mode

    • fullscreenwhen dashboard is opened in fullscreen mode

The Live Widget Configuration section presents the complete reference of the widget configuration type.

Appendix: complete example files

See the complete example files discussed in the previous headings. You could create a webappdirectory, put all the following files on it and read the Live Widget Packaging section to learn how to build a single bundle.js using Webpack.

Last updated

Was this helpful?