Related
I have a PHP pie chart which uses data from Mysql to show the chart. However if one data is missing the whole chart turns to one color. For example for grading if the input is A, B, D and F(pay attention C grade input is missing) then the whole pie chart is in one color like Orange or red.
Can you please help me with this? Thanks
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('0000ff', '006600', 'ffff00','DD7C1D', 'FF3300', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = array_filter(explode('*',$data));
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
I'm trying to calculate EMI as per the attached screenshot. but there is a minor difference in my calculation. what I have done so far as below:
function interest($investment,$year,$rate=15,$n=1){
global $total_result, $total_interest, $totalamount;
$accumulated=0;
if ($year > 1){
$accumulated=interest($investment,$year-1,$rate,$n);
}
$accumulated += $investment;
$rateC = $rate / 100;
$result = $rateC / 12 * pow(1 + ($rateC) / 12, ($year * 12)) / (pow(1 + ($rateC) / 12, ($year * 12)) - 1) * $accumulated;
$result = 85.60;
for ($i=0; $i < 12; $i++) {
// echo round($accumulated,2).'<br>';
$accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
$innntrest = ($accumulated - $investment) / 12;
$i_result = $result - $innntrest;
$accumulated = $investment - $i_result;
$investment = $accumulated;
echo '<br>***'.round($result,2).'***'.round($i_result,2).'***'.round($innntrest,2).'<br>';
$total_result = $total_result + round($result,2);
$total_interest = $total_interest + $i_result;
$totalamount = $totalamount + $innntrest;
}
return $accumulated;
}
Where $investment=1000(loan amount), $year=1 (loan term), $rate=5%(interest rate), $n=4 (compound)
So I want exact output as an attached screenshot, so can anyone help me with this code to find out the formula? Thanks in advance!
After a few days of research, I got the solution as below:
function interest($investment,$year,$rate=15,$n=1,$payment_frequency=4){
$accumulated=0;
$accumulated += $investment;
$rateC = $rate / 100;
$total_i = 0;
if($payment_frequency == 1) {
$accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
} else {
$rate = pow(1 + ($rateC/$n),($n*($year/($year*$payment_frequency)))) - 1;
$result = ($rate*$accumulated)/(1 - pow((1 + $rate),-($year*$payment_frequency)));
for ($i=0; $i < ($year*$payment_frequency); $i++) {
$accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
$innntrest = ($accumulated - $investment) * 100;
$i_result = $result - $innntrest;
$accumulated = $investment - $i_result;
$investment = $accumulated;
$total_i += $innntrest;
}
return $total_i;
}
}
I'm using the XIRR function from the php financial library (http://www.phpclasses.org/package/892-PHP-Financial-functions-with-the-Excel-function-names-.html) but I get strange results with these values (dates are d/m/y):
(01/01/2014, -400) , (01/10/2014, 18)
MS Excel correctly returns 0.98, while the XIRR function returns -1.5714653207915E+40. The code is as follow:
$f->XIRR(array(-400,18), array(
mktime(0,0,0,1,1,2014),
mktime(0,0,0,10,1,2014),
), 0.1);
Can anyone explain me what am I doing wrong? Thanks in advance for any help.
I have written one code from PHP Excel Functions.
I have calculated XIRR and XNPV.
Here is code sample with some dummy data.
Main advantage is that, There is no dependency on any library in this
code.
<?php
$rate = 0.12;
$values = array(-5000,-3000,-8000,25000,-4000);
$dates = array('01-02-2015','05-05-2016','02-03-2018','03-03-2019','05-03-2019');
/** FINANCIAL_MAX_ITERATIONS */
define('FINANCIAL_MAX_ITERATIONS', 128);
/** FINANCIAL_PRECISION */
define('FINANCIAL_PRECISION', 1.0e-08);
$result = XIRR($values,$dates,0.1);
print_r($result);
function XIRR($values, $dates, $guess = 0.1) {
$x1 = 0.0;
$x2 = $guess;
$f1 = XNPV($x1, $values, $dates);
$f2 = XNPV($x2, $values, $dates);
for ($i = 0; $i < 128; ++$i) {
if (($f1 * $f2) < 0.0) break;
if (abs($f1) < abs($f2)) {
$f1 = XNPV($x1 += 1.6 * ($x1 - $x2), $values, $dates);
} else {
$f2 = XNPV($x2 += 1.6 * ($x2 - $x1), $values, $dates);
}
}
$f = XNPV($x1, $values, $dates);
if ($f < 0.0) {
$rtb = $x1;
$dx = $x2 - $x1;
} else {
$rtb = $x2;
$dx = $x1 - $x2;
}
for ($i = 0; $i < FINANCIAL_MAX_ITERATIONS; ++$i) {
$dx *= 0.5;
$x_mid = $rtb + $dx;
$f_mid = XNPV($x_mid, $values, $dates);
if ($f_mid <= 0.0) $rtb = $x_mid;
if ((abs($f_mid) < FINANCIAL_PRECISION) || (abs($dx) < FINANCIAL_PRECISION)) return $x_mid;
}
}
function XNPV($rate, $values, $dates) {
$valCount = count($values);
$xnpv = 0.0;
for ($i = 0; $i < $valCount; ++$i)
{
$datediff = strtotime($dates[$i]) - strtotime($dates[0]);
$datediff = round($datediff / (60 * 60 * 24));
$xnpv += $values[$i] / pow(1 + $rate,$datediff / 365);
}
return $xnpv;
}
?>
The correct XIRR value is -98.417% as shown below
-400 + 18(1+i)^-(273/365) = 0
18(1+i)^-(273/365) = 400
(1+i)^-(273/365) = 400/18
(1+i)^(273/365) = 18/400
(1+i) = (18/400)^(365/273)
1+i = (0.045)^(1.336996337)
i = (0.045)^(1.336996337) - 1
i = -0.984174769
i = -98.417%
Use the American mm/dd/yyyy :
mktime(0,0,0,1,1,2014),
mktime(0,0,0,1,10,2014)
I have this simple pricing table. I'd want to insert another 2 columns displaying MIN and MAX values from column $przoff.
Here's a screenshot that shows how columns should look like:
<?php
$i=0;
while ($i < $num) {
$cod=mysql_result($result,$i,"cod_int");
$name=mysql_result($result,$i,"nome");
$conf=mysql_result($result,$i,"conf");
$type=mysql_result($result,$i,"tipo");
$cconf=mysql_result($result,$i,"cost");
$cunit=mysql_result($result,$i,"costo_prod");
$incqta=mysql_result($result,$i,"incid_qta");
$inconf=($cconf*$incqta/100)+($cunit*$incqta/100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd=$cunit*10/100; }
elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd=$cunit*5/100; }
else {
$incprd=0; }
$przoff=($cconf+$cunit+$inconf+$incprd);
?>
<tr>
/* TABLE CONTENT */
</tr>
<?php $i++; } ?>
Find the min and max value before printing the table with same process. Then print in table.
Process 1:
<?php
$i = 0;
while ($i < $num) {
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
if ($i == 0) {
$min_val = $przoff;
$max_val = $przoff;
} else {
if ($przoff < $min_val) {
$min_val = $przoff;
}
if ($przoff > $max_val) {
$max_val = $przoff;
}
}
?>
<?php
$i++;
}
$i = 0;
while ($i < $num) {
$cod = mysql_result($result, $i, "cod_int");
$name = mysql_result($result, $i, "nome");
$conf = mysql_result($result, $i, "conf");
$type = mysql_result($result, $i, "tipo");
$cconf = mysql_result($result, $i, "cost");
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
$min_val = $min_val;
$max_val = $max_val;
?>
<tr>
/* TABLE CONTENT */
</tr>
<?php
$i++;
}
?>
Process 2:
<?php
$min_val = "";
$max_val = "";
$i = 0;
while ($i < $num) {
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
if ($przoff < $min_val || empty($min_val)) {
$min_val = $przoff;
}
if ($przoff > $max_val || empty($max_val)) {
$max_val = $przoff;
}
?>
<?php
$i++;
}
$i = 0;
while ($i < $num) {
$cod = mysql_result($result, $i, "cod_int");
$name = mysql_result($result, $i, "nome");
$conf = mysql_result($result, $i, "conf");
$type = mysql_result($result, $i, "tipo");
$cconf = mysql_result($result, $i, "cost");
$cunit = mysql_result($result, $i, "costo_prod");
$incqta = mysql_result($result, $i, "incid_qta");
$inconf = ($cconf * $incqta / 100) + ($cunit * $incqta / 100);
if ($cunit > 0 && $cunit <= 0.2500) {
$incprd = $cunit * 10 / 100;
} elseif ($cunit > 0.2500 && $cunit <= 0.5000) {
$incprd = $cunit * 5 / 100;
} else {
$incprd = 0;
}
$przoff = ($cconf + $cunit + $inconf + $incprd);
$min_val = $min_val;
$max_val = $max_val;
?>
<tr>
/* TABLE CONTENT */
</tr>
<?php
$i++;
}
?>
Only difference is min value and max value initializing.
I want to create piechart in my pdf file created using fpdf. already i had created pdf with fpdf . then i want to create pie chart in that using same table data, is there any option to create pie chart using fpdf ?
Please Help.
Thanks in advance
Try this make changes in code as per your requirements:
You can display following view file on your pdf using pdf helper.
you can use dom pdf helper download it from following link.
http://code.google.com/p/dompdf/downloads/detail?name=dompdf_0-6-0_beta3.zip
<?php
$show_label = true; // true = show label, false = don't show label.
$show_percent = true; // true = show percentage, false = don't show percentage.
$show_text = true; // true = show text, false = don't show text.
$show_parts = false; // true = show parts, false = don't show parts.
$label_form = 'square'; // 'square' or 'round' label.
$width = 199;
$background_color = 'FFFFFF'; // background-color of the chart...
$text_color = '000000'; // text-color.
$colors = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
$shadow_height = 16; // Height on shadown.
$shadow_dark = true; // true = darker shadow, false = lighter shadow...
// DON'T CHANGE ANYTHING BELOW THIS LINE...
$data = $_GET["data"];
$label = $_GET["label"];
$height = $width/2;
$data = explode('*',$data);
if ($label != '') $label = explode('*',$label);
for ($i = 0; $i < count($label); $i++)
{
if ($data[$i]/array_sum($data) < 0.1) $number[$i] = ' '.number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
else $number[$i] = number_format(($data[$i]/array_sum($data))*100,1,',','.').'%';
if (strlen($label[$i]) > $text_length) $text_length = strlen($label[$i]);
}
if (is_array($label))
{
$antal_label = count($label);
$xtra = (5+15*$antal_label)-($height+ceil($shadow_height));
if ($xtra > 0) $xtra_height = (5+15*$antal_label)-($height+ceil($shadow_height));
$xtra_width = 5;
if ($show_label) $xtra_width += 20;
if ($show_percent) $xtra_width += 45;
if ($show_text) $xtra_width += $text_length*8;
if ($show_parts) $xtra_width += 35;
}
$img = ImageCreateTrueColor($width+$xtra_width, $height+ceil($shadow_height)+$xtra_height);
ImageFill($img, 0, 0, colorHex($img, $background_color));
foreach ($colors as $colorkode)
{
$fill_color[] = colorHex($img, $colorkode);
$shadow_color[] = colorHexshadow($img, $colorkode, $shadow_dark);
}
$label_place = 5;
if (is_array($label))
{
for ($i = 0; $i < count($label); $i++)
{
if ($label_form == 'round' && $show_label && $data[$i] > 0)
{
imagefilledellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $colors[$i % count($colors)]));
imageellipse($img,$width+11,$label_place+5,10,10,colorHex($img, $text_color));
}
else if ($label_form == 'square' && $show_label && $data[$i] > 0)
{
imagefilledrectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $colors[$i % count($colors)]));
imagerectangle($img,$width+6,$label_place,$width+16,$label_place+10,colorHex($img, $text_color));
}
if ($data[$i] > 0)
{
if ($show_percent) $label_output = $number[$i].' ';
if ($show_text) $label_output = $label_output.$label[$i].' ';
if ($show_parts) $label_output = $label_output.$data[$i];
imagestring($img,'2',$width+20,$label_place,$label_output,colorHex($img, $text_color));
$label_output = '';
$label_place = $label_place + 15;
}
}
}
$centerX = round($width/2);
$centerY = round($height/2);
$diameterX = $width-4;
$diameterY = $height-4;
$data_sum = array_sum($data);
$start = 270;
for ($i = 0; $i < count($data); $i++)
{
$value += $data[$i];
$end = ceil(($value/$data_sum)*360) + 270;
$slice[] = array($start, $end, $shadow_color[$value_counter % count($shadow_color)], $fill_color[$value_counter % count($fill_color)]);
$start = $end;
$value_counter++;
}
for ($i=$centerY+$shadow_height; $i>$centerY; $i--)
{
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $i, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][2], IMG_ARC_PIE);
}
}
for ($j = 0; $j < count($slice); $j++)
{
if ($slice[$j][0] != $slice[$j][1]) ImageFilledArc($img, $centerX, $centerY, $diameterX, $diameterY, $slice[$j][0], $slice[$j][1], $slice[$j][3], IMG_ARC_PIE);
}
OutputImage($img);
ImageDestroy($img);
function colorHex($img, $HexColorString)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
return ImageColorAllocate($img, $R, $G, $B);
}
function colorHexshadow($img, $HexColorString, $mork)
{
$R = hexdec(substr($HexColorString, 0, 2));
$G = hexdec(substr($HexColorString, 2, 2));
$B = hexdec(substr($HexColorString, 4, 2));
if ($mork)
{
($R > 99) ? $R -= 100 : $R = 0;
($G > 99) ? $G -= 100 : $G = 0;
($B > 99) ? $B -= 100 : $B = 0;
}
else
{
($R < 220) ? $R += 35 : $R = 255;
($G < 220) ? $G += 35 : $G = 255;
($B < 220) ? $B += 35 : $B = 255;
}
return ImageColorAllocate($img, $R, $G, $B);
}
function OutputImage($img)
{
header('Content-type: image/jpg');
ImageJPEG($img,NULL,100);
}
?>
Hope this will help you... :)