javascripthtmlcssgauge

Changing color based on gauge value - Javascript


I'm trying to write a Javascript function to go alongside a gauge that I copied online, the gauge works like this:

const gaugeElement = document.querySelector(".gauge");

function setGaugeValue(gauge, value) {
  if (value < 0 || value > 1) {
    return;
  }

  gauge.querySelector(".gauge__fill").style.transform = `rotate(${value / 2}turn)`;
  gauge.querySelector(".gauge__cover").textContent = `${Math.round(value * 100)}%`;

}

setGaugeValue(gaugeElement, 0.04);
.gauge {
    width: 100%;
    max-width: 250px;
    font-family: 'Lato', SansSerif;
    font-size: 32px;

}

.gauge__body {
    width: 100%;
    height: 0;
    padding-bottom: 50%;
    background: #b4c0be;
    position: relative;
    left: 35px;
    border-top-left-radius: 100% 200%;
    border-top-right-radius: 100% 200%;
    overflow: hidden;

}

.gauge__fill {
    position: absolute;
    top: 100%;
    left: 0;
    width: inherit;
    height: 100%;
    background-color: #5bb62a;
    transform-origin: center top;
    transform: rotate(0.25turn);
    transition: transform 0.2s ease-out;
}

.gauge__cover {
    width: 75%;
    height: 150%;
    background: #f5f6fa;
    position: absolute;
    border-radius: 50%;
    top: 25%;
    left: 50%;
    transform: translateX(-50%);

    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 25%;
    box-sizing: border-box;

}
<div class="card">
  <div class="FuelLevelText">
    <h3>Fuel Level</h3>
  </div>
  <div class="gauge">
    <div class="gauge__body">
      <div id="gaugecolor" class="gauge__fill"></div>
      <div class="gauge__cover"></div>
    </div>
  </div>
</div>

There's also a bit of CSS that goes with this

This works fine to change the gaugeElement value and will change the percentage on screen, however I'd like to add a rule so if the value goes below 0.05 or 5% the gauge fill background-color turns red to indicate it's running low. I'm very very new to coding so I'm struggling to find a resource online that has something similar to what I need that I can adapt to my needs.

I tried adding a line:

if (gaugeElement, '< 0.05') {
    function changebackground(color) {
        #gaugecolor = color;
    }

    changebackground('red');
}

However I don't know enough Javascript to know if this is even targeting the right section?


Solution

  • If the value goes below 0.05 or 5% the gauge fill background-color turns red.

    
    const gaugeElement = document.querySelector(".gauge");
    
    function setGaugeValue(gauge, value) {
      if (value < 0 || value > 1) {
        return;
      }
    
      gauge.querySelector(".gauge__fill").style.transform = `rotate(${value / 2}turn)`;
      gauge.querySelector(".gauge__cover").textContent = `${Math.round(value * 100)}%`;
    
      if (value < 0.05) {
        gauge.querySelector(".gauge__fill").style.backgroundColor = 'red';
      } else {
        gauge.querySelector(".gauge__fill").style.backgroundColor = ''; // Reset the background color
      }
    }
    
    setGaugeValue(gaugeElement, 0.26);