Is there a PHP trick to align a colum of numbers?

software development

#1

I remember doing this in the dark ages with Fortran. Check to see is the number is < 10, then add three spaces, if the number is >10 but < 100 add two spaces. But is there an easier way in to add text to a chart in PHP than this? Using a non-proportional font isn’t enough.

[php]#Add text to the plotarea
$c->addText(490,50,“Adipose clipped chinook this year: $ACchkAtotal”, “…/…/fonts/cour.ttf”, 10);
$c->addText(490,63,“Adipose clipped chinook jacks this year: $ACchkJtotal”, “…/…/fonts/cour.ttf”, 10);
$c->addText(490,76,“Adipose present chinook this year: $APchkAtotal”,"…/…/fonts/cour.ttf", 10);
$c->addText(490,89,“Adipose present chinook jacks this year: $APchkJtotal”, “…/…/fonts/cour.ttf”, 10);
$c->addText(490,102,“Adipose Unknown chinook this year: $APchkJtotal”, “…/…/fonts/cour.ttf”, 10);[/php]

A PHP table?


#2

I’d use html/css from within php to align right. I’ll stop at that tho, because there is more than one way to do that.


#3

I should have added that I’m placing this text inside a chart using ChartDirector php libraries from ASE Software.

http://ykfp.org/php/lyletrap/lylechinookurl092015share.php?year=2015


#4

Could use sprintf, e.g.

[php]
$text = sprintf(“Adipose clipped chinook this year: %10d”, $ACchkAtotal);
$c->addText($x, $y, $text, “…/…/fonts/cour.ttf”, 10);
[/php]

where the 10 in “%10d” is the width of the column.


#5

That’s something different, Andrew. But I guess I’d still have to count the characters of the first string, then estimate the number of digits in the number, then calculate the column width as a number variable to put between % and D. Maybe something like:
[php]function count_digit($number) {
return strlen((string) $number);
}
[/php]


#6

To be clear, using sprintf() to format the string, with a format specifier like “%10d”, will automatically insert spaces before the number as necessary to make that field take up a total of 10 characters.

I suppose you might need to do something like what you’re describing if you don’t know how large your numbers will be, and you don’t want to use any more spaces than absolutely necessary. That’s kind of unavoidable unless ChartDirector supports some kind of fancier layout than what you’re using here.


#7

I’m trying something like this now, calculating the number of spaces to add, but I can’t seem to get the substitution of the $spaces number back into the sprintf line.

[php]#Define function to count digits in a number
function count_digit($number) {
return strlen((string) $number);
}

#Add text to the plotarea
$lefttext = “Adipose clipped chinook this year:”;
$leftLen = strlen($lefttext);
$rightLen = count_digit($ACchkAtotal);
$spaces = 45 - $leftLen - $rightLen;
$text = sprintf($lefttext, “%”,$spaces,“d”, $ACchkAtotal);
#echo $text, $leftLen, $rightLen, $spaces;
echo “%”,$spaces,“d”;
$c->addText(490, 50, $text,"…/…/fonts/cour.ttf", 10);[/php]

It would be cool if I can get this to work, but I’m on the way to doubling the lines of code.


#8

Andrew pointed you in the right direction, but you would have to examine the documentation of sprintf() carefully in order to get it do the work you are trying to implement on your own.

$format = “%-40s%6d”;
$text = sprintf($format, $label, $quantity);

This would result in a string that is 46 characters wide with the label left-aligned and the quantity right-aligned. However if $quantity is more than 6 digits the string will be longer than 46 characters.


#9

I read up of sprintf enough to figure out the first argument in the format, but I still had trouble with substituting into the string a variable number of spaces.

I started bumbling around with str_pad, but couldn’t see any difference in $text at $echo, even when I tried specifying a space character other that space, like “o”.

[php]#Define function to count digits in a number
function count_digit($number) {
return strlen((string) $number);
}

#Add text to the plotarea
$lefttext = “Adipose clipped chinook this year:”;
$leftLen = strlen($lefttext);
$rightLen = count_digit($ACchkAtotal);
$spaces = 45 - $leftLen - $rightLen;
#$text = sprintf($lefttext, “%”,$spaces,“d”, $ACchkAtotal);
#$format =
#$text = sprintf($lefttext, “%10d”, $ACchkAtotal);
#$text = (str_pad($lefttext,$spaces), $ACchkAtotal);
#$text = str_pad($lefttext,intval($spaces),“o”);
$text = str_pad($lefttext, 8,“o”);
echo $text, $ACchkAtotal;
#echo $text, “I”;
#echo “%”,$spaces,“d”;[/php]

Update: I see my problem with str_pad. The second argument to str_pad() takes the full length of the final string, so I need to add the length of $lefttext to the length of the desired pad length.


#10

This is working for me, but I don’t understand why " " or " " or no argument didn’t work for me in the str_pad statement, either in the statement or as $padchar. Is it just that echo or print didn’t show the blanks?

[php]#Define function to count digits in a number
function count_digit($number) {
return strlen((string) $number);
}

#Add text to the plotarea
$lefttext = “Adipose clipped chinook this year:”;
$leftLen = strlen($lefttext);
$rightLen = count_digit($ACchkAtotal);
$spaces = 45 - $rightLen;
$padchar = “.”;
$text = str_pad($lefttext, $spaces, $padchar) . $ACchkAtotal;
$text = str_replace(’.’,’ ',$text);
print $text;[/php]
[hr]
This is working for me, but I don’t understand why " " or " " or no argument didn’t work for me in the str_pad statement, either in the statement or as $padchar. Is it just that echo or print didn’t show the blanks?

Edit: Yes, it just looks like that for echo or print. I examined the page source and all the blanks get inserted if I just use the first two args with str_pad.


#11

I got it cleaned up and tried to avoid some duplicate lines of code by putting them in a function. I guess str_pad works with the default space character because rather than displaying the line with echo or print, it’s putting the text on a png output.

[php]#Define function to format text in plot area with the correct number of spaces
function textformat($lefttext, $righttext) {
$leftLen = strlen($lefttext);
$rightLen = strlen((string)$righttext);
$spaces = 45 - $rightLen;
$text = str_pad($lefttext, $spaces) . $righttext;
return $text;
}
#Add text to the plotarea
$c->addText(490, 50, textformat(“Adipose clipped chinook this year:”, $ACchkAtotal),"…/…/fonts/cour.ttf", 10);
$c->addText(490, 63, textformat(“Adipose clipped chinook jacks this year:”, $ACchkJtotal),"…/…/fonts/cour.ttf", 10);
#$c->addText(490,63,“Adipose clipped chinook jacks this year: $ACchkJtotal”, “…/…/fonts/cour.ttf”, 10);
$c->addText(490,76, textformat(“Adipose present chinook this year:”,$APchkAtotal),"…/…/fonts/cour.ttf", 10);

$c->addText(490,89, textformat(“Adipose present chinook jacks this year:”,$APchkJtotal), “…/…/fonts/cour.ttf”, 10);
$c->addText(490,102, textformat(“Adipose Unknown chinook this year:”,$uknChktotal), “…/…/fonts/cour.ttf”, 10);

#Output the chart
header(“Content-type: image/png”);
print($c->makeChart2(PNG));
?>[/php]
[hr]
Oh crap, ChartDirector has a markup language just for this stuff:
http://www.advsofteng.com/doc/cdphp.htm#legendpie2.htm
http://www.advsofteng.com/doc/cdcfdoc/cdml.htm


#12

Thanks.