Code Monkey home page Code Monkey logo

expense-chart's Introduction

Expenses Chart Web Component by Jeff Lang

This is a solution to the Expenses chart component challenge on Frontend Mentor.

Time to complete: 10 hrs 5 min

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
  • Use the JSON data file provided to dynamically size the bars on the chart

Screenshot

Expense Chart Preview

Links

My process

Built with

How I Started

I knew I wanted to create a dynamic component using JSON data, so I had to figure that out first. I wanted simple--just read from the file. To do that I needed a development web server and the quickest and easiest way for me was to setup node.js with express. I then used an app.js file to set the path for my static file resources and send the index.html file upon request (the app.js file is included in my github repo for reference).

Everything still works upon pushing the files to a pages Github repo and that was my overall goal with this project.

What I learned

I had seen websites where the bar chart loaded dynamically and the bars were animated as they increased in height to their final value and I wanted to do that with this challenge. I achieved that by assigning default values for height to the bars, and a default value of $0.00 to the label, which were both changed when the data was loaded via JS.

Below is an example chart with one column. The div with class of "exp-col" is the bar:

<section id="chart">
  <div class="exp-col">
    <div class="col-label hidden">$0.00</div>
  </div>
  <div class="weekday">sun</div>
</section>
.exp-col {
  position: relative;
  min-height: 0.1rem;
  width: 100%;
  background-color: #EC755D;
  border-radius: 0.35rem;
  cursor: pointer;
  transition: min-height 0.5s ease-in;
}

A couple of notes on the JS:

  • weeklySpend is an array of just the amounts pulled from the JSON so that the maxSpend can be determined, and that array can be looped over to calculated column height
  • maxColHeight is determined by the height of the chart less the height of the column label
const maxSpend = Math.max(...weeklySpend)
weeklySpend.forEach(spend => {
  expenseColumns[counter].style.minHeight = 
  Math.floor(spend / maxSpend * maxColHeight) + "px"
})

Continued development

  • Media queries and identifying break points went better with this project. I would like to be able to better identify what needs to happen with the layouts, especially for mobile devices.
  • I feel like my JS is a bit hacky, especially with determining and setting the column heights, so I should probably do some refactoring of that code in the future.

Useful resources

  • Kevin Powell: Get started with grid - This was my first real attempt at grid so I knew I wanted to review before coding. Kevin Powell has been one of the most valuable resources for implementing good CSS and layouts. This video is just 10 minutes long and goes over the basics, and it links to a much longer video for more in depth grid techniques.

Author

expense-chart's People

Contributors

jefflangtech avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.