It is currently April 19th, 2024, 3:47 am

Histogram autoscaling function?

Discuss the use of Lua in Script measures.
FlyingHyrax
Posts: 232
Joined: July 1st, 2011, 1:32 am
Location: US

Histogram autoscaling function?

Post by FlyingHyrax »

Hello again, still working on this.

I want to add an autoscaling option for the histogram. It occurred to me to check Rainmeter's source and try to see how it handles autoscaling.

I found this (https://github.com/rainmeter/rainmeter/blob/master/Library/MeterHistogram.cpp line 357)

Code: Select all

	if (m_Autoscale)
{
// Go through all values and find the max

double newValue = 0.0;
for (int i = 0; i < maxSize; ++i)
{
newValue = max(newValue, m_PrimaryValues[i]);
}

// Scale the value up to nearest power of 2
if (newValue > DBL_MAX / 2.0)
{
m_MaxPrimaryValue = DBL_MAX;
}
else
{
m_MaxPrimaryValue = 2.0;
while (m_MaxPrimaryValue < newValue)
{
m_MaxPrimaryValue *= 2.0;
}
}
Finding the maximum value from the current set is straightforward enough; I've already got that working. But I'm not sure what's happening starting in line 368 from above... can someone with C++ chops help me understand this better? If I can see what is going on I think I can translate it over to my Lua script pretty well.

Thanks

Update
I got this working to my satisfaction. The C++ code was a bit of a red herring, though it did get me thinking along the right lines in the end. Here is my solution:

Code: Select all

if auto then
			if Values[i] == maxV then
				h = maxHeight
			else
				h  = math.ceil((Values[i]/maxV)*maxHeight)
			end
		else
			h = math.ceil(Values[i]*maxHeight)
		end
The Values table holds the relative (0.0 - 1.0, or percentage) values from my CPU measure. maxHeight is the tallest a bar in the histogram can be. h is what the 'H' key in the image meter will be set to. The entire if/else is inside an iterator that goes through all of my image meters for the histogram.

It looks to me like the first nested if/else (if Values[i] == maxV) is redundant; I don't see why I can't get rid of it and just have:

Code: Select all

if auto then
	h  = math.ceil((Values[i]/maxV)*maxHeight)
else
	h = math.ceil(Values[i]*maxHeight)
end
...but for whatever reason, if I try the above things go wonky.
Last edited by FlyingHyrax on January 10th, 2013, 9:02 pm, edited 3 times in total.
Flying Hyrax on DeviantArt
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Histogram autoscaling function?

Post by jsmorley »

User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Histogram autoscaling function?

Post by smurfier »

jsmorley wrote:This might help:

http://docs.rainmeter.net/snippets/autoscale
A more math centric version of the autoscale function, along with a lua centric round.

Code: Select all

function AutoScale(num, idp)
	local suffix = { 'B', 'kB', 'mB', 'gB', 'tB', 'pB', 'eB', 'zB', 'yB' }
	local scale = num == 0 and 1 or math.ceil(math.log(num)/(10*math.log(2)))
	return Round(num / 1024 ^ (scale-1), idp or 0), suffix[scale] or scale
end

function Round(num, idp)
	return tonumber(string.format('%.'..(idp or 0)..'f', num))
end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .