Jump to content

[SOLVED] troubles determining label increments and values in graph not starting at 0


Recommended Posts

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. :P

 

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. :P

 

Any ideas on how I can make this work? I'm so close!! I much appreciate the assist. :)

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;
?>

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.