วันพฤหัสบดีที่ 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 ได้

1 ความคิดเห็น:

  1. jpgraph เพื่อให้แสดง font ภาษาไทย ไฟล์มันหายไปแล้วครับ

    ตอบลบ