Skip to content

seapagan-fem/expense-chart-component

Repository files navigation

Frontend Mentor - Expenses chart component solution

This is a solution to the Expenses chart component challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.

Table of contents

Overview

The challenge

Users should be able to:

  • View the bar chart and hover over the individual bars to see the correct amounts for each day
  • See the current day’s bar highlighted in a different colour to the other bars
  • View the optimal layout for the content depending on their device’s screen size
  • See hover states for all interactive elements on the page
  • Bonus: Use the JSON data file provided to dynamically size the bars on the chart

Screenshot

Screenshot

Links

My process

Built with

  • Semantic HTML5 markup
  • CSS custom properties
  • Flexbox
  • CSS Grid
  • Mobile-first workflow
  • Data read from local JSON file
  • Custom tooltips with animations

What I learned

This was a fun one, with not too many stumbling blocks. I enjoyed creating the graph dynamically and am quite chuffed with the custom animated tooltips too.

To get the max value of the data, we cant use Math.max() directly as it is an array of objects, so I mapped over the spread of the data :

maxSpend = Math.max(...data.map((day) => day.amount));

The max bar height was 150px in the Figma file, so I normalized each bar to that inside a string template :

spendBar.style.height = `${Math.round(150 * (amount / maxSpend))}px`;

The day highlight is dynamically calculated from the actual current day.

const weekday = new Date()
  .toLocaleDateString("default", { weekday: "short" })
  .toLowerCase();

Author