|
Is there a PHP trick to align a colum of numbers? |
Posted by Paul Huffman on Sep-17-2015 03:14 |
|
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.
Here's what I'm using now. It only stays aligned until one of the numbers get > 10 or 100.
#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);
http://ykfp.org/php/lyletrap/lylechinookurl092015share.php?year=2015 |
Re: Is there a PHP trick to align a colum of numbers? |
Posted by Peter Kwan on Sep-17-2015 22:35 |
|
Hi Paul,
There are two methods:
(a) You can use a CDMLTable (created using BaseChart.addTable) and put the text and
numbers in different columns. In this way, the numbers can align with each others.
(b) You can use CDML to align the text. There is an example in the "Pie Chart with Legend
(2)" sample code. In that sample code, the legend entry is configured using CDML so that
the numbers aligned.
http://www.advsofteng.com/doc/cdphp.htm#legendpie2.htm
Hope this can help.
Regards
Peter Kwan |
Re: Is there a PHP trick to align a colum of numbers? |
Posted by Paul Huffman on Sep-19-2015 05:58 |
|
Thanks Peter. I should have checked back with the forum to see if you had an answer. I'll have to look at those methods. Maybe I wouldn't have to use a non proportional font then.
What I used was php str_pad. First I tried sprintf, but couldn't figure out how to get a variable inserted into the format specification. But then I got real confused with str_pad because it seemed like str_pad default space character wasn't working because there were no spaces inserted when I used debug lines with echo or print to look at preliminary results. But I could insert another character like "." then replace it with " " :
$spaces = 45 - $rightLen;
$padchar = ".";
$text = str_pad($lefttext, $spaces, $padchar) . $ACchkAtotal;
$text = str_replace('.',' ',$text);
print $text;
It turned out that it was just echo and print's behavior that didn't display the spaces.
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.
#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));
?> |
Re: Is there a PHP trick to align a colum of numbers? |
Posted by Paul Huffman on Jan-23-2016 06:24 |
|
I already have a legend and I couldn't figure out a way to add this text as a second legend box.
My attempts to align the text columns with CDML failed, still have to count up columns to advance, I think.
$c->addText(490,50,"Adipose clipped chinook this year:.<*advanceTo=40*><*block,width=40,halign=right*>{$ACchkAtotal}");
$c->addText(490,63,"Adipose clipped chinook jacks this year:.<*advanceTo=40*><*block,width=40,halign=right*>{$ACchkJtotal}");
didn't align the arguments in columns like I wanted.
Are there some examples in the documentation on CDML tables? I can see how I create them, but not sure how I write to the table cells. |
Re: Is there a PHP trick to align a colum of numbers? |
Posted by Paul Huffman on Jan-23-2016 08:46 |
|
I found some examples of making tables out of axis labels under the examples in Other XY Chart Features> Data Table 1 and Data Table 2. Form those examples, I bumbled around and came up with better aligned text. Not sure I like the outline of the tables cells, but didn't find a way to eliminate them, unless I use the other approach, use CDML to align the text.
#Add a table for test in upper right
$table=$c->addTable(590,55,TopLeft,3,6);
# Use Arial Bold as the font for the first row
$StyleObj = $table->getStyle();
$StyleObj->setFontStyle("arialbd.ttf");
$colStyleObj = $table->getColStyle(2);
$colStyleObj->setAlignment(Right);
$colStyleObj = $table->getColStyle(1);
$colStyleObj->setAlignment(Left);
#add text to table
$table->setText(1,1,"Adipose clipped chinook this year:");
$table->setText(2,1,$ACchkAtotal);
$table->setText(1,2,"Adipose clipped chinook jacks this year:");
$table->setText(2,2,$ACchkJtotal);
$table->setText(1,3,"Adipose present chinook this year:");
$table->setText(2,3,$APchkAtotal);
$table->setText(1,4,"Adipose present chinook jacks this year:");
$table->setText(2,4,$APchkJtotal);
$table->setText(1,5,"AdiposeUnknown chinook this year:");
$table->setText(2,5,$uknChktotal);
#Add text to the plotarea
#$c->addText(490,50,"Adipose clipped chinook this year: $ACchkAtotal", "../../fonts/cour.ttf", 10);
#$c->addText(490,50,"Adipose clipped chinook this year:.<*advanceTo=40*><*block,width=40,halign=right*>{$ACchkAtotal}");
#$c->addText(490,63,"Adipose clipped chinook jacks this year: $ACchkJtotal", "../../fonts/cour.ttf", 10);
#$c->addText(490,63,"Adipose clipped chinook jacks this year:.<*advanceTo=40*><*block,width=40,halign=right*>{$ACchkJtotal}");
#$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: $uknChktotal", "../../fonts/cour.ttf", 10); |
Re: Is there a PHP trick to align a colum of numbers? |
Posted by Peter Kwan on Jan-26-2016 01:49 |
|
Hi Paul,
You can use:
$c->addText(490,50,"<*block,width=400*>Adipose clipped chinook this year:
<*/*>$ACchkAtotal");
(Note: the above assumes your chart is no less than 1000 pixels wide)
In the above, the text "Adipose clipped chinook this year:" is included in a block which is
400 pixels long, which should be wide enough for the text. In this way, the second piece of
text $ACchkAtotal will start at x = 490 + 400 = 890. If every line is similarly structured,
the second text always start x=890, they will be left-aligned to x=890.
If you prefer to right align the second text, the code is like:
$c->addText(490,50,"<*block,width=400*>Adipose clipped chinook this year:<*/*>
<*block,width=100,halign=right*>{$ACchkAtotal}<*/*>");
If you would like to use the CDMLTable method, to remove the border, simply set the
border color to transparent. May be you can try:
$StyleObj->setBackground(Transparent, Transparent);
Hope this can help.
Regards
Peter Kwan |
|