PHPlot で日本語表示する方法
PHPlotでの日本語を表記するのは、現在では非常に簡単です。
- TrueType フォントを プログラムからアクセスできる場所に置く
- フォントの設定をする。
- 引数にUTF-8に変換して文字列を渡してやる。(mbstringを用いるか iconvを用いる)
これだけでできます。
ただし注意事項があります
- PHPlotは、GDを利用して描画を行っており、GDは、文字の取り扱いに FreeType ライブラリを使用しています。FreeTypeライブラリには、TTFフォントに埋め込みビットマップフォントが入っている場合にそのフォントの回転がうまくできないバグがあるようです。
-
- 埋め込みビットマップフォントは、文字が小さい場合などに利用されますので、フォントが小さい場合に、文字の回転がうまくいかない場合があります。 つまり、Y軸のタイトルがおかしくなるわけです. (例)
- したがって、埋め込みビットマップフォントを持たないフォントデータを使用することが必要です。
- そのようなフォントデータは fontforge を利用したりして作成できますし、
新しい(ver3以降の)IPA フォントでは、ビットマップフォントが埋め込まれていないので問題無く利用できます。
以下に動作例をあげます
ソース
<?php
# ドキュメントの 5.6章の例を日本語化したものです
# 5.6. Example - Bar Chart, Label Options
# PHPlot Example: Bar chart, annual data
require_once 'phplot.php';
$data = array(
array('1985', 340), array('1986', 682), array('1987', 1231),
array('1988', 2069), array('1989', 3509), array('1990', 5283),
array('1991', 7557), array('1992', 11033), array('1993', 16009),
array('1994', 24134), array('1995', 33768), array('1996', 44043),
array('1997', 55312), array('1998', 69209), array('1999', 86047),
array('2000', 109478), array('2001', 128375), array('2002', 140767),
);
$plot = new PHPlot(800, 600);
# フォントの置き場所を指定します。XREAなので 以下の場所(#########はID)にフォントを置きました。
$plot->SetTTFPath('/virtual/#########/freefonts/');
# デフォルトフォントの指定。すべてのフォントに日本語フォントを使ってみます。
$plot->SetDefaultTTFont('ipam-mona.ttf');
$plot->SetImageBorderType('plain');
$plot->SetPlotType('bars');
$plot->SetDataType('text-data');
$plot->SetDataValues($data);
# Let's use a new color for these bars:
$plot->SetDataColors('magenta');
# Force bottom to Y=0 and set reasonable tick interval:
$plot->SetPlotAreaWorld(NULL, 0, NULL, NULL);
$plot->SetYTickIncrement(10000);
# Format the Y tick labels as numerics to get thousands separators:
$plot->SetYLabelType('data');
$plot->SetPrecisionY(0);
# Main plot title:
$str = "米国 携帯電話登録者";
# mbstrigを使ってUTF-8に変換します。
$str = mb_convert_encoding($str,"UTF-8","EUC-JP");
# iconvを使ってUTF-8に変換します。
# $str = iconv("EUC-JP","UTF-8//TRANSLIT",$str);
# UTF-8に変換した値を関数に渡します。
$plot->SetTitle($str);
# Y Axis title:
$str = "登録者(千人)";
# mbstrigを使ってUTF-8に変換します。
$str = mb_convert_encoding($str,"UTF-8","EUC-JP");
# iconvを使ってUTF-8に変換します。
# $str = iconv("EUC-JP","UTF-8//TRANSLIT",$str);
# UTF-8に変換した値を関数に渡します。
$plot->SetYTitle(($str));
# Turn off X tick labels and ticks because they don't apply here:
$plot->SetXTickLabelPos('none');
$plot->SetXTickPos('none');
$plot->DrawGraph();
?>
ソース
<?php
# ドキュメントの 5.8章の例を日本語化したものです
# PHPlot Example: Pie/text-data-single
require_once 'phplot.php';
# The data labels aren't used directly by PHPlot. They are here for our
# reference, and we copy them to the legend below.
$data = array(
array('オーストラリア', 7849),
array('コンゴ共和国', 299),
array('カナダ', 5447),
array('コロンビア', 944),
array('ガーナ', 541),
array('中国', 3215),
array('フィリピン', 791),
array('南アフリカ', 19454),
array('メキシコ', 311),
array('アメリカ合衆国', 9458),
array('ソビエト連邦', 9710),
);
$plot = new PHPlot(800,600);
# フォントの置き場所を指定します。XREAなので 以下の場所(#########はID)にフォントを置きました。
$plot->SetTTFPath('/virtual/#########/freefonts/');
# デフォルトフォントの指定。すべてのフォントに日本語フォントを使ってみます。
$plot->SetDefaultTTFont('ipam-mona.ttf');
$plot->SetImageBorderType('plain');
$plot->SetPlotType('pie');
$plot->SetDataType('text-data-single');
$plot->SetDataValues($data);
# Set enough different colors;
$plot->SetDataColors(array('red', 'green', 'blue', 'yellow', 'cyan',
'magenta', 'brown', 'lavender', 'pink',
'gray', 'orange'));
# Main plot title:
$str = "世界の金の生産量, 1990年 \n(1000s of Troy Ounces)";
# mbstrigを使ってUTF-8に変換します。
$str = mb_convert_encoding($str,"UTF-8","EUC-JP");
# iconvを使ってUTF-8に変換します。
# $str = iconv("EUC-JP","UTF-8//TRANSLIT",$str);
# UTF-8に変換した値を関数に渡します。
$plot->SetTitle($str);
# Build a legend from our data array.
# Each call to SetLegend makes one line as "label: value".
foreach ($data as $row)
{
# mbstrigを使ってUTF-8に変換します。
$row[0]=mb_convert_encoding($row[0],"UTF-8","EUC-JP");
# iconvを使ってUTF-8に変換します。
# $row[0]= iconv("EUC-JP","UTF-8//TRANSLIT",$row[0]);
$plot->SetLegend(implode(': ', $row));
}
$plot->DrawGraph();
?>
PHPlot home