Hi Folks,
I am sort of new to php, but not programming in general. I need some help though for creating a dynamic image.
For starters, see attached image. The image depicts 6 people's power usage. (I'll get it out the way: I would live the freehand look to it but don't mind rectangles to start with)
Say you have 6 people comparing their energy usage. If they all using 1000W, the values would all be the same. If one is using 2000W, his block would be considerably larger than the others. (Colour changing is optional and the simplest part). If I am comparing 13 people, it must do the same. Pretty straight forward.
Working with simple rectangles:
Now, what I have at the moment is:
[*]a container image, 600x300
[*]a user class which has two properties, usage and username.
[*]an array holding 6 of the above classes.
users:
$users = Array();
$users[0] = new user("Fred", 1200);
$users[1] = new user("Bob", 1300);
$users[2] = new user("Ben", 1400);
$users[3] = new user("Mik", 2200);
$users[4] = new user("AAA", 3000);
$users[5] = new user("BBB", 4000);
The direction I am heading is, find the user who has a usage which is 1/3 in the sorted list. (Bob)
Then, 600/3=200 (3 blocks per row). Get the user's usage factor. 1300/200=0.15384. This will then be used to work out the width of the other user's width.
Draw a rectangle with 0, 0, 200, 150 (x, y, x2, y2)
Remove user from array.
Now loop over array, pick out first user. (fred)
x=200, x2 = (x + (u.usage * fac)) which is 200 + 184 = 384
draw rectangle of 200, 0, 384, 150.
next user: (ben)
x=x2 (384)
x2 = (x + (u.usage * fac)) which is 384 + 215 = 599
count = 3, set y = 150, x2=0 (new row)
next user: (mik)
x= x2 (0)
x2 = (x + (u.usage * fac)) which is 0 + 338 = 338
next user: (AAA)
x= x2 (338)
x2 = (x + (u.usage * fac)) which is 338 + 416 = 754 (BANG!)
See my problem. I have overshot the total width of 600.
My question is two fold: How do I do the maths for working out the size of the rectangles, and how do I decide on a starting size.
I cannot use the largest as it will then be out of proportion.
I'm pretty screwed on it. Thinking I might have to think of a different method.
The place this is being used is for electricity monitoring. http://www.myenergyusage.org
TIA
Cheers,
Crispin
Help with some Image stuff and (hangs head) maths.
Moderator: General Moderators
-
CrispinProctor
- Forum Newbie
- Posts: 1
- Joined: Sat May 23, 2009 12:17 pm
Help with some Image stuff and (hangs head) maths.
- Attachments
-
- Usage.jpg (23.7 KiB) Viewed 64 times