แสดงบทความที่มีป้ายกำกับ jpgraph แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ jpgraph แสดงบทความทั้งหมด

วันพฤหัสบดีที่ 14 ตุลาคม พ.ศ. 2553

set folder thai font ให้กับ jpgraph เพื่อให้แสดง font ภาษาไทย

สามารถ Download นำไฟล์ไปทดสอบได้ทันที (ไฟล์ thai_001.php และ thai_002.php)
Download
ถ้าไม่สามารถใช้งานได้แสดงว่ามีปัญหาเกี่ยวกับ configuration ของ php ในไฟล์ php.ini ละครับ

jpgraph_ttf.inc.php
/* Placeholders for defined fonts */
FF_USERFONT1 => array(
FS_NORMAL =>'CORDIA.TTF',
FS_BOLD =>'CORDIA.TTF',
FS_ITALIC =>'CORDIA.TTF',
FS_BOLDITALIC =>'CORDIA.TTF' ),

========
ไฟล์แสดง graph

จุดสำคัญคือตรงนี้ครับสมมุติว่าไฟล์ font *.ttf อยู่ใน folder scripts ซึ่งอยู่ระดับรองจากไฟล์โค้ด php
และอย่าลืมว่า string ภาษาไทย ที่ส่งไปให้กับ function ของ jpgraph จะต้องเป็น utf8

define('TTF_DIR',dirname(__FILE__).'/scripts/'); //กำหนดตำแหน่ง folder ที่เก็บ font ttf
require_once ('../scripts/jpgraph/jpgraph/jpgraph.php');
require_once ('../scripts/jpgraph/jpgraph/jpgraph_bar.php');



... โค้ดบางส่วนถูกตัดทอนออกไป

$graph = new Graph(550,320);
$graph->img->ttf->SetUserFont1('CODIA.TTF','CODIA.TTF','CODIA.TTF','CODIA.TTF'); //เทียบเท่ากับกำหนดค่าใน jpgraph_ttf.inc.php
$graph->SetScale('intint');
$graph->SetShadow();
$graph->SetMargin(60,30,40,160);
$bplot = new BarPlot($dataY);
$bplot->SetWidth(0.2);
$bplot->SetFillColor('orange');
$graph->Add($bplot);
$graph->title->Set('การแจ้งซ่อม : จำแนกตามสาขา : '.range_display($start,$end));
$ttf = FF_USERFONT1;
$graph->title->SetFont($ttf,FS_NORMAL,14);
$graph->yaxis->title->SetFont($ttf,FS_NORMAL,14);
$graph->xaxis->title->SetFont($ttf,FS_NORMAL,14);
$graph->xaxis->SetFont($ttf,FS_NORMAL,14);
$graph->xaxis->SetTickLabels($labelY);
$graph->xaxis->SetLabelAngle(70);
$graph->xaxis->HideLastTickLabel();


link: โค้ดช่วยแสดง object ของ jpgraph ที่สามารถ set font ได้

โค้ดช่วยแสดง object ของ jpgraph ที่สามารถ set font ได้

โค้ดช่วยแสดง object ของ jpgraph ที่สามารถ set font ได้ครับ ส่งตัวแปรที่ประกาศด้วย new
อย่างเช่น $graph และ $activity ไปยัง function echoSetFont แบบนี้ครับ


<?php // content="text/plain; charset=utf-8"


require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');

// Some data
$ydata = array(11,3,8,12,5,1,9,13,5,7);

// Create the graph. These two calls are always required
$g = new Graph(350,250);
$g->SetScale('textlin');

// Create the linear plot
$line=new LinePlot($ydata);
$line->SetColor('blue');

// Add the plot to the graph
$g->Add($line);

//START SHOW SETFONTABLE OBJECT
echoSetFont($g,'$g','FF_JASMINE');
echoSetFont($line,'$line','FF_JASMINE');
function echoSetFont($obj,$name,$fontNumber='FF_USERFONT1'){
foreach($obj as $k=>$o){
if (is_object($o)){
$vars = array_keys(get_object_vars($o));
if (method_exists($o,'SetFont')){
echo $name,'->',$k,'->SetFont('.$fontNumber.')<br/>';
}
if ($k != 'graph_theme'){
echoSetFont($o,$name.'->'.$k,$fontNumber);
}
}
}
}
//END SHOW SETFONTABLE OBJECT

// Display the graph
$g->Stroke();
?>