You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the previous blog post of this series, we created a React website that was retrieving a list of countries using Axios and a REST API hosted in MongoDB Realm.
In this blog post, we will continue to build on this foundation and create a dashboard with COVID-19 charts, built with MongoDB Charts and embedded in a React website with the MongoDB Charts Embedding SDK.
To add some spice in the mix, we will use our list of countries to create a dynamic filter so we can filter all the COVID-19 charts by country.
You can see the final result here that I hosted in a MongoDB Realm application using the static hosting feature available.
Prerequisites
The code of this project is available on GitHub in this repository.
To run this project, you will need node and npm in a recent version. Here is what I'm currently using:
node -v
# v14.17.1npm -v
# 8.0.0
You can run the project locally like so:
cdmongodb-realm-react-chartsnpminstallnpmstart
In the next sections of this blog post, I will explain what we need to do to make this project work.
Create a MongoDB Charts Dashboard
Before we can actually embed our charts in our custom React website, we need to create them in MongoDB Charts.
Here is the link to the dashboard I created for this website. It looks like this.
Dashboard in MongoDB Charts
If you want to use the same data as me, check out this blog post about the Open Data COVID-19 Project and especially this section to duplicate the data in your own cluster in MongoDB Atlas.
As you can see in the dashboard, my charts are not filtered by country here. You can find the data of all the countries in the four charts I created.
To enable the filtering when I'm embedding my charts in my website, I must tell MongoDB Charts which field(s) I will be able to filter by, based on the fields available in my collection. Here, I chose to filter by a single field, country, and I chose to enable the unauthenticated access for this public blog post (see below).
In the User Specified Filters field, I added country and chose to use the JavaScript SDK option instead of the iFrame alternative that is less convenient to use for a React website with dynamic filters.
For each of the four charts, I need to retrieve the Charts Base URL (unique for a dashboard) and the Charts IDs.
Now that we have everything we need, we can go into the React code.
React Website
MongoDB Charts Embedding SDK
First things first: We need to install the MongoDB Charts Embedding SDK in our project.
npmi@mongodb-js/charts-embed-dom
It's already done in the project I provided above but it's not if you are following from the first blog post.
React Project
My React project is made with just two function components: Dashboard and Chart.
The index.js root of the project is just calling the Dashboard function component.
Line 17 - Retrieve the list of countries from the REST API using Axios (cf previous blog post).
Lines 18-22 - Select a random country in the list for the initial value.
Lines 22 & 26 - Update the filter when a new value is selected (randomly or manually).
Line 32 counties.map(...) - Use the list of countries to build a list of radio buttons to update the filter.
Line 32 <Charts .../> x4 - Call the Chart component one time for each chart with the appropriate props, including the filter and the Chart ID.
As you may have noticed here, I'm using the same filter fitlerCountry for all the Charts, but nothing prevents me from using a custom filter for each Chart.
You may also have noticed a very minimalistic CSS file Dashboard.css. Here it is:
The Chart component isn't doing much. It's just responsible for rendering the Chart once when the page is loaded and reloading the chart if the filter is updated to display the correct data (thanks to React).
Note that the second useEffect (with the chart.setFilter(filter) call) shouldn't be executed if the chart isn't done rendering. So it's protected by the rendered state that is only set to true once the chart is rendered on the screen.
And voilà! If everything went as planned, you should end up with a (not very) beautiful website like this one.
Conclusion
In this blog post, your learned how to embed MongoDB Charts into a React website using the MongoDB Charts Embedding SDK.
We also learned how to create dynamic filters for the charts using useEffect().
We didn't learn how to secure the Charts with an authentication token, but you can learn how to do that in this documentation.
If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.
The text was updated successfully, but these errors were encountered:
React Website with Embedded MongoDB Charts
https://www.mongodb.com/developer/how-to/mongodb-charts-embedding-sdk-react/
https://github.com/mongodb-developer/mongodb-charts-embedded-react
This website is an example of how you can embed charts built with MongoDB Charts into a React website with dynamic filters.
This repository is part of the second blog post of this series of two blog posts.
The first blog post talks about MongoDB Realm + React + REST API + Axios.
Final Website
The final result has been deployed in MongoDB Realm Static Hosting.
https://react-charts-npwaa-qynhm.mongodbstitch.com/
to fix build problem
"TypeError: MiniCssExtractPlugin is not a constructor"
https://stackoverflow.com/questions/70715794/typeerror-minicssextractplugin-is-not-a-constructor
facebook/create-react-app#11930
Start the Beast
Author
Maxime Beugnet maxime@mongodb.com
Introduction
In the previous blog post of this series, we created a React website that was retrieving a list of countries using Axios and a REST API hosted in MongoDB Realm.
In this blog post, we will continue to build on this foundation and create a dashboard with COVID-19 charts, built with MongoDB Charts and embedded in a React website with the MongoDB Charts Embedding SDK.
To add some spice in the mix, we will use our list of countries to create a dynamic filter so we can filter all the COVID-19 charts by country.
You can see the final result here that I hosted in a MongoDB Realm application using the static hosting feature available.
Prerequisites
The code of this project is available on GitHub in this repository.
To run this project, you will need node and npm in a recent version. Here is what I'm currently using:
You can run the project locally like so:
In the next sections of this blog post, I will explain what we need to do to make this project work.
Create a MongoDB Charts Dashboard
Before we can actually embed our charts in our custom React website, we need to create them in MongoDB Charts.
Here is the link to the dashboard I created for this website. It looks like this.
Dashboard in MongoDB Charts
If you want to use the same data as me, check out this blog post about the Open Data COVID-19 Project and especially this section to duplicate the data in your own cluster in MongoDB Atlas.
As you can see in the dashboard, my charts are not filtered by country here. You can find the data of all the countries in the four charts I created.
To enable the filtering when I'm embedding my charts in my website, I must tell MongoDB Charts which field(s) I will be able to filter by, based on the fields available in my collection. Here, I chose to filter by a single field, country, and I chose to enable the unauthenticated access for this public blog post (see below).
In the User Specified Filters field, I added country and chose to use the JavaScript SDK option instead of the iFrame alternative that is less convenient to use for a React website with dynamic filters.
For each of the four charts, I need to retrieve the Charts Base URL (unique for a dashboard) and the Charts IDs.
Now that we have everything we need, we can go into the React code.
React Website
MongoDB Charts Embedding SDK
First things first: We need to install the MongoDB Charts Embedding SDK in our project.
It's already done in the project I provided above but it's not if you are following from the first blog post.
React Project
My React project is made with just two function components: Dashboard and Chart.
The index.js root of the project is just calling the Dashboard function component.
The Dashboard is the central piece of the project:
It's responsible for a few things:
Line 17 - Retrieve the list of countries from the REST API using Axios (cf previous blog post).
Lines 18-22 - Select a random country in the list for the initial value.
Lines 22 & 26 - Update the filter when a new value is selected (randomly or manually).
Line 32 counties.map(...) - Use the list of countries to build a list of radio buttons to update the filter.
Line 32 <Charts .../> x4 - Call the Chart component one time for each chart with the appropriate props, including the filter and the Chart ID.
As you may have noticed here, I'm using the same filter fitlerCountry for all the Charts, but nothing prevents me from using a custom filter for each Chart.
You may also have noticed a very minimalistic CSS file Dashboard.css. Here it is:
The Chart component looks like this:
The Chart component isn't doing much. It's just responsible for rendering the Chart once when the page is loaded and reloading the chart if the filter is updated to display the correct data (thanks to React).
Note that the second useEffect (with the chart.setFilter(filter) call) shouldn't be executed if the chart isn't done rendering. So it's protected by the rendered state that is only set to true once the chart is rendered on the screen.
And voilà! If everything went as planned, you should end up with a (not very) beautiful website like this one.
Conclusion
In this blog post, your learned how to embed MongoDB Charts into a React website using the MongoDB Charts Embedding SDK.
We also learned how to create dynamic filters for the charts using useEffect().
We didn't learn how to secure the Charts with an authentication token, but you can learn how to do that in this documentation.
If you have questions, please head to our developer community website where the MongoDB engineers and the MongoDB community will help you build your next big idea with MongoDB.
The text was updated successfully, but these errors were encountered: