複数の凡例の表示スタイルの設定

広告

複数の凡例を表示する場合に縦方向か横方向のどちらの方向へ並べて表示するかを設定します。設定するにはLegendクラスで用意されている「SetLayout」メソッドを使います。

Specify vertical or horizontal legend layout

Parameter:
  $aDirection  Layout for legend box(Default LEGEND_VERT)

引数で凡例をどちらの方向へ並べていくのかを指定します。指定できる値は次の通りです。

設定値方向
LEGEND_VERT垂直方向
LEGEND_HOR水平方向

デフォルトの値は「LEGEND_VERT」ですので垂直方向へ並べて表示していきます。

具体的には次のように記述します。

$graph = new Graph(250, 200);

$graph->legend->SetLayout(LEGEND_HOR);

サンプル

では実際に試してみます。

sample2-1.php

<?php

include ("jpgraph/jpgraph.php");
include ("jpgraph/jpgraph_line.php");

$graph = new Graph(300, 200, "auto"); 
$graph->SetFrame(true);
$graph->SetScale("textlin");

$graph->img->SetMargin(40, 40, 30, 30);
$graph->legend->SetLayout(LEGEND_HOR);

$ydata1 = array(10, 4, 7, 9, 2, 3);
$ydata2 = array(5, 12, 3, 8, 5, 9);

$lineplot1 = new LinePlot($ydata1);
$lineplot1->SetColor(array(255, 0, 0));
$lineplot2 = new LinePlot($ydata2);
$lineplot2->SetColor(array(0, 0, 255));

$lineplot1->SetLegend("2005");
$lineplot2->SetLegend("2006");

$graph->Add($lineplot1);
$graph->Add($lineplot2);

$graph->Stroke();
?>

上記のPHPファイルをブラウザで開いてみます。ブラウザには次のように表示されます。

凡例の表示方向を設定する

複数の凡例を表示する場合に横方向に並べて表示されるようになりました。

( Written by Tatsuo Ikura )