Jump to content

Recommended Posts

I'm having a mental math block, so I need a little help:

 

I'm trying to use Google's chart API (http://code.google.com/apis/chart) to generate a chart of the US based on order numbers (to show where most of the orders are coming from).

 

The way the API works is that you pass a number on a scale from 0-100 and that number corresponds to a listing of states.  Depending on the number, it colors the state based on the scale of 0-100 (right now for me dark green = 100, pale green = 0 and it interpolates between).

 

So I'm basically calculating a percentage (state total / country total) * 100.  What I then also want to do is put those numbers on a 0-100 scale so that the highest percentage = 100 and the lowest = 0 (or slightly above 0.

 

Anyone know what I should do here?

 

By the way, the percentages are already going to be in order because I order by the most orders per state descending.  So the highest percentage in my array is going to be element 0 and the lowest will be count(array).

Link to comment
https://forums.phpfreaks.com/topic/110454-solved-scaling/
Share on other sites

Is this what you mean?

 

<?php

   $maxpercent = 46; //  46% is the new 100%
   $percent = 23; // 23 would be 50% of 46

   // scale of 1-100, 23 should be 50
   // since 46 "equals" 100, we find out what it takes to make 46 turn
   // into 100, so divide 100 by 46.  then we just take that number
   // and multiply it with $percent to scale it up
   $pos = round((100 / $maxpercent) * $percent); 

   echo $pos;
?>

 

Link to comment
https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566742
Share on other sites

Edit: Blerh Crayon Violent beat me to it (I had to stop and test everything hehe), but here it is anyway.

Redit:  OMG beat me by 9 minutes....  lol

 

 

 

OK... so for state/country....  You basically want to change that data set to be scaled to 100?

 

So, let's say the highest state/country ratio is x, you would want that to be 100?

 

So, couldn't you do...

 

h = highest ratio

o = 100/h      This is the number you would multiply the highest percentage by to make it 100....

So, then for each rate as r, you would do:

googlenumber = ro;

 

But, then the lowest number won't be 0 (unless there's 0 sales in some state....)

 

<?php

//let's say there's a thousand orders nation wide

$orders = array(
200/1000, //this should be 100
100/1000,  //this should be 50
50/1000,  //this should be 25
35/1000, //this should be 0
);

$h = $orders[0];
$o = 100/$h;

foreach($orders as $rate) {
echo ($rate*$o) . PHP_EOL;
}
/*
100
50
25
17.5
*/

Link to comment
https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566752
Share on other sites

Hmm, i interpreted it a bit differently. I thought you wanted to have the bottom value of your set as 0, and the top as 100. Ill post what i had in case that was what you were after.

 

The maths is pretty much the same; you have to work out what 1% is 'worth' by dividing the range by 100. You then take the minimum value off of each, then add what 1% is * the value:

 

<?php
$array = array('AL' => 5, 'AK'=> 8, 'AZ' => 10);
arsort($array);
$total = array_sum($array);
$percents = array();
foreach($array as $k=> $v){
    $percents[$k] = $v/$total*100;
}
function scale($array){
    $max = max($array);
    $min = min($array);
    $range = $max - $min;
    $percent = 100/$range;
    $scaled = array();
    foreach($array as $k=>$v){
        $scaled[$k] = ($v-$min)*$percent;
    }
    return $scaled;
}
$scaled = scale($percents);
print_r($scaled);
?>

Link to comment
https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566764
Share on other sites

Oh, OK cool....  I thought that you were implying that I copied your post (I don't even work, so I didn't think it was aimed at me, but I guess I'm paranoid) ;p.  I tried to figure out GingerRobot's way for a while, and then I gave up lol...

 

Edit: added the part in parentheses.

Link to comment
https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566774
Share on other sites

Hmm, i interpreted it a bit differently. I thought you wanted to have the bottom value of your set as 0, and the top as 100. Ill post what i had in case that was what you were after.

 

The maths is pretty much the same; you have to work out what 1% is 'worth' by dividing the range by 100. You then take the minimum value off of each, then add what 1% is * the value:

 

well in my code,  if you make

 

$percent = 0; // 0%..no sales..

 

then it will return 0 on the scale. Or hell, even if you use a bit more than 0%, I do have a round wrapped around the equation, so you still might get 0 returned (for some odd reason I assumed he wanted whole numbers).  I think he might wanna take out the round, because that might hurt someone's feelings if you say they are a zero when they sold something.  But other than that, the equation/return is free to show .00000...1 out of 100. 

 

Oh, OK cool....  I thought that you were implying that I copied your post (I don't even work, so I didn't think it was aimed at me, but I guess I'm paranoid) ;p.  I tried to figure out GingerRobot's way for a while, and then I gave up lol...

 

Edit: added the part in parentheses.

 

lol nah I was referencing his bitching about some girl nagging him at work

Link to comment
https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566778
Share on other sites

Well, you guys took like way too long.  I posted this on a non PHP board that I run and they had the answer in a few minutes:

 

<?php
$st = "";
$vals = array();
$i = 0;
while($row = mysql_fetch_array($result))
{
	$st .= strtoupper($row['state']);
	if($i == 0)
		$factor = 100 / (($row['cnt'] / $totalstate) * 100);
	$vals[] = number_format($factor * (($row['cnt'] / $totalstate) * 100), 2);
	$i++;
}
$vals = implode(",", $vals);
?>

Link to comment
https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566798
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.