Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic background colors according to numerical values

Context

I am looking for a way to make automatic background colors according to numerical values.

In my example, the colors vary from green to red depending on the percentages:

enter image description here

Question

I just need css or I also need javascript?

Example

You can use this markup: http://jsfiddle.net/3J3Yb/1/.

like image 846
GG. Avatar asked Dec 28 '25 16:12

GG.


2 Answers

Taking off from Joseph's solution:

Red is (255,0,0); yellow is (255,255,0); green is (0,255,0). So if you want your gradation to go through yellow, you need to gradually increase the green to 100% and then reduce the red to 0.

http://jsfiddle.net/mblase75/Xw3JL/1

$(".percentVals td").each(function(){
    var r = Math.min(100,parseInt(this.innerHTML,10)*2);
    var g = Math.min(100,200-parseInt(this.innerHTML,10)*2);
    this.style.backgroundColor = "rgb(" + r + "%," + g + "%,0%)"
});
like image 152
Blazemonger Avatar answered Dec 31 '25 06:12

Blazemonger


Do you mean something similar to this?

http://jsfiddle.net/Xw3JL/

Needs tweaking though

like image 26
Joseph Marikle Avatar answered Dec 31 '25 06:12

Joseph Marikle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!