Add custom components
With the React framework, you can create your own components from scratch. These components can be used for two things: dynamic parameters and charts
Objective
With the React framework, you can create your own components from scratch. These components can be used for two things: dynamic parameters and charts.
At any point, feel free to refer to the Technical Guide 🔧 to learn more about custom components!
Use a custom component as a dynamic parameter
Generate a component
Let's see how to create a custom component and how to use it as a dynamic parameter.
As an example, we will show step-by-step you how to create this checkbox group component:
To generate a component, you can use our pre-build script by running the command below:
Select the component generator, name it DynamicParameterCheckbox and answer "No" to the last question as shown below:
It is important that your component name starts with DynamicParameter to register it as a dynamic parameter
This script generates a folder in src/components containing three files :
DynamicParameterCheckbox.jsxDynamicParameterCheckbox.lessindex.js
Declare a component
Before you start working on the component itself let's take a minute to see how to declare it and use it.
Follow these steps to declare the new component:
-
Go to
src/components/index.js -
Import the component by adding this line at the top of the file :
-
Declare it by adding its name to the components object
You can now call this component in your configuration files by specifying its alias. To obtain a component alias, transform its name from camelCase to kebab-case (DynamicParameterCheckbox becomes dynamic-parameter-checkbox), then remove "dynamic-parameter" and/or "chart" from it.
Here, the component's alias is checkbox. Now go to config/menus/sidebar.json and change the component property of dynamic-weekday to "checkbox" (i.e. the alias).
Go to your application page, notice that the dynamic-weekday component has changed.
Don't worry if it doesn't work right now, its behavior will be set in the next section.
Edit a component
The framework used in this guide is going to be React.
We will guide you step by step to make your components, but if you are having any trouble because you are not familiar with this framework you can check the documentation here. 🆘
Also, since we are working with functional components, we will use one of latest features available, React Hooks. You can check this page for more information.
Let's start editing DynamicParameterCheckbox.jsx
Since this component is declared as a dynamic-parameter, some props are already automatically passed to it by Platforms data flow logic. These props can be logged when the component renders the first time, by making use of the useEffect hook.
Start by importing useEffect and useState (you will need this one later) by changing the first line of the file:
Then add props to the arguments of the functional component:
Finally, add the useEffect hook inside this function right before the return statement.
Your functional component should be defined as below:
Open your browser's console panel, you should be able to see the props.
As you can see, most of these props are passed in sidebar.json when defining the dynamic-parameter.
The only exception is items which is the result of an internal request querying the field specified in dictionary (week_day in this case)
Now let's create the checkboxes.
To achieve this, you will need to install a small package: react-checkbox-group. Do so by running:
This package allows you to use a checkbox group component. You can now import it by adding this line at the top of the file:
Add a boolean variable expanded to the component state to be able to display or hide the CheckboxGroup with a little arrow icon:
Change the function return statement like this:
The title section is using props.title. This means that you need to pass a title prop in dynamic-weekday definition in config/menus/sidebar.json, as shown below: (the HTML element has also been removed since it's now redundant)
Finally, style your component by changing DynamicParameterCheckbox.less, using the code snippet below.
DynamicParameterCheckbox.less
Your dynamic-parameter looks good now, but it's not working properly yet.
There is a problem, you cannot check any box!
The reason is that you need to bind CheckboxGroup to our dynamic-parameter component. To do so, you have to add an array to the state of your component. It will keep track of the selected options, so name it selectedOptions:
Add a function onChange to update the array you have just defined:
Finally, you need to add the two properties (onChange and value, an array containing selectedOptions) to CheckboxGroup. Bind them by changing the return statement:
Almost there! 🚴♂️
The last step is to bind this dynamic-parameter to the redux global store to allow it to affect charts requests. We'll use React Redux hooks for a more modern and streamlined approach. Add these lines to the imports at the top of the file:
Next, inside your DynamicParameterCheckbox functional component, access the Redux state and dispatch function using hooks. Place these lines, inside your component function:
-
useSelector: This hook allows you to extract data from the Redux store state. Here, we're fetching the value associated with props.id from the querystring part of your Redux state. This options variable will automatically update whenever the Redux state changes for this specific ID. -
useDispatch: This hook returns a reference to the dispatch function from the Redux store. You'll use this dispatch function to send actions to update the store.
Inside your DynamicParameterCheckbox functional component, create a helper function updateModel to dispatch your actions to the global store:
This function now uses the dispatch function obtained from useDispatch to send the set action, updating the querystring in the Redux store.
Then, update your onChange function to simply call updateModel with the new options:
To ensure your component's internal state (selectedOptions) is synchronized with the Redux store's state (especially after a page refresh or external changes to the Redux state for this parameter), update your useEffect hook like this:
This useEffect hook will run whenever the options value (which comes directly from your Redux store via useSelector) changes. It keeps your component's local selectedOptions state in sync with the global Redux state, providing a consistent view of the dynamic parameter.
Your dynamic parameter is now connected to the global store. You can make sure of this because the charts connected to dynamic-weekday are affected by this dynamic parameter.
You have created your first custom dynamic parameter.
If you got lost 😪along the way, no worries. You can find the final code here.
Adding a search bar
Let's add a second dynamic parameter to be able to filter by station.
Generate a new component:
Select the component generator. Name it DynamicParameterAutocompleteBox and answer "No" to the last question.
Change the content of the newly generated .jsx file to this:
DynamicParameterAutocompleteBox.jsx
Declare it in src/components/index.js
by importing your new component and adding it to the components object defined in the file.
Go to config/dashboards/rides-analytics.json, look for the panel whose id is search-bar and replace it with a dynamic-parameter as such:
Finally, remove the whole dyn-stations object from config/menus/sidebar.json, as it is redundant with the new search bar.
Use a custom component as a chart
In this section, you will learn how to create a custom component and how to use it as a chart.
As an example, you will create an interactive map component using react-leaflet library. The final result will look like this:
Let's start by creating the component using the script as usual.
Select the component generator, name it ChartMap and answer "No" to the last question.
It is important that your chart component name starts with 'Chart', so it is recognized and registered properly.
Declare it in src/components/index.js.
You can now use it in your analytics dashboard. Go to rides-analytics.json, and add the new chart and its title to the custom-map panel:
Like dynamic-parameters, charts also automatically receive some properties when they are mounted. Let's see what they look like.
As usual, import useEffect and useState by changing the first line of the ChartMap.jsx file:
Then add props to the arguments of the functional component:
Finally, add a useEffect hook inside this function right before the return statement.
Your final functional component should look like this:
Open your browser's console panel, you should be able to see the props passed to this component.
Most of these props come from the chart definition in config/dashboards/rides-analytics.json.
The exception is data which is a JSON representing the result of the request associated with the chart. This JSON is meant to be as generic as possible, so it can be a bit tedious to manipulate.
In this example -- and in most cases -- the JSON object is too complex so we use this helper function to flatten it and make it way easier to handle:
Copy and paste the code snippet above between imports and ChartMap, then update your useEffect hook content to visualize the new flattened results:
To display these results on the map, you need to store this data in the component's state. Declare an array inside the ChartMap component like this:
Update useEffect to change this array instead of logging the results:
Now that you've prepared the data, let's see now how to make the map.
Now import react-leaflet components by adding this line to the top of your file:
Go to ChartMap.less and change the stylesheet using the code snippet below:
ChartMap.less
Go back to ChartMap.jsx and define your default map parameters (center, zoom, minZoom) right below your state:
Change your function return statement to display an empty map centered on Chicago:
Now add circle markers inside LeafletMap right below the TileLayer to represent stations:
You can customize your circle markers by adding some props to control their size, their weight, their color, etc. You can find the complete documentation here
Below a code snippet changing the circles' color and making their size proportional to the rides.
Next, add a popup to display the station name and the number of rides associated to it when clicking a circle.
Add it inside CircleMarker as shown below:
You can make the average number of rides follow the formatting rules specified in config/formatter.json for rides by using FpMeasure.
Import FpMeasure:
Change the popup value:
If you play a little with your map, you will notice that it keeps reloading when you change your dynamic parameters. You can disable this behavior by adding a noReload property into your chart configuration options in config/dashboards/rides-analytics.json:
Reorganize your circles by clusters to avoid overlapping, it's pretty straightforward thanks to the react-leaflet-markercluster library.
Import the MarkerClusterGroup component:
Then wrap your circle markers in MarkerClusterGroup like this:
You have created your first custom component.
If you got lost, don't worry. You can find the final code here.
Learn more about creating your custom components in the Technical Guide!
Publish your local app on Data Platform
Now that you've completed your app locally, it's time to plug it into the platform.
You can either do it by uploading the app as a zip file, or by connecting it to a git repository.
Import your app as a zip file
First, you need to export your app as a zip. Your zipped file should not contain the following folders: node_modules, forepaas, webpack, .git.
You can also use our pre-build script to zip your app, to generate a zip file ready to be uploaded to your Project, simply run this command.
A zip file with the same name as your application folder will be generated.
Then, you can either create a new application using this .zip file by following these steps or you can edit an existing application and upload the zip file to create a new version of your application:
Connect your app to a git repository
To link a git repository to the platform, please follow this guide.
Once your app is uploaded, build it then deploy it.
Your fancy app is now deployed on Data Platform!
Go further
If you need training or technical assistance to implement our solutions, contact your sales representative or click on this link to get a quote and ask our Professional Services experts for a custom analysis of your project.
Ask questions, give your feedback and interact directly with the team building the Data Platform on the dedicated Discord channel.
If you need support with your OVHcloud services, create a request in our Help Centre.
Join our community of users.

