Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 18, 2018 10:27 am

How to Build a SaaS Dashboard in React With Google Sheets and FusionCharts

In this tutorial I’ll explain the process of creating a SaaS dashboard by fetching data from Google Sheets using the Google Sheets API. To create this dashboard we will be using React, a JavaScript library for building user interfaces; FusionCharts, a JavaScript based charting library; and Bulma, a CSS framework based on Flexbox.

Coming Up

Here’s what we’ll be covering:

  1. Setup


  • React

  • FusionCharts and FusionCharts React Component

  • Bulma CSS Framework

  • Google Sheets API Setup

  • Connecting Google Sheets API & Fetching data
  • Building the SaaS Dashboard Layout
  • Creating KPIs
  • Creating Charts
  • Conclusion
  • Before we go ahead and set up, I want to show you a preview of the SaaS Dashboard you’ll be able to create, once you’ve gone through this tutorial. Here’s a live link of what we’ll be building.

    what well be building


    1. Setup

    To follow along, you’ll need to set up your project with following dependencies:


    1. React

    2. FusionCharts Core Package and its React Component

    3. Bulma

    Including React

    Facebook’s React boilerplate will get us started in a jiffy. It will set up React along with all the utilities that we will need for our dashboard app. So, in the terminal of your choice (iTerm with Zsh for me) you can go ahead and enter:

    You can learn more about create-react-app, the boilerplate we have used here.

    saas-dashboard is the working directory where the React boilerplate will be installed along with all utilities and dependencies, and we will be adding a few more that we will need for this tutorial as explained below.

    Including FusionCharts Core Package and Its React Component 

    We will be using FusionCharts to render charts in our dashboard app. You can go ahead and read more about FusionCharts on fusioncharts.com

    There are multiple ways to install FusionCharts; for general instructions you can check out this documentation page.

    FusionCharts Direct Download

    You can directly download JavaScript files from the FusionCharts website and include them in your application using a <script> tag in /public/index.html of the dashboard app.

    Using NPM

    We’ll be using NPM in this tutorial. So, in the terminal, navigate to the working directory i.e. saas-dashboard and enter:

    FusionCharts provides a lightweight and simple-to-use component for React that can be used to add JavaScript charts in React apps without any hassle. We will be using it in our app, so let’s install it using the command below:

    You can learn more about the FusionCharts React component from the FusionCharts repo.

    Including Bulma

    To create the layout and UI for our dashboard app we will be using the Bulma CSS framework. So, in the terminal go ahead and enter:

    Now that we have added all the dependencies for our dashboard app, we can go and set up Google Sheets API.

    Google Sheets API Setup

    We’ll be creating a new project for our dashboard app on the Google Developer API console to consume data from Google Sheets. I’ll be calling it “gsheets-dashboard”. You can create a new project using this link.

    Once the project is created, you’ll be redirected to the Google Developer API dashboard. Now, to enable the Google Sheets API for our app, click Go to APIs overview. Once you click Enable APIs and Services you’ll be presented with the API Library, so go ahead and search for “Google Sheets API”. 

    Once you find it, click Enable, and after it is processed you should see the page as shown below:

    In the sidebar, head over to Credentials and click the Create credentials button and select API Key. Click the Restrict Key and set a name for it (I’ve set it to “SaasDashboardAPIKey”).

    Save the key generated, as we’ll need it to pull data from our Google Sheet later.

    Under API Restrictions select the Google Sheets API and save. Now we are good to go for our next step where we’ll connect the Google Sheets API and fetch some data.


    2. Connecting Google Sheets API & Fetching Data

    We’ll head to the Google Sheet that we will be using for our dashboard app. Here’s a screenshot of how it looks, built with some sample data that I’ve collected for a fictional SaaS business. You’ll see there’s monthly data for three years, focusing on some Key Performance Indicators (KPIs) of a SaaS business. These include revenue, customers, and other growth indicators.

    Our Google sheet

    Now we need to share the sheet so that anyone can see it. For this, in the File menu, click Share. Then, click Get shareable link and after it’s processed, the sheet will be shared for “Anyone with link can view” access by default. 

    Since we want to make the sheet public, head over to “Anyone with link can view” and click the More option in the drop-down. Select “On - Public on the web” option and save. 

    You can access the sheet I’ll be using from this link.

    We’ll keep a note of the spreadsheet ID (this can be found in the URL for Google Sheets, for me it’s 1QZ3vKssyG9xEG-RJklQlBezIR8WqCfML4EfO2qe5vbw). 

    We will be using the batchGet method for our dashboard app. It returns one or more ranges of values from a spreadsheet. You can learn more about it here. Armed with both the API Key and spreadsheet ID, let’s head to Google Sheets API explorer to test it (you can also use browser/postman (I used this!) to test it and get JSON response which looks something like this):

    test

    I’ve censored the API Key–you can put your own API Key in its place. 

    Now let’s open up our working directory (saas-dashboard for me) in a code editor and create a new file called config.js. Add your API key and spreadsheet ID as follows.

    Now let’s head over to the App.js file. We’ll be adding everything directly to App.js, which our boilerplate has given us. This is not ideally the best architecture, rather just a display of the concepts.

    The steps below show how I’ll connect our dashboard app to the Google Sheets API and fetch data from it:

    1. Import config.js we created using the code below and declare a variable with a request URL for the Google Sheets API.

    2. Now, we’ll set an empty array in this.state as shown in the code below:

    3. Fetch the JSON data from React’s lifecycle componentDidMount method:

    Awesome! Now that we have established a connection with our Google Sheets, we can start building the layout for our dashboard. 

    Note: You can verify the connection by logging the items variable inside state.


    3. Building the SaaS Dashboard Layout

    We’ll be using Bulma to build the layout for our dashboard app. Bulma is a CSS framework based on Flexbox and gives us tons of pre-built and customisable elements and components.

    Here’s how it will look. For more information on what these KPIs mean for a business, read Saas Metrics That Really Matter: KPIs You Should Be Tracking. Once you’ve created the dashboard, the final version will look like the screenshot below:

    You can also check out the live dashboard here.

    Now, let’s divide our dashboard’s layout into three parts:


    1. Navigation Section

    2. KPI Section

    3. Charts Section

    We will also overwrite some of the default styles provided by Bulma using our own CSS which will be present in the file App.css.

    Creating the Navigation Section 

    To create the navigation, we will be using Bulma’s Navbar component. Below is the resultant HTML snippet:

    Now that our navigation section is ready, we’ll create a container to house the next two sections of our dashboard. Here’s the HTML snippet:

    You can learn more about containers here.

    Creating the KPI Section 

    To create the KPI section, we’ll use an HTML <section> and use the Columns and Card components provided by Bulma. Below is the HTML snippet:

    The above snippet will create one KPI card. Similarly, we will create cards for all four KPIs that we want to showcase.

    Creating the Charts Section 

    To create the charts section, we’ll again use an HTML <section> with Columns and Card components provided by Bulma. We’ll be leaving an empty <div> with the unique ID for the chart.

    Below is the HTML snippet:

    We will add all the chart cards in single column components to make our dashboard responsive, adding different breakpoints provided by Bulma for each column. You can learn more about these responsive break points in Bulma’s documentation.

    The above snippet will create one chart card. Similarly, we will create cards for all six charts that we want to showcase. If you’ve followed the above steps thus far you should have a similar layout as in the image above. If not, don’t worry, I’ll be adding the link to Github repo for this dashboard at the end of the tutorial.


    4. Creating KPI’s for SaaS Dashboard

    Now that our layout is ready, we will define functionality for some of the elements and feed data to them from Google Sheets. We begin by defining a function called getData in our component which will take the year as an argument to de-structure Google Sheets’ data present in the app’s state.

    Now, we’ll loop through the data to calculate values as needed for KPIs. Below is the code to create the KPI for “Renewed Users”.

    Similarly, we will define variables for other KPIs and assign a value to them upon looping through the data using the above code snippet.


    5. Creating Charts for SaaS Dashboard

    Now, we will form JSON data for the charts and use FusionCharts and its React component to render them.

    In the getData function that we created in the previous step, we’ll define an empty array which will have data for the chart. Below is the code needed:

    We will be using the “Multi-series 2D Single Y Combination Chart” (mscombi2d) chart in our dashboard. FusionCharts provides a ton of attributes which can be used to customize the look and feel of your charts.

    Now, we will create a file called “chartCosmetics.js” which will have cosmetics options for our chart so that we don’t have to define them each time we create one. Here’s how it looks:

    Now, we will form a JSON data array for each chart and use the above cosmetic options:

    Note: You have to declare a null variable in the app’s state for each chart so that it can be used later as we did above for Google Sheet data.

    Now that the JSON data is ready for our charts, we will pass it to FusionCharts’ React component under the <div> element we created for each chart.

    To learn more about the usage of the FusionCharts React component, you can refer to this developer documentation page

    You can follow the above steps to create the remaining charts. We will now call getData function with 2018 as an argument from the componentDidMount method so that our dashboard loads with 2018 data by default. If you’ve followed the above steps thus far you should have a functional dashboard as in the image below:

    final dashboard

    Conclusion

    This tutorial will help you create a SaaS dashboard using Google Sheets. Having followed along, you can now work your magic on adding more UI elements, charts, KPIs and additional features. I have added some styling and functionality myself and you can check out the final dashboard here.

    For reference, you can check out the source code from the Github repository. If you have any questions or feedback, leave a comment below or yell at me on Twitter!


    Original Link: https://webdesign.tutsplus.com/tutorials/building-a-saas-dashboard-with-google-sheets-in-react--cms-32425

    Share this article:    Share on Facebook
    View Full Article

    TutsPlus - Code

    Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

    More About this Source Visit TutsPlus - Code