This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Custom javascript function change color of svg icon
#1
Hi,

I would like to change the color of the svg icon in visualisation based on it's value.
On the internet I found that the css attribute 'fill' should do the job but it doesn't.
The script itself works, if I alter it to change the background of the 'icon' element, it changes color accordingly.
Code:
icon.css('background-color', color);

How can I make it changes the color of the svg?

Code:
$(function() {
  $('.bgcolor-by-value').each(function(i, el) {
    var $el = $(el), addr = $(el).data('object'), icon = $el.find('.icon');
    if (addr) {
      grp.listen(addr, function(obj) {
      console.log(icon.get(0), obj.value);
        var value = obj.value
          , color = 'green'; // default color

        if (value >= 3 && value <= 4) {
          color = 'orange';
        }
        else if (value >= 5) {
          color = 'red';
        }

        icon.css('background-color', color);
      });
    }
  });
});

Thanks!
Reply
#2
Have you considered using Additional icons for this? You can specify a value range and assign a specific icon to this range. It's also possible to change the whole icon hue which might work in certain cases: https://forum.logicmachine.net/showthrea...0#pid12060
Reply
#3
(29.12.2021, 13:55)admin Wrote: Have you considered using Additional icons for this? You can specify a value range and assign a specific icon to this range. It's also possible to change the whole icon hue which might work in certain cases: https://forum.logicmachine.net/showthrea...0#pid12060

Thanks for the tip. Additional icons is indeed an option, I've opted for the 'filter' attribute though.

I made my svg grey 40%, this seems to work for changing to green, orange, red,..
Additionally the text color changes too with the code below.
Converting hex color to filter css color can be done here: https://codepen.io/sosuke/pen/Pjoqqp

Code:
$(function() {
  $('.iconcolor-by-value').each(function(i, el) {
    var $el = $(el), addr = $(el).data('object'), icon = $el.find('.icon');
    if (addr) {
      grp.listen(addr, function(obj) {
      console.log(icon.get(0), obj.value);
        var value = obj.value
          , color = 'green'; // default color
            filter = 'invert(55%) sepia(95%) saturate(2530%) hue-rotate(82deg) brightness(108%) contrast(129%)';

        if (value >= 3 && value <= 4) {
          color = 'orange';
          filter = 'invert(68%) sepia(55%) saturate(5908%) hue-rotate(359deg) brightness(98%) contrast(100%)';
        }
        else if (value >= 5) {
          color = 'red';
          filter = 'invert(16%) sepia(100%) saturate(7486%) hue-rotate(15deg) brightness(104%) contrast(121%)';
        }

       // icon.css('background-color', color);
        $(el).css('color', color);
        icon.css('filter', filter);
      });
    }
  });
});
Reply


Forum Jump: