ultrus Posted August 12, 2007 Share Posted August 12, 2007 Hello all, I'm on a mission to dynamically generate stock quote line graphs. Stock quote graphs are usually very dramatic looking, even if the quote price changes slightly throughout the day. This is because the bottom label on the vertical side of the graph starts at an even increment just below or even with the lowest stock price of the day, instead of 0. I'm getting close to making this happen for my project, but am not there yet. There may also be a better way to do what I'm doing? Sometimes my values fit within my labels. Sometimes my lowest value falls below my lowest label, which would make the line in my graph go below the chart. I'm sure that would make a bad impression on my client's investors. heh. The below script will output a good list of labels that my stock prices fit into nicely (for now): <?php //this time, I want 5 labels on the side of the graph $verticalLabelNum = 5; //stock prices throughout the day $stockValues = array(6.0,3.25,3.22,3.25,3.21,3.22); //get stock value range, and determine vertical increments $sortStock = $stockValues; sort($sortStock); $lowestStock = $sortStock[0]; rsort($sortStock); $highestStock = $sortStock[0]; $stockRange = $highestStock - $lowestStock; echo "highest=$highestStock<br>lowest=$lowestStock<br>range=$stockRange<br>"; //decide vertical increments that are easy to understand //there may be a better way to do this? $incArray = array(.01,.02,.05,.1,.2,.25,.5,1,2,2.5,5,10,20,25,50,100); for($i=0; $i<count($incArray); $i++) { if($stockRange < $incArray[$i] * $verticalLabelNum) { //check if still in range of even intervals $lowRemain = $lowestStock % $incArray[$i]; //this does not work great echo "lowRemain=$lowRemain<br>"; $highRemain = $highestStock % $incArray[$i]; //this does not work great echo "highRemain=$highRemain<br>"; $stockRange += ($incArray[$i] * $lowRemain) + ($incArray[$i] * $highRemain); echo "modified range: $stockRange<br>"; if($stockRange < $incArray[$i] * $verticalLabelNum) { $stockInc = $incArray[$i]; break; } else { $stockInc = $incArray[$i + 1]; break; } } } echo "label increment=$stockInc<br><br>"; //create vertical values starting from 0, ending right after or at highest stock price $verticalValues = array(); $vertVal = 0; $done = false; while(!$done) { $verticalValues[] = $vertVal; if($vertVal>=$highestStock) { $done = true; } $vertVal += $stockInc; } //prep to write labels from bottom up $verticalValues = array_reverse($verticalValues); //display specified number of line graph labels for($i=0;$i<$verticalLabelNum;$i++) { echo "label: " . $verticalValues[$i] . "<br>"; } ?> prints this: highest=6 lowest=3.21 range=2.79 lowRemain=0 highRemain=0 modified range: 2.79 label increment=1 label: 6 label: 5 label: 4 label: 3 label: 2 Now, if I change the stock value array to be more subtle, <?php $stockValues = array(4.1,3.25,3.22,3.25,3.21,3.22); ?> I get this: highest=4.1 lowest=3.21 range=0.89 lowRemain= highRemain= modified range: 0.89 label increment=0.2 label: 4.2 label: 4 label: 3.8 label: 3.6 label: 3.4 3.4 is still to high for the lowest stock price of 3.21. $lowRemain and $highRemain are null, but if I force them to 1, the labels still are not right. Any ideas on how I can make this work? I'm so close!! I much appreciate the assist. Quote Link to comment https://forums.phpfreaks.com/topic/64526-solved-troubles-determining-label-increments-and-values-in-graph-not-starting-at-0/ Share on other sites More sharing options...
Barand Posted August 12, 2007 Share Posted August 12, 2007 <?php $stockValues = array(4.1,3.25,3.22,3.25,3.21,3.22); $maxY = ceil (max ($stockValues)); $minY = floor ( min ($stockValues)); Quote Link to comment https://forums.phpfreaks.com/topic/64526-solved-troubles-determining-label-increments-and-values-in-graph-not-starting-at-0/#findComment-321667 Share on other sites More sharing options...
ultrus Posted August 12, 2007 Author Share Posted August 12, 2007 wOW! That works great. I see some other areas now that I can reduce and improve as well. It's these simple things that drive me nuts. Thank you Barand for the direction. Quote Link to comment https://forums.phpfreaks.com/topic/64526-solved-troubles-determining-label-increments-and-values-in-graph-not-starting-at-0/#findComment-321674 Share on other sites More sharing options...
Barand Posted August 12, 2007 Share Posted August 12, 2007 That was probably too coarse. Try <?php $stockValues = array(4.1,3.25,3.22,3.25,3.21,3.22); $maxsv = max($stockValues); $maxY = ceil($maxsv/0.2 ) * 0.2; $minsv = min($stockValues) ; $minY = floor($minsv/0.2 ) * 0.2; echo $maxY, '<br>', $minY; ?> Quote Link to comment https://forums.phpfreaks.com/topic/64526-solved-troubles-determining-label-increments-and-values-in-graph-not-starting-at-0/#findComment-321675 Share on other sites More sharing options...
ultrus Posted August 13, 2007 Author Share Posted August 13, 2007 Ah, I see why you did that. Thanks again! This should work great. Quote Link to comment https://forums.phpfreaks.com/topic/64526-solved-troubles-determining-label-increments-and-values-in-graph-not-starting-at-0/#findComment-321962 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.