php jpgraph ใช้ภาษาไทยไม่ได้ มีวิธีแก้


การใช้ 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 พาไป

คำสำคัญ (Tags): #javascript#jpgraph#php#programming
หมายเลขบันทึก: 181833เขียนเมื่อ 12 พฤษภาคม 2008 00:15 น. ()แก้ไขเมื่อ 20 มิถุนายน 2012 10:25 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (16)

เนื้อหานี้ได้ copy มาจาก website หนึ่ง แต่จำไม่ได้แล้วว่าเอามาจากไหน ขอให้ credit คนเขียนคนแรกด้วยครับ. ขออภัยที่ไม่ได้อ้างถึงในตอนแรก

ดึงข้อมูลจาก database มาเขียนเป็นกราฟต้องทำยังไงค่ะ

รบกวนช่วยให้คำชี้เเนะ เพราะว่าเป็นมือใหม่อยู่อ่ะค่ะ

ขอบคุณมากนะคะ ^^

มีประโยชน์ ขอบคุณครับ

เวลาผมใส่ค่าในแกน X จะเกิด Error ขึ้นครับ แต่ถ้าไม่ใส่($graph->xaxis->SetTickLabels($datax);

)รันได้ปกติ แต่แกน X จะขึ้นเป็นตัวเลขแทน คือผมต้องการให้ขึ้นเป็นตัวอักษรตามที่เรากำหนด เหมือนตัวอย่างที่พี่เขียนมาให้ดูครับ ไม่รู้จะแก้อย่างไง ขอความกรุณาด้วยครับ ผมใช้กราฟเบบบาร์เหมือนที่พี่เขียนเป็นตัวอย่าง ขอบคุณอย่างมากครับ

// 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 X-axis labels

$graph->xaxis->SetTickLabels($datax);

$graph->xaxis->SetLabelAngle(50);

ขอบคุณมากครับ

JPGRAPH 3 ตอนนี้ทำภาษาไทยได้แล้ว

ขอบคุณครับ ไม่ได้ตาม update ตัวนี้นานเลยครับ

สุดยอดครับ JPGraph 3 ภาษาไทยได้ซะที

หุหุดีเลยครับ ขออนุญาต นำไปใช้นะครับ www.lamphuncenter.com

ขอบคุณครับ

สำหรับคนที่ต้องการทราบว่าจะต้องเปลี่ยน font ที่ไหนบ้างลองดูที่นี่ครับ


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

แก้ยังไงค่ะ

Explanation:
HTTP headers have already been sent back to the browser indicating the data as text before the library got a chance to send it's image HTTP header to this browser. This makes it impossible for the library to send back image data to the browser (since that would be interpretated as text by the browser and show up as junk text).
Most likely you have some text in your script before the call to Graph::Stroke(). If this texts gets sent back to the browser the browser will assume that all data is plain text. Look for any text, even spaces and newlines, that might have been sent back to the browser.

For example it is a common mistake to leave a blank line before the opening "<?php".

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท