Modifying the color of a variety of distinct data points

There was a previous inquiry regarding Changing Colour of Specific Data, which can be found here: Changing colour of specific data

Building upon that question, I now have a new query. After successfully changing the 2017 dates to pink, I am seeking a way to display the 2018 dates in blue. How can I introduce a second date? Even better, is there a method to automatically alternate colors for each year without manual input? For now, I would appreciate guidance on adding a second year using the existing solution.

Json file:

[
    {
        "id": 1,
        "month": "2017-03-01"
    }
    {
        "id": 2,
        "month": "2017-04-01"
    }
    {
        "id": 3,
        "month": "2017-05-01"
    }
    {
        "id": 4,
        "month": "2017-06-01"
    }
    {
        "id": 5,
        "month": "2017-07-01"
    }
    {
        "id": 6,
        "month": "2017-08-01"
    }
    {
        "id": 7,
        "month": "2017-09-01"
    }
    {
        "id": 8,
        "month": "2017-10-01"
    }
    {
        "id": 9,
        "month": "2017-11-01"
    }
]

To address this, I considered utilizing a switch case, but my HTML currently looks like this:

<table>
  <tr>
    <th>Line 1</th>
    <th *ngFor="let volumes of volumes" [ngClass]="{ 'pink-color': checkYear(volumes.month) }">{{ volumes.month | uppercase }}</th>
  </tr>
</table>

Component.ts:

checkYear(date) {
        return new Date(date).getFullYear() == 2017 ? true : false;
}

CSS:

.pink-color {
    background-color: pink;
}

Answer №1

To dynamically apply classes based on the year, you can use a function within the ngClass directive.

Here is an example:

 [ngClass]="getClass(volumes.month)"

In your component:

getClass(date) {
  var year = new Date(date).getFullYear();
  if(year == 2017){
    return "pink-color";
  }
  if(year == 2018){
    return "blue-color";
  }
  /* Add more conditions for different years like yellow for 2019 */
  return "";// default class for other years
}

You can define the styles for the blue color class as follows:

.blue-color {
    background-color: blue;
}

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

Finding the element and extracting its text value

Below is the HTML code: <span> <b>Order number:</b> </span> <span>A36HASJH</span> The value within the span element, A36HASJH, is dynamic and changes with each order. How can I identify this element and store it as a s ...

Transforming a JSON object property value from an array into a string using JavaScript

I am facing an issue with an API call I am using, as it is sending objects with a single property that contains an array value (seen in the keys property in the response below). However, I need to work with nested arrays in order to utilize the outputted v ...

Is it possible to modify a scss style in Prestashop?

I have encountered an issue while using a default theme in Prestashop 1.6 that I need assistance with. My goal is to move the navbar up by 25px. I understand that I should make changes to #block_top_menu { padding-top: 25px; } in blocktopmenu.scss, which ...

Utilizing inline styles with the asset pipeline feature in Rails

<%= link_to root_path do %> <div class=""> <div style="background-image:<%= image_tag " image1.png "%>"> </div> <div> <h2>Title</h2> <p>Paragraph</p> </div& ...

Drag and Drop File Upload with Angular 2

I'm currently working on incorporating a drag and drop upload feature for angular 2, similar to the one found at: Given that I am using angular 2, my preference is to utilize typescript as opposed to jquery. After some research, I came across a libra ...

JavaScript will continue to process the submit to the server even after validation has been completed

My current challenge involves implementing form validation using JavaScript. The goal is to prevent the form from being sent to the server if any errors are found. I have made sure that my JavaScript function returns false in case of an error, like so: ...

A straightforward method to flatten and unflatten JSON files

I've managed to flatten JSON files effortlessly by executing this command in a combination of Node.js and Bash: npx flat foo.json > bar-flat-output.json. But now, I'm searching for another foolproof command that can easily undo the flattening ...

How to use CSS to dynamically adjust the maximum width of an overflow container based on its children's content

I have a container with an overflowing <div> that displays multiple <ul> <li> lists. Each list always contains the same number of bordered <li> items, resembling a table. However, there may be instances where a <ul> has only ...

executing a controller method in AngularJS

Looking to trigger a controller function from a directive tag. Check out this demo: https://jsfiddle.net/6qqfv61k/ When the 'Export to Excel' button is clicked, I need to call the dataToExport() function from appCtrl since the data is ready for ...

Troubleshoot Azure SignalR with a group of individuals (not testing connections on personal localhost)

I recently developed an Azure SignalR application that is currently running locally on https://localhost:12345/ within a C# web API project. Within my Angular application, I am consuming the Azure SignalR application. While debugging the SignalR code wit ...

The dropdown div does not activate when the image is clicked

When the image in the simple dropdown is clicked, it fails to activate the dropdown feature. This issue was encountered while working on a Shopify project, making it challenging to troubleshoot. To demonstrate the problem, I have re-created the scenario i ...

Is it possible to activate ionViewWill/DidEnter from a tab navigating to an external page in Ionic/Angular?

I am attempting to initiate an event upon returning to my tab page from a page outside of the routing module. 2. I have experimented with: Employing @ionic/angular NavController from both pages: From the tab page: this.navCtrl.navigateForward([&apos ...

Error: The system encountered an unexpected character (92) while parsing the JSON file [Java]

Apologies for the messy title, but I'm completely puzzled by this issue. I'm attempting to parse a JSON String using Jackson. The code is simple: import com.fasterxml.jackson.databind.ObjectMapper; import formatter.Tweet; import com.fasterxml.j ...

What's New with Material 3 in Angular 17?

Even though I've taken the time to read all of the documentation at https://github.com/material-components/material-web/blob/main/docs/quick-start.md, I'm still struggling to figure out how to incorporate this into my Angular 17 project. I went ...

The app is opening the index.html file instead of the expected index.jsx file

After creating a page with React.jsx, I encountered an issue where my page was initially opening index.jsx upon running npm start. However, when I deleted and then restored the index.html file to "public", the page started opening index.html instead. This ...

Binding data to custom components in Angular allows for a more flexible

In my current scenario, I am looking to pass a portion of a complex object to an Angular component. <app-component [set]="data.set"></app-component> I want the 'data.set' object in the parent class to always mirror the 'set&apo ...

Issue with AnimeJS Motion Path causing element to deviate from desired SVG path

I'm attempting to use an SVG element and the AnimeJS library to make the orange marker follow the course of this RC car race track. https://i.stack.imgur.com/8FKHC.png Despite my efforts, I am encountering strange and undesirable outcomes. At times ...

React Native: Struggling with Button Styling

I am relatively new to React Native, although I have experience with React in my professional work. I'm finding that applying styles to components in React Native is a bit different than what I'm used to. Specifically, I am struggling to apply s ...

Is it possible for me to tap into the component creation process within the Angular Router?

Inspiration struck me when I realized the potential of adding a directive to the component designed for this particular route. It would elevate the functionality by letting me convey crucial information in a more declarative manner. Despite learning that ...

Having trouble with the functionality of my JavaScript slider

After attempting to incorporate a slider into my website, I encountered an issue where the slider was not functioning as expected. The slider I tried to implement can be found here: https://codepen.io/amitasaurus/pen/OMbmPO The index of the site can be a ...