How to show progress percentage in gradient bar?
Tags: react, css, tutorial
Published: 2020-03-16 19:00:06

---
title: How to show progress percentage in gradient bar?
published: true
description: make custom component to render percentage in gradient bar
tags: react, css, tutorial
---

Today we are going to build this with the help of react and Css variables. So let's get started.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a2do69keizamu3whx15o.png)

The demo for the tutorial is [here](https://codesandbox.io/s/sad-rain-8d3qd)

#Challenge
If there were no segments, it'd have been easier to implement the progress bar. But the progress bar itself is divided into segment like in the picture above. So before writing the range function, there are some initialization to be done to know in which bar the percentage falls on. 

#Initializing essential variables
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7n3ado6xkegwd56bzbrn.png)
This variable gives us what 100% equals to in each bar. Since 100 / 7  = 14.28, each bar is 14.28%.

TruePercent variable will hold the percent we want to show in the bar.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dtfe9vgoyd6o7v84vue7.png)

We also want to show the label in this case indicating high value, medium value or low value.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3vlg5ambncyav6yewe30.png)
The rangeIndicator will give us the value of to which quartile or bar the percentage falls on. 

For example: 
*If the truePercent = 78, the rangeIndicator = 78/14.28 = 5.46 ~= 5. Thus the potentialLabel = 'medium'.*

#Creating Indicator
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ier1tiklfsxabrb95j4v.png)
This will just render the indicator with low, medium or high text.

#Creating the structure
The structure is simple. All the work in done in the rangeArray function.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q6oz88ev50ovzag1v07s.png)

#Main Function
All the work is done in this function. The function is responsible for rendering the colored and empty bar.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ay6ud238sigzdhld9770.png)

Let's break the function into multiple parts.

The loop basically runs from 1 to BAR_COUNT value.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zq2nxkas23pft58r9e94.png)
This variable will give the percent which we'll compare with truePercent to know up to where the bar should be filled.
i.e Math.round(1 * 14.28) = 14
    Math.round(2 * 14.28) = 28.56 ~= 29 and so on.

If the truePercent > currentPoint, we'll simply fill the bar.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/egtuzirpzvgkypfq8u4u.png)

If not, then further calculation needs to be done.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lq6buoain9jfjy1c8yaz.png)
The linear-gradient css property can be used to make different sorts of gradient effect. To achieve the effect of half filled and half empty, we need to know how much the filled percent is. The basic syntax for linear-gradient is 

*background-image: linear-gradient(90deg, color percent, white percent);*

Since we know the truePercent lies in 6th bar. We need to know up to which percent of 6th bar the color is filled. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/92qdnrmio96ujnc1ngk3.png)

The above percent will become
((78 - 72)/14) * 100 = 42.85 ~= 42

Thus the percent for gradient is 42% and for white is (100-42) = 58% and the quartileValue = 6.

Once we have percent and quartile value, now it's time to set the percent value to Css variable and push the remaining bar into the array. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/uk85hxp9ismvdegbls5e.png)

When the loop reaches to the quartileValue, the root.style.setProperty will set the percent value to the ith block. The left value set above is use to position the indicator exactly at the gradient separation part. The element is then pushed into array and the class is set according to the condition satisfied.

In this was, we can make progress bar indicator with the user defined percent value. Feel free to comment if the code is confusing :). 



I hope this tutorial will be useful for some of you in coming days. Will return soon with another tutorial.