How to use Japanese characters on PHPlot
It becomes very easy to use Japanese characters on PHPlot.
It needs only following steps.
- Place Japanese TrueType fonts where PHPlot main program can access.
- Place Font Setting commands in your PHPlot script.
- Convert Japanese Characters to UTF-8 and set to the variavle.(by using mbstring or iconv)
Notes: Caution!!
- PHPlot uses GD for drawings . GD uses FreeType library for displaying characters. Current FreeType Libaray has some bugs in the rotating characters when the TTF font including BITMAP font is used. The included BITMAP font is used in case of displaying small characters . So if you want to use small font in Y axis title , the rotation of character will not work.
- (Example)
- So Don't use Fonts including Bitmap fonts.
- You can create the fonts not including bitmap fonts by using fontforge utilities .
New (after ver3) IPA fonts have no including bitmaps . So please use this.
Examples
Source Code
<?php
# 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);
# Define font place . This site places on the XREA.com host,so I place fonts in following folder. (########## : my ID)
$plot->SetTTFPath('/virtual/#########/freefonts/');
# Define default font.
$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 = "米国 携帯電話登録者";
# Convert to UTF-8 with mbstring.
$str = mb_convert_encoding($str,"UTF-8","EUC-JP");
# Convert to UTF-8 with iconv
# $str = iconv("EUC-JP","UTF-8//TRANSLIT",$str);
# Set converted value.
$plot->SetTitle($str);
# Y Axis title:
$str = "登録者(千人)";
# Convert to UTF-8 with mbstring.
$str = mb_convert_encoding($str,"UTF-8","EUC-JP");
# Convert to UTF-8 with iconv
# $str = iconv("EUC-JP","UTF-8//TRANSLIT",$str);
# Set converted value.
$plot->SetYTitle(($str));
# Turn off X tick labels and ticks because they don't apply here:
$plot->SetXTickLabelPos('none');
$plot->SetXTickPos('none');
$plot->DrawGraph();
?>
Source Code
<?php
# 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);
# Define font place . This site places on the XREA.com host,so I place fonts in following folder. (########## : my ID)
$plot->SetTTFPath('/virtual/#########/freefonts/');
# Define default font.
$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)";
# Convert to UTF-8 with mbstring.
$str = mb_convert_encoding($str,"UTF-8","EUC-JP");
# Convert to UTF-8 with iconv
# $str = iconv("EUC-JP","UTF-8//TRANSLIT",$str);
# Set converted value.
$plot->SetTitle($str);
# Build a legend from our data array.
# Each call to SetLegend makes one line as "label: value".
foreach ($data as $row)
{
# Convert to UTF-8 with mbstring.
$row[0]=mb_convert_encoding($row[0],"UTF-8","EUC-JP");
# Convert to UTF-8 with iconv
# $row[0]= iconv("EUC-JP","UTF-8//TRANSLIT",$row[0]);
$plot->SetLegend(implode(': ', $row));
}
$plot->DrawGraph();
?>
PHPlot home