Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted almost 3 years ago

Expense chart component with Flexbox

Bence Zámbó•510
@zambobence
A solution to the Expenses chart component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


Dear All, I would be grateful for any kind of recommendation and feedback.

I have thought that the column with the greatest value could be formatted in Javascript and be dynamic although could not figure it out how.

Thank you Bence

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Prantik•660
    @prantiknoor
    Posted almost 3 years ago

    Hey @benceturbulence. You used this to measure the height height: ${data.amount*2.5}px. But if the data will be big, then the chart will also be big.

    So you can use a limit. const maxHeight = 200;. And you can find the max amount by this: const maxAmount = Math.max(...column.map((el) => el.amount));

    By these two values, you can measure the height of every column. const height = (element.amount / maxAmount) * maxHeight;

    getData().then((column) => {
        // No matter how big the amount, the size of bar will be under maxHeight
      const maxHeight = 200;
    
      const maxAmount = Math.max(...column.map((el) => el.amount));
    
      let htmlArray = column
        .map((element) => {
          const height = (element.amount / maxAmount) * maxHeight;
          return setHtml(element.day, height);
        })
        .join("");
      document.getElementById("column_container").innerHTML = htmlArray;
    });
    
    function setHtml(day, height) {
      return `
        <div class="column">
        <span class="col" style ="width: 100%; height: ${height}px"></span>
        <p class = "days accent">${day}</p>
        </div>
        `;
    }
    
    Marked as helpful
  • James English•620
    @anglicus
    Posted almost 3 years ago

    Yes, you can format the column with the greatest value dynamically. You just have to include some way of finding the maximum value in your JS and then mark that column by adding a class to it. Looking at your code, you could modify your getData() function to look something like this (there may be a more elegant way to do it, but this should at least give you the basic idea):

    getData().then(column => {
      console.log(column);
      let max = 0; // will store the largest value of amount
      let max_index = -1; // will set the index where the largest value is found
      let htmlArray = column.map((element, index) => {
        // check if the amount exceeds the largest found so far
        if (element.amount > max) {
          // if so, set the max and max_index
          max = element.amount;
          max_index = index;
        }
        return setHtml(element);
      });
      const columnContainer = document.getElementById('column_container');
      columnContainer.innerHTML = htmlArray.join('');
      // add .highlight to the column that had the greatest amount
      columnContainer.querySelector(`:nth-child(${max_index + 1})`).classList.add("highlight");
    })
    

    After that, just add a bit to your CSS to recolor the column where the maximum occurred:

    .highlight > .col {
      background-color: var(--cyan);
    }
    
    Marked as helpful
  • Bence Zámbó•510
    @zambobence
    Posted almost 3 years ago

    Thank you very much for your valuable feedback, I really appreciate it.

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub