In this exercise, you'll be augmenting the sensor status page with an sap.f.Card to present additional data about the sensor's status. You'll also implement some layouting with box controls and incorporate an sap.f.cards.NumericHeader to accurately represent the temperature.
Let's start with spiffing up the SensorStatus.view.xml view.
-
Open
sensormanager/webapp/view/SensorStatus.view.xml. -
Add the
sap.fandsap.f.cardslibraries toSensorStatus.view.xml.
<mvc:View
controllerName="keepcool.sensormanager.controller.SensorStatus"
displayBlock="true"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:f="sap.f"
xmlns:card="sap.f.cards">- Embed an
sap.f.Cardwith a card header intoSensorStatus.view.xml. Add the customer name as the header title through data binding. To do this, replace the<Page>...</Page>with the following:
<Page
id="SensorStatusPage"
title="{i18n>titleSensorStatus}"
showNavButton="true"
navButtonPress=".navToSensors">
<content>
<f:Card width="200px" class="sapUiSmallMargin">
<f:header>
<card:Header title="{parts: ['i18n>cardTitle', 'sensorModel>customer']}"/>
</f:header>
<f:content>
</f:content>
</f:Card>
</content>
</Page>To display the data for the chosen sensor in your card, you need to access the data provided by the Router for the navigation step and assign the correct binding context to the view. This way, the standard data binding will work with the data related to the chosen sensor.
-
Open
sensormanager/webapp/controller/SensorStatus.controller.ts. -
Attach a callback function to the
routeMatchedevent to fetch the passed sensor index and utilize it in the data binding path for the view. You also have to define a TypeScript interface for the parameter object, so that the type of index is clearly defined asnumber. As result, the file should look like this:
import Controller from "sap/ui/core/mvc/Controller";
import UIComponent from "sap/ui/core/UIComponent";
import { Route$MatchedEvent } from "sap/ui/core/routing/Route";
interface RouteMatchedParameters {
index: number
}
/**
* @namespace keepcool.sensormanager.controller
*/
export default class SensorStatus extends Controller {
public onInit() {
(this.getOwnerComponent() as UIComponent).getRouter().getRoute("RouteSensorStatus")?.attachMatched(this.onRouteMatched, this);
}
public onRouteMatched(event: Route$MatchedEvent) {
this.getView()?.bindElement({
path: "/sensors/" + (event.getParameter("arguments") as RouteMatchedParameters).index,
model: "sensorModel"
});
}
public navToSensors() {
(this.getOwnerComponent() as UIComponent).getRouter().navTo("RouteSensors");
}
}Within
onRouteMatched, the first line sets the data binding context to the values filled below: the model is the WsensorModel" one holding the sensor data and the path inside the model from which the data is used is "/sensors/" plus the index of the clicked sensor, which is retrieved from the routing event. The resulting path (like e.g./sensors/8) refers to the data of one specific sensor in the JSON data structure.When data properties like "temperature" are used in this view's data binding, they automatically refer to the temperature of the sensor with the current index 8.
- Switch to the browser tab where the application preview is opened. Click any sensor. Now the sensor status page contains a card with the customer name.
To further enhance the visualization, you will swap the sap.f.cards.Header with the sap.f.cards.NumericHeader in your newly created card.
- Open
sensormanager/webapp/view/SensorStatus.view.xmland exchange the existing<f:header>content with the following:
<f:header>
<card:NumericHeader title="{i18n>cardTitle} {sensorModel>customer}"
subtitle="{sensorModel>location}" number="{sensorModel>temperature}" scale="{i18n>temperatureUnit}"/>
</f:header>- Switch to the browser tab where the application preview is open. Click on any sensor. Now, the sensor status page presents a card that includes temperature information (still without Fahrenheit conversion).
- Add a formatter to apply semantic coloring to the card header.
The formatter utilizes both the threshold and the current temperature from the model. Based on these values, it returns the
sap.m.ValueColor. Opensensormanager/webapp/format/util.tsand insert the formatter function given below. Don't forget to import thesap.m.ValueColormodule, which offers excellent color support, using the Quick Fix.
export function formatValueColor(temperature: number): ValueColor {
if (temperature < Threshold.Warm) {
return ValueColor.Neutral;
} else if (temperature >= Threshold.Warm && temperature < Threshold.Hot) {
return ValueColor.Critical;
} else {
return ValueColor.Error;
}
}-
The
sap.f.cards.NumericHeadercontrol features astateproperty, which enables you to present the state of your control in a visually engaging manner. Proceed to opensensormanager/webapp/view/SensorStatus.view.xml. -
First add the dependency to retrieve the required
util, including the "core" namespace needed for the "require" itself.
<mvc:View displayBlock="true"
controllerName="keepcool.sensormanager.controller.SensorStatus"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:f="sap.f"
xmlns:card="sap.f.cards"
xmlns:core="sap.ui.core"
core:require="{
util: 'keepcool/sensormanager/format/util'
}">- Add the
stateproperty to your numeric header and enter data binding information pointing to your newly created formatter function. As you have access to theutilmodule, you also use it for the number formatting and Fahrenheit conversion.
<f:header>
<card:NumericHeader title="{i18n>cardTitle} {sensorModel>customer}" subtitle="{sensorModel>location}"
number="{parts: ['sensorModel>temperature', 'i18n>temperatureUnit'], formatter:'util.formatTemperature'}"
scale="{i18n>temperatureUnit}" state="{path: 'sensorModel>temperature', formatter: 'util.formatValueColor'}" />
</f:header>- Switch to the browser tab where the application preview is opened. Select any sensor. The sensor status page should now display a card with color-coded temperature details that vary based on the temperature value.
To show some historical data you can use the temperatureLog of the sensor data. You'll use an sap.suite.ui.microchart.LineMicroChartto add the data points.
-
Open
sensormanager/webapp/view/SensorStatus.view.xml. -
Add the namespace for the
sap.suite.ui.microchartlibrary to theSensorStatus.view.xml.
<mvc:View
controllerName="keepcool.sensormanager.controller.SensorStatus"
displayBlock="true"
xmlns:mvc="sap.ui.core.mvc"
xmlns="sap.m"
xmlns:f="sap.f"
xmlns:card="sap.f.cards"
xmlns:core="sap.ui.core"
core:require="{
util: 'keepcool/sensormanager/format/util'
}"
xmlns:mc="sap.suite.ui.microchart">- Add the chart to the content aggregation of the card and bind the
temperatureLogto thepointsaggregation. For each point you'll display thetemperatureproperty.
<f:content>
<mc:LineMicroChart
id="lineMicroChart"
class="sapUiSmallMarginBeginEnd sapUiSmallMarginBottom"
size="Auto"
points="{sensorModel>temperatureLog}">
<mc:LineMicroChartEmphasizedPoint
x="{sensorModel>time}"
y="{sensorModel>temperature}"/>
</mc:LineMicroChart>
</f:content>🧑🎓 In the JSON data, the
temperatureLogin each sensor is an extensive array containing historical temperature data and the corresponding timestamp. By binding thepointsto this array, eachtemperatureLogentry will serve as one point in the chart. X and Y values are mapped in theLineMicroChartEmphasizedPointsettings.
- Switch to the browser tab where the application preview is opened. Click any sensor. Now the sensor status page contains a chart with a temperature history.
-
Open
sensormanager/webapp/view/SensorStatus.view.xml. -
To enhance the readability of the chart, you should provide some additional information. Add labels to the chart along with an indicator for the threshold. As done in previous exercises, also provide some semantic coloring and show only the relevant data points with the help of expression binding.
<f:content>
<mc:LineMicroChart
id="lineMicroChart"
class="sapUiSmallMarginBeginEnd sapUiSmallMarginBottom"
leftBottomLabel="15 AM"
rightBottomLabel="15 PM"
threshold="{=util.formatTemperature(util.Threshold.Hot, ${i18n>temperatureUnit})}"
thresholdDisplayValue="{=util.formatTemperature(util.Threshold.Hot) + '{i18n>temperatureUnit}'}"
showThresholdValue="true"
size="Auto"
points="{sensorModel>temperatureLog}">
<mc:LineMicroChartEmphasizedPoint
x="{sensorModel>time}"
y="{parts: ['sensorModel>temperature', 'i18n>temperatureUnit'], formatter:'util.formatTemperature'}"
color="{path: 'sensorModel>temperature', formatter:'util.formatValueColor'}"
show="{=${sensorModel>temperature}>util.Threshold.Warm}"/>
</mc:LineMicroChart>
</f:content>- Switch to the browser tab where the application preview is displayed. Select any sensor. The sensor status page should now exhibit a chart containing a temperature history with colored data points, additional labels and a threshold line.
Congratulations! You've successfully enriched your sensor status page with a detailed card showing sensor information. This card, created using sap.f.Card and sap.f.cards.NumericHeader, displays valuable sensor data such as temperature, customer details, and a history chart of temperature data. You've also learned how to use microcharts with additional features like emphasized points, threshold lines and semantic coloring for better data visualization.
You have done it all, this was the last step of the tutorial. Keep up the good work and stay curious about SAPUI5 as there is still a lot to learn! Find more information and tutorials about SAPUI5 below.
- Cards: https://ui5.sap.com/#/topic/5b46b03f024542ba802d99d67bc1a3f4
sap.f.Card: https://ui5.sap.com/#/api/sap.f.Cardsap.f.cards.NumericHeader: https://ui5.sap.com/#/api/sap.f.cards.NumericHeader- Methods and Events for Navigation : https://ui5.sap.com/#/topic/516e477e7e0b4e188b19a406e7528c1e
- UI5 Microcharts: https://ui5.sap.com/#/topic/9cbe3f06465e47b8a136956034a718ed
- Formatting, Parsing, and Validating Data: https://ui5.sap.com/#/topic/07e4b920f5734fd78fdaa236f26236d8
- UI5 Tutorials: https://sapui5.hana.ondemand.com/#/topic/8b49fc198bf04b2d9800fc37fecbb218
- SAP Tutorials: https://developers.sap.com/tutorial-navigator.html




