jpgraph is a pretty slick tool. It makes nice looking charts and graphs, and it is very easy to use. You simply pass it an array and it can draw it. Many other tools require serious data massaging and configuration to simply display a simple graph.
However, it’s hard to use your own truetype fonts. I verified that TTF works on my PHP installation, but I received frustrating errors for commands that probably ought to work (but I see now why they didn’t). There’s nothing as simple as:
$graph->xaxis->SetFont("/usr/X11R6/lib/X11/fonts/TTF/luxirr.ttf");
One error I received was “unknown font family specification”, which, according to Google, has only been seen by a couple people in Japan. Heh.
The FAQ has an entry about adding new TTF fonts, but what it describes involves modifying the library to add your font family, which is something not everyone will be able to do, or will be willing to do. (I chose not to because I prefer to use libraries as-is whenever possible).
Here’s how I use a .ttf file that does not exist in the one directory JpGraph checks, without modifying the library. It should work for .ttf files in your local directory. Quick summary: You should be able to put the first block of code near the top of your file, and use the lines from the second block of code just before calling the Graph->Stroke() method. I’ve included some boring details below as well. This has been tested with JpGraph versions 1.19 and 1.20.3 and PHP 4.4.x. It does not work with PHP 5.
Note: This is a hack. You’ll be creating a new TTF superclass, since jpgraph uses a hardcoded TTF_DIR variable for the path, and you’ll be replacing an existing font family in the code, because jpgraph hardcodes the font count. (PHP doesn’t let you redefine constants for some undocumented and unknown reason, making this class stuff necessary). The new class is as follows:
class TTF_local {
var $font_files, $style_names, $ttf, $family_local;
function TTF_local($fontfile,$fontfamily) {
$this->ttf = new TTF();
$this->style_names=$this->ttf->style_names;
$this->font_files=$this->ttf->font_files;
$this->family_local = $fontfamily;
$this->font_files[$this->family_local] =
array(FS_NORMAL=>$fontfile);
}
function File($family, $style=FS_NORMAL) {
if ($family == $this->family_local &&
isset ($this->font_files[$family]) &&
isset ($this->font_files[$family][$style]) &&
file_exists ($this->font_files[$family][$style]) &&
is_readable ($this->font_files[$family][$style])) {
return $this->font_files[$family][$style];
} else {
JpGraphError::Raise(”Error with font $family/$style . It may not exist.”);
}
}
}
(Note: The error handling in this class could be greatly improved).
And the following code will instruct JpGraph to use the new class, and will redefine font family 43 (this value must be 10 < = x <= 43 as defined by jpgraph.php). This assumes $graph is a Graph() object, and that you want the luxirr.ttf file from FreeBSD 4.10's XFree86-fontScalable-4.3.0 package.
$family_local = 43;
$graph->cache->img->ttf = new
TTF_local('/usr/X11R6/lib/X11/fonts/TTF/luxirr.ttf',$family_local);
$graph->xaxis->SetFont($family_local,FS_NORMAL);
Whew. That sure was a lot of code for something relatively basic. Maybe a later version of jpgraph will let you just point directly at a path, or PHP will let you re/undefine constants. Who knows what the future may hold!
Note: You may also be able to create your own local jpg-config.inc file, and require() that before the jpgraph.php file. I don't know if that works, but it does still limit you to working with one and only one font directory.
Note2: A proper patch would involve an update to the TTF class that rips out all of the assumptions the library makes about filenames and paths, to be replaced with a configuration file (the path of which could be specified as a constructor argument to TTF()) that lists out the fonts and paths, as well as a method that lets you specify arbitrary fonts on-the-fly. If there is any interest in such a thing, I'll write it up and submit the patch.


April 24th, 2006 at 5:13 am
Yes, very interested! Came here through Google :)
Your method gives a lot of “cannot access private member” errors, and after making them public i get “Fatal error: Call to a member function SetFont() on a non-object in /usr/home/sites/viewlogic.nl/dev/onderdelen/grafieken/linebarex2.php on line 35″. I also tried to change the File function, but it would just fall back to the ugly internal font.
November 24th, 2007 at 1:49 am
It helped a lot, thanx!
Through Google, too :)