การใช้ jpgraph แก้ไขปัญหาภาษาไทย

เขียนโปรแกรมกับ php แล้วจำเป็นต้องออกรายงานหรือ Graph แสดงข้อมูล ไม่อยากเสียเวลาเขียน function graph ให้วุ่นวาย ก็เรียกใช้บริการ jpgraph (http://www.aditus.nu/jpgraph/) ซึ่งมีรูปแบบของ graph ให้เลือกใช้อย่างมากมาย มากจนเลือกไม่ถูก แต่ไม่วายมีปัญหากับภาษาไทย ซะนี่ ออกมาเป็นภาษาต่างดาว

ทำงัยดีหล่ะ ต้องพึ่งพาพี่ Google และ document ของ jpgraph (http://www.aditus.nu/jpgraph/phptip04.php) ... กว่าจะได้ผลลัพธ์ตามต้องการก็ใช้เวลานิดหน่อย ดูรายละเอียดตามนี้เลยครับ ลองมาแล้วได้ผลจริงครับ

ใช้ jpgraph แล้วภาษาไทย ไม่ได้ ให้แก้ตามนี้ คือเพิ่ม font ภาษาไทย เข้าไปใน file

1. jpg-config.inc.php

DEFINE('CORDIA_TTF_FONT', 'cordia.ttf');
DEFINE('CORDIAB_TTF_FONT', 'cordiab.ttf');
DEFINE('CORDIAI_TTF_FONT', 'cordiai.ttf');
DEFINE('CORDIAZ_TTF_FONT', 'cordiaz.ttf');

DEFINE('ANGSA_TTF_FONT', 'angsa.ttf');
DEFINE('ANGSAB_TTF_FONT', 'angsab.ttf');
DEFINE('ANGSAI_TTF_FONT', 'angsai.ttf');
DEFINE('ANGSAZ_TTF_FONT', 'angsaz.ttf');

DEFINE('TAHOMA_TTF_FONT', 'tahoma.ttf');
DEFINE('TAHOMAB_TTF_FONT', 'tahomabd.ttf');
 

2. jpgraph_ttf.inc.php

// Thai font ( Number 40-80 )
DEFINE("FF_ANGSA",47);
DEFINE("FF_CORDIA",48);
DEFINE("FF_TAHOMA",49);

และเพิ่มเติมใน class TTF ในไฟล์เดียวกัน

  /* thai font */
  FF_CORDIA => array(FS_NORMAL=>CORDIA_TTF_FONT,
    FS_BOLD=>CORDIAB_TTF_FONT,
    FS_ITALIC=>CORDIAI_TTF_FONT,
    FS_BOLDITALIC=>CORDIAZ_TTF_FONT ),
  
  FF_ANGSA => array(FS_NORMAL=>ANGSA_TTF_FONT,
    FS_BOLD=>ANGSAB_TTF_FONT,
    FS_ITALIC=>ANGSAI_TTF_FONT,
    FS_BOLDITALIC=>ANGSAZ_TTF_FONT ),
 
  FF_TAHOMA => array(FS_NORMAL=>TAHOMA_TTF_FONT,
    FS_BOLD=>TAHOMAB_TTF_FONT),

สำคัญอีกอย่างคือการแสดงผล ภาษาไทยใน graph ต้องแปลงให้เป็น unicode ก่อน เช่น

$title = iconv('TIS-620', 'UTF-8',"รายงาน Graph แสดงจำนวน");
 

ตัวอย่างเมื่อเรียกใช้งาน

<?php

// Example for use of JpGraph,
// ljp, 01/03/01 20:32
include ("../jpgraph/src/jpgraph.php");
include ("../jpgraph/src/jpgraph_bar.php");
 
$title = iconv('TIS-620', 'UTF-8',"รายงานแสดงค่าใช้จ่ายต่อเดือน");
 
// We need some data
$datay=array(0.13,0.25,0.21,0.35,0.31,0.06);
$datax=array("Jan","Feb","Mar","Apr","May","June");
// Setup the graph.
$graph = new Graph(600,400,"auto"); 
$graph->img->SetMargin(60,40,50,120);
$graph->SetScale("textlin");
$graph->SetMarginColor("lightblue");
$graph->SetShadow();
// Set up the title for the graph
$graph->title->Set($title);
$graph->title->SetFont(FF_TAHOMA, FS_BOLD, 16);
//$graph->title->SetFont(FF_TAHOMA,FS_NORMAL,12);
$graph->title->SetColor("darkred");
$graph->legend->SetFont(FF_TAHOMA,FS_BOLD);
// Setup font for axis
$graph->xaxis->SetFont(FF_TAHOMA,FS_NORMAL,12);
$graph->yaxis->SetFont(FF_TAHOMA,FS_NORMAL,12);
// Show 0 label on Y-axis (default is not to show)
$graph->yscale->ticks->SupressZeroLabel(false);
// Setup X-axis labels
$graph->xaxis->SetTickLabels($datax);
$graph->xaxis->SetLabelAngle(50);
// Create the bar pot
$bplot = new BarPlot($datay);
$bplot->SetWidth(0.6);
// Setup color for gradient fill style
$bplot->SetFillGradient("navy","#EEEEEE",GRAD_LEFT_REFLECTION);
// Set color for the frame of each bar
$bplot->SetColor("white");
$graph->Add($bplot);
$bplot->value->Show();
// Finally send the graph to the browser
$graph->Stroke();
?> 

 

ที่มา : narisa, jpgraph, และที่ google พาไป