profile

Diary Of A Dev

#5: JavaScript interview task - Build an interactive pie chart.

Published about 1 month ago • 1 min read

Hello Devs!

Welcome back to another Tuesday of building a frontend feature. Today we are solving a very commonly asked Interactive JS feature in an interview setup.

[An interactive pie chart].

This one is going to be a quick issue as there isn't many moving parts to this question.

What are we building?

Live preview with code: https://stackblitz.com/edit/vitejs-vite-psytzb?file=index.html

Philosophy

Here the task is to create a range slider and a pie chart and bind the `pie chart` value with `range slider` value.

So that when range slider value is changed, it reflects automatically in pie-chart.

Simple! :-)

System Design

Creating a range slider is straight forward with `<input type="range" />`.

The challenge here is to create the `pie-chart`.

For the pie-chart we will start with a `<div>` and `--percentage` CSS variable.

`--percentage` here is a CSS variable. Its value can be manipulated with JavaScript.

Let the coding begin

First thing first.

Add HTML body.

Here we have added:

  • Range input with minimum, maximum allowed and default value (set to 10) specified.
  • A div for pie-chart with style set to `--percentage` variable (default set to 10) which will be manipulated with JS.

Now let's add some CSS to our HTML.

The thing to notice here is `background-image` property.

That's where the secret to this task is.

`conic-gradient` allows to set background-image to specified percentage of background. For ex:
- 100% will add image to full background.
- 50% will add image to half background, and so on.

And that's why we added `--percentage` variable, so that we can change it based on range input value.

Alright, now all we got to do is capture range input value and bind it to the `--percentage` CSS variable. So let's do that quickly.

This is pretty easy to understand.

  • Add an event listener to range slider input.
  • Whenever range slider input is changed, set the pie-chart div style `--percentage` variable to the value (event.target.value) from range slider.

Well, go ahead and change the value of your range slider. The pie-chart automatically updates in real-time.


Reply to this email, if you have any suggestion on which feature should I build next.


Whenever you are ready, I can help you in 2 ways.

  • Ask Me Anything: If you have any question related to web development. Ask me here. I reply to all questions.
  • LinkedIn For Developers: Book 1:1 call with me to turn around your LinkedIn profile and attract better opportunities.

Diary Of A Dev

Become better F.E developer in 10 minutes a week.

I am building one complex frontend feature or solving a complex frontend interview question every week. And breakdown the exact steps, thought process, and system design behind it. Join the newsletter to learn with me.

Read more from Diary Of A Dev

Hello Devs, The feature we are building today is frequently asked in React interviews. But it doesn't test only your React skills, it test your overall JavaScript and basic logic building skills. So, let's dive into building our fourth frontend feature: [Folder/File explorer UI] What are we building? Live preview with code: https://stackblitz.com/edit/vitejs-vite-4bqecn?file=src%2FApp.jsx Philosophy The basic philosophy behind building a "Folder/File explorer UI" is as following: List all the...

about 2 months ago • 3 min read

Hello Devs, Nowadays, one feature that you will see in almost all sites specially in blogs is theme switching specifically dark mode. And that's what we are building today. [Dark Mode in NextJs page with Tailwind and Shadcn-UI] So, let's dive in to build our third frontend feature. What are we building? Live preview with code: https://stackblitz.com/edit/stackblitz-starters-adalin?file=app%2Fpage.tsx Philosophy. First, let's understand the philosophy and basic system design behind adding dark...

about 2 months ago • 4 min read
password strength indicator screenshot

Howdy Devs, Las week we built an `Adaptive progress bar text`. Despite the complexity of that feature, I am thrilled to see the response from you all on my first issue. Thanks a lot for that. Today we are building a simple "Password Strength Indicator" in React. So, let's dive in to build our second frontend feature. [Password Strength Indicator] What are we building? Live Preview: https://stackblitz.com/edit/vitejs-vite-usrrut?file=src%2FApp.jsx Let the coding began. If you have built...

2 months ago • 2 min read
Share this post