Akita National College of Technology Yamamoto's Laboratory
ホーム研究内容コンピューター講義ノート学内限定
コンピューターグラフ作成web_gnuplot →ソースリスト

web_gnuplotのソースリスト


index.php

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML LANG="ja">

<HEAD> 
<META HTTP-EQUIV = "Content-Type" CONTENT = "text/html; charset=EUC-JP">
<META http-equiv="Pragma" content="no-cache">
<META http-equiv="Cache-Control" content="no-cache">
<META http-equiv="Expires" content="0">
<link rel="stylesheet" href="../../../stylesheet.css" type="text/css">
<TITLE>webを使った gnuplot グラフ作成</TITLE>
<STYLE TYPE="text/css">
<!--
BLOCKQUOTE {margin:3px 20px 5px 100px}
.list {margin:4px 20px 4px 20px}
.enumerate {margin:7px 40px 7px 50px}
.CommandName {font-size:12pt; color:#1F6f1F}
.discriptin {padding-bottom : 20px}
a.help:link {color:#00FFFF;text-decoration:none}
a.help:visited {color:#00FFFF;text-decoration:none}
a.help:hover {color:#ffffff;text-decoration:none}
-->
</STYLE>
</HEAD>


<BODY class="frame">

<?PHP
mb_http_output("EUC_JP");
mb_internal_encoding("EUC_JP");
ob_start("mb_output_handler");
$side_webgnuplot=TRUE;
include "../../../head_menu.php";
include "../begin_side_menu_graph.php";
ob_end_flush();
?>


<div STYLE="position:relative; top:0px; left:0px; text-align:left; font-size:10pt">
<a href="../../index.html">コンピューター</a>
&rarr;
<a href="../graph.html">グラフ作成</a>
&rarr;
web_gnuplot
</div>

<H1>web_gnuplot</H1>
<hr>

<!-- ========================================================= -->
<a name="introduction"><H2>webを使ったグラフ作成ツール</H2></a>
<!-- ========================================================= -->
<P>webのインターフェースを使ったグラフ作成ツールです.グラフはgnuplotで作成します.</P>

<p>簡単な例を<a href="help/example_1.png">ここ</a>に載せておきます.<a href="help/example_1.png">この例</a>のように記述して,プロットボタンをクリックするとグラフができます.いろいろな機能がありますので,使ってください.</p>

<p>グラフデータ入力画面の項目をクリックするとヘルプがあらわれます.作図するときの参考にしてください.</p>

<p>また,<a href="source.php">ソースリスト</a>を載せておきます.汚いプログラムですが,何かの参考になると思います.</p>

<!-- --------------------------------------------------------- -->
<a name="input_form"><H3>グラフデータ入力画面</H3></a>
<!-- --------------------------------------------------------- -->

<?PHP
    if($_GET["status"]=="clear"){
        setcookie("webgp_data[plot_data]","",0);
    }
 
 $x_low=array("1列","2列","3列","4列","5列","6列","7列","8列","9列");
 $y_low=array("1列","2列","3列","4列","5列","6列","7列","8列","9列");
 $line=array("","折線","スプライン","指数","対数","定数","1次関数","2次関数","3次関数","4次関数","5次関数","6次関数",
                 "7次関数","8次関数","9次関数",
                 "1次関数(原点通過)","2次関数(原点通過)","3次関数(原点通過)",
                 "4次関数(原点通過)","5次関数(原点通過)","6次関数(原点通過)",
                 "7次関数(原点通過)","8次関数(原点通過)","9次関数(原点通過)");
 $g_file=array("emf","eps","png","png(透明)");
?>

<FORM method="POST" action="mk_graph.php">
    <TABLE border="1" style="background-color:indianred;">
    <TBODY>

     <!-- ---------- タイトル ------------- -->
    <TD><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/title.php" class="help">タイトル</a></TD>
    <TD><INPUT size="60" type="text" name="title" <?php 
     if(isset($_COOKIE[webgp_data][title])){
            print "value=\"".$_COOKIE[webgp_data][title]."\">&nbsp;\n";
        }else{
            print ">&nbsp;\n";
        }?>
    </TR>
    <TR>

     <!-- ---------- x軸 ------------- -->
    <TR>
    <TD><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/x_axis.php" class="help">x軸</a></TD>
    <TD>最小&nbsp;<INPUT size="8" type="text" name="x_min" <?PHP
                       if(isset($_COOKIE[webgp_data][x_min])){
                           print "value=\"".$_COOKIE[webgp_data][x_min]."\">\n";
                       }else{
                           print ">\n";
                       }
                       print "&nbsp;最大&nbsp;<INPUT size=\"8\" type=\"text\" name=\"x_max\"";
                       if(isset($_COOKIE[webgp_data][x_max])){
                           print "value=\"".$_COOKIE[webgp_data][x_max]."\">\n";
                       }else{
                           print ">\n";
                           }

                       print '<INPUT type="checkbox" name="x_axis" value="checked"'.$_COOKIE[webgp_data][x_axis].">対数\n";

                       print "&nbsp;ラベル&nbsp;<INPUT size=\"15\" type=\"text\" name=\"x_label\"";
                       if(isset($_COOKIE[webgp_data][x_label])){
                           print "value=\"".$_COOKIE[webgp_data][x_label]."\">\n";
                       }else{
                           print ">\n";
                       }
                       ?>
        </TD>
    </TR>

     <!-- ---------- y軸 ------------- -->
    <TR>
    <TD><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/y_axis.php" class="help">y軸</a></TD>
    <TD>最小&nbsp;<INPUT size="8" type="text" name="y_min" <?PHP
                       if(isset($_COOKIE[webgp_data][y_min])){
                           print "value=\"".$_COOKIE[webgp_data][y_min]."\">\n";
                       }else{
                           print ">\n";
                       }
                       print "&nbsp;最大&nbsp;<INPUT size=\"8\" type=\"text\" name=\"y_max\"";
                       if(isset($_COOKIE[webgp_data][y_max])){
                           print "value=\"".$_COOKIE[webgp_data][y_max]."\">\n";
                       }else{
                           print ">\n";
                           }
                       print '<INPUT type="checkbox" name="y_axis" value="checked"'.$_COOKIE[webgp_data][y_axis].">対数\n";
                       print "&nbsp;ラベル&nbsp;<INPUT size=\"15\" type=\"text\" name=\"y_label\"";
                       if(isset($_COOKIE[webgp_data][y_label])){
                           print "value=\"".$_COOKIE[webgp_data][y_label]."\">\n";
                       }else{
                           print ">\n";
                       }
                       ?>
        </TD>
    </TR>

    <!-- ---------- 表示 ------------- -->
    <TR>
    <TD><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/hyoji.php" class="help">表示</a></TD>
    <TD>グリッド&nbsp;<?PHP
                       print "<INPUT type=\"checkbox\" name=\"x_grid\" value=\"checked\" ".
                            $_COOKIE[webgp_data][x_grid].">x\n";
                       print "<INPUT type=\"checkbox\" name=\"y_grid\" value=\"checked\" ".
                           $_COOKIE[webgp_data][y_grid].">y\n";

	  print "&nbsp;&nbsp;\n";
	  print '<INPUT type="checkbox" name="monochrome" value="checked" '.$_COOKIE[webgp_data][monochrome].">白黒\n";

                       ?>


    </TR>

    <!-- ---------- xデータ ------------- -->
    <TR>
    <TD><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/x_data.php" class="help">xデータ</a></TD>
    <TD><?PHP
          if(isset($_COOKIE[webgp_data][x_row])){
              set_select("x_row",$x_low,$_COOKIE[webgp_data][x_row]);
          }else{
              set_select("x_row",$x_low,0);
          }
          ?>
    </TD>
    </TR>

    <!-- ---------- yデータ ------------- -->
    <TR>
    <TD><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/y_data.php" class="help">yデータ</a></TD>
    <TD><?PHP
          if(isset($_COOKIE[webgp_data][y_row])){
              set_select("y_row",$y_low,$_COOKIE[webgp_data][y_row]);
          }else{
              set_select("y_row",$y_low,1);
          }

	  print "&nbsp;&nbsp;\n";
	  ?>
	  ポイントサイズ&nbsp;<INPUT size="4" type="text" name="psize" <?PHP
				   if(isset($_COOKIE[webgp_data][psize])){
				     print "value=\"".$_COOKIE[webgp_data][psize]."\">\n";
				   }else{
				     print "value=1.0>\n";
				   }
         
          print "&nbsp;ライン\n";

          if(isset($_COOKIE[webgp_data][line])){
              set_select("line",$line,$_COOKIE[webgp_data][line]);
          }else{
              set_select("line",$line,0);
          }
          ?>
    </TD>
    </TR>

    <!-- ---------- データエリア ------------- -->
    <TR>
          <TD colspan=2><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/data_area.php" class="help">データエリア</a>&nbsp;<INPUT TYPE=button VALUE="データクリア" onClick="location.href='http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/index.php?status=clear'"></TD>
    </TR>
    <TR>
    <TD colspan=2><textarea name="plot_data" cols=60 rows=20><?PHP
                  if(isset($_COOKIE[webgp_data][plot_data]) && $_GET[status]!="clear"){
                      print "\n".$_COOKIE[webgp_data][plot_data]."\n";
                  }
?></textarea></TD>
    </TR>
    <TR>


    <!-- ---------- 描画 ------------- -->
    <TR>
    <TD><a href="http://www.akita-nct.jp/~yamamoto/comp/graph/web_gnuplot/help/byoga.php" class="help">描画</a></TD>
        
    <TD>
        <?PHP
        print "ファイル&nbsp;";
          if(isset($_COOKIE[webgp_data][g_file])){
              set_select("g_file",$g_file,$_COOKIE[webgp_data][g_file]);
          }else{
              set_select("g_file",$g_file,0);
          }

          ?>
                  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <INPUT TYPE="submit" value="プロット">
        </TD>
    </TR>

    </TBODY>
    </TABLE>
</FORM>

<!-- ---------------- 関数 ----------------------- -->
<?php
             //=================================================================
             // setting of radio button
             //    1st argument:name
             //    2nd argument:label
             //    3rd argument:checked number
             //=================================================================
             function set_radio($name,$label,$on){
                           for($i=0; isset($label[$i]); $i++){
                               if($i==$on){
                                   print "<INPUT type=\"radio\" name=\"".$name."\" value=\"".$i.
                                   "\" checked>".$label[$i]."\n";
                               }else{
                                   print "<INPUT type=\"radio\" name=\"".$name."\" value=\"".$i.
                                   "\">".$label[$i]."\n";
                               }
                           }
                       }

             //=================================================================
             // setting of select
             //    1st argument:name
             //    2nd argument:label
             //    3rd argument:selected number
             //=================================================================
             function set_select($name,$label,$on=1){
                 print "<select name=\"".$name."\">\n";
                           for($i=0; isset($label[$i]); $i++){
                               if($i==$on){
                                   print "<option value=".$i." selected>".$label[$i]."\n";
                               }else{
                                   print "<option value=".$i.">".$label[$i]."\n";
                               }
                           }
                           print "</select>\n";
                       }

?>

<!-- ---------------- 本文はここまで ---------------- -->
<?PHP
 $show_counter=FALSE;
 $show_update=TRUE;
 include "../../../end_side_menu.php";
?>


</BODY>
</HTML>

mk_graph.php

<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML LANG="ja">

<HEAD> 
<META HTTP-EQUIV = "Content-Type" CONTENT = "text/html; charset=EUC-JP">
<META http-equiv="Pragma" content="no-cache">
<META http-equiv="Cache-Control" content="no-cache">
<META http-equiv="Expires" content="0">
<link rel="stylesheet" href="../../../stylesheet.css" type="text/css">
<TITLE>グラフの作成</TITLE>
<STYLE TYPE="text/css">
<!--
h4 {margin-left:20px;}
p {margin-left:40px; margin-right:40px}
-->
</STYLE>
</HEAD>

<BODY class="frame">

<?PHP
mb_http_output("EUC_JP");
mb_internal_encoding("EUC_JP");
ob_start("mb_output_handler");
include "../../../head_menu.php";
ob_end_flush();

// ----------------- クッキーの設定 ----------------------------------
$save_time=60*60*24;

setcookie("webgp_data[title]",$_POST["title"],time()+$save_time);

setcookie("webgp_data[x_min]",$_POST["x_min"],time()+$save_time);
setcookie("webgp_data[x_max]",$_POST["x_max"],time()+$save_time);
setcookie("webgp_data[x_axis]",$_POST["x_axis"],time()+$save_time);
setcookie("webgp_data[x_label]",$_POST["x_label"],time()+$save_time);

setcookie("webgp_data[y_min]",$_POST["y_min"],time()+$save_time);
setcookie("webgp_data[y_max]",$_POST["y_max"],time()+$save_time);
setcookie("webgp_data[y_axis]",$_POST["y_axis"],time()+$save_time);
setcookie("webgp_data[y_label]",$_POST["y_label"],time()+$save_time);

setcookie("webgp_data[x_grid]",$_POST["x_grid"],time()+$save_time);
setcookie("webgp_data[y_grid]",$_POST["y_grid"],time()+$save_time);
setcookie("webgp_data[color]",$_POST["color"],time()+$save_time);

setcookie("webgp_data[x_row]",$_POST["x_row"],time()+$save_time);
setcookie("webgp_data[y_row]",$_POST["y_row"],time()+$save_time);

setcookie("webgp_data[y_withline]",$_POST["y_withline"],time()+$save_time);
setcookie("webgp_data[y_spline]",$_POST["y_spline"],time()+$save_time);
setcookie("webgp_data[y_exp]",$_POST["y_exp"],time()+$save_time);
setcookie("webgp_data[y_log]",$_POST["y_log"],time()+$save_time);
setcookie("webgp_data[psize]",$_POST["psize"],time()+$save_time);
setcookie("webgp_data[line]",$_POST["line"],time()+$save_time);

setcookie("webgp_data[plot_data]",$_POST["plot_data"],time()+$save_time);

setcookie("webgp_data[g_file]",$_POST["g_file"],time()+$save_time);
setcookie("webgp_data[monochrome]",$_POST["monochrome"],time()+$save_time);


//------------- 古いファイルの削除 ---------------------------------------------
$tmp_dir="tmp/";
$handle = opendir($tmp_dir);
while(false !== ($file = readdir($handle))){
  if($file != "." && $file != ".." && $file != "data.txt" && $file != "gp_error.txt"){
    $file = $tmp_dir.$file;
    $fst = stat($file);
    $age = time()-$fst["mtime"];
    if(3600<$age)unlink($file);
  }
 }

// ----------------- パイプを開く ----------------------------------

$fp=fopen("tmp/data.txt","w");
fwrite($fp,$_POST["plot_data"]);
fclose($fp);


$error=0;

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin
   1 => array("pipe", "w"),  // stdout
   2 => array("file", "tmp/gp_error.txt", "a") // stderr
);


$gnuplot = proc_open('gnuplot', $descriptorspec, $pipes);

if ( ! is_resource($gnuplot) ) {
  print "proc_open error\n";
  exit(1);
}


// ----------------- グラフ作成 ----------------------------------
$stdout_file="tmp/stdout.txt";

fwrite($pipes[0],"reset\n");
fwrite($pipes[0],"set print \"".$stdout_file."\"\n");
if(isset($_POST["monochrome"])){
       fwrite($pipes[0],"set terminal png medium monochrome\n");
   }else{
       fwrite($pipes[0],"set terminal png medium\n");
   }
fwrite($pipes[0],"set output \"tmp/graph.png\"\n");
fwrite($pipes[0],"unset key\n");
fwrite($pipes[0],"set samples 2048\n");
fwrite($pipes[0],"set ticscale 2\n");
fwrite($pipes[0],"set style line 1 lt 4 lw 3 pt 232 ps ".$_POST["psize"]."\n");

if(isset($_POST["title"])){
       fwrite($pipes[0],"set title \"".$_POST["title"]."\"\n");
       fwrite($pipes[0],"show title\n");
   }

if(isset($_POST["x_label"])){
       fwrite($pipes[0],"set xlabel \"".$_POST["x_label"]."\"\n");
   }

if(isset($_POST["y_label"])){
       fwrite($pipes[0],"set ylabel \"".$_POST["y_label"]."\"\n");
   }

if(isset($_POST["x_min"]) && isset($_POST["x_max"])){
    if($_POST["x_min"] < $_POST["x_max"]){
        fwrite($pipes[0],"set xrange [".$_POST["x_min"].":".$_POST["x_max"]."]\n");
    }else{
       $message[$error]="x軸:最大は,最小より大きくする必要があります.";
       $error++;
    }
 }

if(isset($_POST["y_min"]) && isset($_POST["y_max"])){
    if($_POST["y_min"] < $_POST["y_max"]){
        fwrite($pipes[0],"set yrange [".$_POST["y_min"].":".$_POST["y_max"]."]\n");
    }else{
        $message[$error]="y軸:最大は,最小より大きくする必要があります.";
        $error++;
    }
 }

if(isset($_POST["x_axis"])){
    if($_POST["x_min"]<=0){
        $message[$error]="x軸:対数目盛の場合,最小はゼロより大きくする必要があります.";
        $error++;
    }else{
        fwrite($pipes[0],"set logscale x\n");
    }
 }

if(isset($_POST["y_axis"])){
    if($_POST["y_min"]<=0){
        $message[$error]="y軸:対数目盛の場合,最小はゼロより大きくする必要があります.";
        $error++;
    }else{
        fwrite($pipes[0],"set logscale y\n");
    }
 }

if($_POST[x_grid]=="checked"){
    fwrite($pipes[0],"set grid xtics mxtics\n");
 }

if($_POST[y_grid]=="checked"){
    fwrite($pipes[0],"set grid ytics mytics\n");
 }

//------------- 最小自乗法 ---------------------------------------------

$xxx=$_POST["x_row"]+1;
$yyy=$_POST["y_row"]+1;
$x_y=$xxx.":".$yyy;


if(2 < $_POST["line"]){

    if($_POST["line"]==3){
        $fit_func="a*exp(b*x)";
        $para="a,b";
    }else if($_POST["line"]==4){
        $fit_func="a*log(b*x)";
        $para="a,b";
    }else if(4<$_POST["line"] && $_POST["line"]<15){
        $fit_func = "a0";
        $para = "a0";
        for($i=1; $i<=$_POST["line"]-5;$i++){
            if($i==1){
                    $fit_func .= "+a".abs($i)."*x";
            }else{
                $fit_func .= "+a".abs($i)."*x**".abs($i);
            }
            $para .= ",a".abs($i);
        }
    }else if(14<$_POST["line"] && $_POST["line"]<24){
        $fit_func = "a1*x";
        $para = "a1";
        for($i=2; $i<=$_POST["line"]-14;$i++){
            $fit_func .= "+a".abs($i)."*x**".abs($i);
            $para .= ",a".abs($i);
        }
    }

    fwrite($pipes[0],"f(x)=".$fit_func."\n");
    fwrite($pipes[0],"fit f(x) \"tmp/data.txt\" using ".$x_y." via ".$para."\n");
    fwrite($pipes[0],"print ".$para."\n");
 }

//------------- グラフファイル作成 ---------------------------------------------

if($_POST["line"]!=1){
    fwrite($pipes[0],"plot \"tmp/data.txt\" using ".$x_y." with points ls 1");
 }else{
    fwrite($pipes[0],"plot \"tmp/data.txt\" using ".$x_y." with linespoints ls 1");
 }

if($_POST["line"]==2){
    fwrite($pipes[0],",\"tmp/data.txt\" using ".$x_y." smooth csplines with lines");
 }else if($_POST["line"]==3){
    fwrite($pipes[0],",f(x)");
 }else if($_POST["line"]==4){
    fwrite($pipes[0],",f(x)");
 }if(4<$_POST["line"] && $_POST["line"]<23){
    fwrite($pipes[0],",f(x)");
 }

fwrite($pipes[0],"\n");

//------------- グラフファイル作成 ---------------------------------------------

srand((double)microtime()*1000000);
$gf_name=rand(100000,999999);

if($_POST["g_file"]==0){
    $f_type="emf";
    $g_file="tmp/".$gf_name.".emf";
    if(isset($_POST["monochrome"])){
        $term_set = "emf monochrome solid 'Times Roman' 14";
    }else{
        $term_set = "emf color solid 'Times Roman' 14";
    }
 }else if($_POST["g_file"]==1){
    $f_type="eps";
    $g_file="tmp/".$gf_name.".eps";
    if(isset($_POST["monochrome"])){
      $term_set = "postscript eps enhanced monochrome solid 'Times Roman' 14";
    }else{
      $term_set = "postscript eps enhanced color solid 'Times Roman' 14";
    }
 }else if($_POST["g_file"]==2){
    $f_type="png";
    $g_file="tmp/".$gf_name.".png";
    if(isset($_POST["monochrome"])){
      $term_set = "png medium monochrome";
    }else{
      $term_set = "png medium";
    }
 }else if($_POST["g_file"]==3){
    $f_type="透明png";
    $g_file="tmp/".$gf_name.".png";
    if(isset($_POST["monochrome"])){
      $term_set = "png transparent medium monochrome";
    }else{
      $term_set = "png transparet medium";
    }
 }

fwrite($pipes[0],"set terminal ".$term_set."\n");
fwrite($pipes[0],"set output \"".$g_file."\"\n");
fwrite($pipes[0],"replot\n");


//------------- クローズ ---------------------------------------------

fclose($pipes[0]);
proc_close($gnuplot);


//------------- ディスプレイ表示 ---------------------------------------------

print '<h4>グラフ</h4>'."\n";

print "<p><a href=\"".$g_file."\">グラフのファイル(".$f_type.")</a>ができましたので,右クリックによりダウンロードしてください.</p>\n";


if($error == 0){
    print "<div style=\"width:640px;padding:10px 30px 30px 30px;margin:40px;background-color:#CEE0FF;\">\n";
print "<div style=\"margin-top:0px;margin-left:20px;margin-bottom:10px;\">\n";
print "<INPUT TYPE=button VALUE=\"グラフの修正\" onClick=\"location.href='http://www.akita-nct.jp/yamamoto/comp/graph/web_gnuplot/index.php'\">\n";
print "</div>\n";
// print "<img src=\"tmp/graph.png?".time()."\">\n";
 print "<img src=\"tmp/graph.png\">\n";
    print "</div>\n";
 }else{
    print $error."個の入力にエラーがあります.<BR>\n";
    print "<ol>\n";
    foreach($message as $txt){
        print "<li>".$txt."</li><BR>\n";
    }
    print "</ol>\n";
 }

//------------- フィッティングの結果表示 ---------------------------------------------
$fh = fopen($stdout_file, "r");
$fit_line = fgets($fh, 4096);
fclose($fh);
$fit =  split(" ", $fit_line);
$p = split(",", $para);

if(2<$_POST["line"]){
    print '<h4>フィッティング結果</h4>'."\n";
    print '<p>フィッティング関数は,</P>';
    print '<p><span style="padding-left:50px;color:red;">f(x)='.$fit_func."</span></P>";
    print "<p>です.フィッティングのパラメーターは次のようになります.</p>\n";
    $i=0;
    foreach($fit as $value){
        print "<P style=\"padding-left:80px;color:red;\">".$p[$i]." = ".$value."</p>";
        $i++;
    }


    
 }

?>

<!-- ---------------- 本文はここまで ---------------- -->
<?PHP
 $show_counter=FALSE;
 $show_update=TRUE;
 include "../../../end_side_menu.php";
?>

last update:2009/03/03 16:04:34