0

Here is the conditional formatting feature in Microsoft Excel where the color bars represent the percentage in comparing to the highest value.

Conditional formatting in excel

Is there anyway to achieve this with HTML5 using either CSS or JavaScript? Or maybe using additional Angular library as well.

I'm thinking about using the clip-path to but this feature may not be widely supported on all browsers

1 Answer 1

1

Do you mean this?

You can do this simple in vanillaJS. You must get all elements

const elNodes = document.querySelectorAll('.element');
const elements = [...elNodes];

Get their values and find the max value

const elementsValues = elements.map((el) => parseFloat(el.children[1].textContent));
const maxValue = Math.max(...elementsValues);

Finally change the width of elements based on value:

elements.forEach((el) => {
    el.children[0].style.width = 100 / (maxValue / parseFloat(el.children[1].textContent)) + '%';
});
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to work as I expected in Chrome but in Edge it fails right at the line const elements = [...elNodes]; with an error SCRIPT5002: Function expected test.html (13,13) learn.microsoft.com/en-us/scripting/javascript/misc/…. What ... means in javascript? it's really hard to google ... :(
It's spread operator, that means when you have for example an array with values, the spread operator will extract them all. developer.mozilla.org/en/docs/Web/JavaScript/Reference/… This is new feature, added in ES6, so you can replace it with: const elements = Array.prototype.slice.call(elNodes); That do exactly the same thing in the result. The reason why you must do this is because we get a Node elements from a DOM, not an array, so you must convert them to use it like an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.