ober Posted June 16, 2008 Share Posted June 16, 2008 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). Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/ Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566742 Share on other sites More sharing options...
corbin Posted June 16, 2008 Share Posted June 16, 2008 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 */ Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566752 Share on other sites More sharing options...
GingerRobot Posted June 16, 2008 Share Posted June 16, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566764 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 lol maybe if you didn't have people constantly distracting you at work you woulda figured it out on your own Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566766 Share on other sites More sharing options...
corbin Posted June 16, 2008 Share Posted June 16, 2008 Who was that aimed at? Me? Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566769 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 no, ober Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566772 Share on other sites More sharing options...
corbin Posted June 16, 2008 Share Posted June 16, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566774 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566778 Share on other sites More sharing options...
corbin Posted June 16, 2008 Share Posted June 16, 2008 Ahhh that's what I figured right after I posted my post.... Slow/paranoid moment.... Edit: Sorry my last two posts have accomplished absolutely nothing.... OT ftl. Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566788 Share on other sites More sharing options...
ober Posted June 16, 2008 Author Share Posted June 16, 2008 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566798 Share on other sites More sharing options...
.josh Posted June 16, 2008 Share Posted June 16, 2008 shady. Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566803 Share on other sites More sharing options...
Barand Posted June 16, 2008 Share Posted June 16, 2008 I hate people who double-post Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566805 Share on other sites More sharing options...
ober Posted June 17, 2008 Author Share Posted June 17, 2008 At least it wasn't on the same forums. Quote Link to comment https://forums.phpfreaks.com/topic/110454-solved-scaling/#findComment-566922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.