DaveZ
DaveZ

Reputation: 23

Percentage of a set of numbers

I’ve been asked to make a chart that displays how many users are on each page. This chart will be color-coded, where the page with the most users will have 100% opacity, the page with the least viewers will have 40% opacity and the pages in between will have a corresponding opacity.

So, I have a set of numbers, I need

The numbers can be any whole number 1 or greater.

So given 1000, 500, 100, 50

Or, given 2, 1

Or given 1000, 1

Or given 7485, 395, 3

I hope that makes sense.

What equation can I use to solve this?

I know to get the percent is just (# / largest_number) * 100 but I’m lost trying to get it between 100% and 40%

The closest I could get is ((#/largest_number) * 60) + 40, but that assumes 0 is the smallest number and gives me 43 for my smallest number in the 1st set of numbers (50) instead of 40% like I need.

Thanks in advance!

Upvotes: 2

Views: 44

Answers (1)

hbejgel
hbejgel

Reputation: 4837

Instead of getting the percent with (# / largest_number) * 100 do x = ((# - lowest_number) / (largest_number - lowest_number) * 100

This is a basic normalization function - see here for details.

This way largest number will always map to 1 and lowest_number will always map to 0, then you can interpolate using your function (x * 60) + 40

Upvotes: 2

Related Questions