Tips for changing the background color depending on the value

I am creating a table displaying the results of a tournament. Each team's final placement and original seeding will be listed. I plan to include a small bubble next to each team showing how their final placement compares to their initial seeding. To visually represent this, I want the background color to become darker red for teams that performed worse and darker green for teams that performed better.

For instance:

Team A finished 1st but was seeded 3rd -> light green
Team B finished 2nd yet they were seeded 30th -> dark green
Team C finished 10th after being seeded 8th -> light red
Team D finished in 30th place despite having been seeded 1st -> dark red

I also want to establish a threshold so that teams that over or underperformed by a certain margin will have the same intensity of color.

While my website is created using Angular, I wonder if achieving this effect can be as simple as adjusting some CSS properties?

Here is a screenshot from another website where this feature is already implemented:

In the image, you can see the final rankings on the left side and the comparison between performance and initial seeding on the right. An empty field indicates that the seed matches the finish position

Answer №1

To easily manage colors, consider creating an array dedicated to storing color data.

Here is a TypeScript example:

colors = [{ finished: "1st", color: "light green" }, { finished: "30th", color: "dark green"}]

Next, iterate through your table data and apply the ngStyle directive:

<table>
  <th>Team</th>
  <th>Finished</th>
  <tr *ngFor="let res of colors" [ngStyle]="{'background': getColor(res.finished)}">
    <td>res.whateveryourfieldiscalled</td>
    <td>res.whateveryourfieldiscalled</td>
  </tr>
</table>

Finally, implement a TypeScript method called getColor:

getColor(finished) {
 return this.colors.filter(item => item.finished === finished)[0].color
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to apply CSS to an image within a list item that is within an unordered list nested inside a specific

I've encountered an issue in my html file related to a navigation bar situated at the top of my page. The code snippet that initiates this navigation bar is as follows: <div class="“topNav"> <ul> <li><img src= ...

Displaying Unicode CSS Font Awesome Icons as Boxes in a React Project

I have incorporated the jPlayer example into a create-react-app. Encountering an issue where font-awesome icons are displaying as boxes in this CodeSandbox illustration. https://codesandbox.io/s/peaceful-heisenberg-d59nz?fontsize=14&hidenavigation=1&a ...

Changing the CSS class of the Bootstrap datetime picker when selecting the year

Is there a way to change the CSS style of the Bootstrap datetime picker control so that when selecting years, the color changes from blue to red? I attempted to do this with the following code: .selectYear { background-color:red!important; } However ...

How is it possible for the igx-expansion-panel to close when there is a nested angular accordion present?

Currently, I am faced with the challenge of closing the igx-expansion-panel within my Angular project. While everything functions smoothly with a standard panel, things get a bit tricky when dealing with nested angular accordion structures using igx-accord ...

"Enhance your website with a backspace button using jquery - here's

Recently, I delved into the world of jQuery and decided to test my skills by creating a jQuery calculator. Everything worked perfectly except for the backspace button. This is what I tried: if (value === backspace) { $(input).val($(input).val().substring ...

Displaying dates on the Amcharts category axis for instances with empty data

I am currently creating a fruit consumption chart for each day, and so far everything is working correctly in the provided example. var chart = AmCharts.makeChart("chartdiv", { "type": "serial", "hideCredits": true, "fixedColumnWidth": '10px& ...

Changing the position of the icon in the bootstrap validator

I'm currently attempting to validate form fields in a web project. The goal is to achieve a specific result, similar to the image below: https://i.stack.imgur.com/EVeJf.png While I have made progress with a simple solution that almost meets the requi ...

Error: Uncaught TypeError - The function indexOf is not defined for e.target.className at the mouseup event in HTMLDocument (translator.js:433) within the angular

Upon clicking on an SVG to edit my data in a modal bootstrap, I encountered the following error: Uncaught TypeError: e.target.className.indexOf is not a function at HTMLDocument.mouseup (translator.js:433) This is my SVG code: <svg data-dismiss ...

Issue with Angular Reactive Forms: Unable to identify control with unspecified name attribute during form submission

Currently, I am working on an Angular project that involves a component responsible for assigning courses to instructors. This specific component utilizes reactive forms, but it has hit a snag related to form controls. The issue arises when the form dynami ...

The query fails to retrieve results for the specified date and the beginning of the month

I have encountered an issue with my query that is supposed to fetch values between the start and end of the month. Interestingly, when a record is entered on the first day of the month, it doesn't get returned in the query result. Only records entered ...

Obtaining specific data from the forEach iteration, post-click event with JavaScript

Trying to access the value from a Tree-structured Array of Objects stored in Local Storage, Created a function that retrieves values from local storage using forEach and displays them on the screen. Clicking on the Yes/No button triggers the same functio ...

I'm encountering an issue with the dropify plugin where the data-allowed-file-

While using the dropify library to style file upload elements in my form, I ran into an issue with the data-allowed-file-extensions parameter not working as expected. Despite specifying allowed file extensions like "png jpg jpeg", the validation for input ...

Difficulty reloading after updating input using AngularJS ngTable

I'm encountering an issue where I need to utilize ngTable with TableParams to load and modify a table. For instance, when the page initializes, entering a number in the textbox for the first time displays the appropriate number of rows in the table. ...

Is there a way to convert const files without using TranslocoService for importing?

Introduction Greetings! I am currently working on an Angular+Ionic project and utilizing Transloco for text translation. The issue at hand I have a constants file with strings that need to be translated, but I am facing a challenge in figuring out how to ...

Is there a way to dynamically adjust the font size to perfectly fit the content within a div?

There is a division element containing text: <div style="white-space:nowrap;overflow:none;width:50px;"> With some text in it </div> Is there a way to adjust the font size of the text within the division so that all of the content is display ...

Experiencing a PHP Contact Form Problem - Unable to Identify the 400 Error Cause

Greetings! I'm fairly new to all this website creation stuff and I'm currently working on setting up a contact form from a template that I stumbled upon. Despite my best efforts in configuring the PHP code, I keep encountering a 400 response erro ...

What steps are needed to integrate a Spring Boot application with Angular 2?

After incorporating Angular 2 into my Spring Boot application, I successfully deployed all of my files. However, the routing feature is not working as expected. Below is the file structure. This setup works smoothly with a Node.js server. ...

Using a personalized font in CSS isn't functioning correctly and is displaying Times New Roman instead

I've been struggling to include a custom font in my project, but it's not working as expected. Here's how I added it to the .css file: @font-face { font-family: 'Open Sans'; src: url('/open-sans.eot'); src: u ...

Can you explain the process of retrieving data from a Material 2 table?

In my Angular 2 application, I am utilizing a Material 2 table to showcase items and their corresponding data. Each row in the table has an edit button on the right side, which triggers the appearance of input fields populated with the values of that speci ...

Switch background color multiple times on click using JavaScript loop

Hello amazing people! I have a container with 52 small boxes and one large box containing a letter. The smaller boxes are arranged around the larger one using CSS grid, and when hovered over, they turn light grey. My Objective When any of the 52 small b ...