Use a Chart with a REST Data Source
Introduction
With the Charts widget, you can plot data that comes from a REST service.
The widget does not call a REST service itself. Its data source accepts a database query (a microflow, a nanoflow, or an association) so you retrieve the REST data in a microflow and point the chart at that microflow.
This how-to teaches you how to do the following:
- Publish a REST API
- Retrieve the data from a REST service in a microflow
- Use that microflow as the data source for the Charts widget
Prerequisites
Before starting this how-to, make sure you have completed the following prerequisites:
- Create an app
- Import the latest Charts Widgets from the Mendix Marketplace
Setting up Data to be Exposed by a REST Endpoint
Mendix allows you to publish REST web services natively from Studio Pro. This how-to publishes a service first, so that there is an endpoint to consume in the next sections.
To create an Area Chart with data from a REST service, follow these steps:
- Create a new Module in your app.
- Rename the module to ChartsREST.
- Open the Domain model.
- Create Value and Series entities with the attributes and association shown in the picture below.
- Right-click Value and select Generate overview pages....
- Add the Value_NewEdit page generated to your navigation.
- Run the app.
- In your browser, open the NewEdit page.
- Add values and series by entering data in the appropriate fields.
Publishing the Service
To use data from a model in the REST service, you need to create a JSON structure.
Creating the Structure
- Create a JSON Structure
Configuring the REST Service
To configure the REST service, follow these steps:
-
Add Published REST service.
-
Add REST Service Microflow.
-
Add Export mapping.
Retrieving the REST Data in a Microflow
To make the published data available to the chart, consume the service and return its results from a microflow:
- Add a consumed REST service for the endpoint you published.
- Add an import mapping that maps the JSON response to the Value entity.
- Create a microflow named DS_ValuesFromREST.
- In the microflow, add a Call REST service activity that calls the endpoint and applies the import mapping.
- Set the list of Value objects produced by the mapping as the return value of the microflow.
Using the Microflow as a Chart Data Source
To plot the retrieved data, follow these steps:
-
Create a page in your app containing an Area chart widget.
-
Double-click the Area chart widget to open its properties.
-
On the General tab, in the Data source section, add a new Series item.
-
In the General section of the series, leave Data set set to Single series.
-
Set Data source to Microflow and select DS_ValuesFromREST.
-
Set Series name to the text that should appear in the legend.
-
Select the X axis attribute and the Y axis attribute.
-
Run your app and view the chart.