Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Smarty_function won't display jpgraph output

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
geoB
Smarty Regular


Joined: 09 Sep 2010
Posts: 46

PostPosted: Sun Oct 03, 2010 2:32 pm    Post subject: Smarty_function won't display jpgraph output Reply with quote

My usual abductive logic approach (flop around aimlessly, hoping for luck or inspiration) has failed after 6-8 hours of trying to get a graph out of jpgraph.

I've verified a functioning installation using a non-Smarty php file with virtually the same code as the smarty function below, so I know it's not jpgraph per se.

What I have is a template containing:
Code:
<img src="{make_graph type='visit'}">

and a function
Code:
function smarty_function_make_graph($params, &$smarty) {
    $type=$params['type'];

require('jpgraph.php');
require('jpgraph_bar.php');
switch ($type) {
  case 'search':
    $xaxis = 'Searches';
    $sql = "select count(*) as $xaxis, ";
    $gtitle = 'Searches by month';
    $ytitle = 'Searches';
    break;
  case 'visit':
    $xaxis = 'Visitors';
    $sql = "select count(distinct ip) as $xaxis, ";
    $gtitle = 'Visitors by month';
    $ytitle = 'Visits';
    break;
}

//create an array of month names such that current month is last (11th) element
$month_name = array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
$now = getdate();
$curr_month = $now['mon']-1;
$datax = array_slice($month_name,$curr_month,12-$curr_month);
$new_year = array_slice($month_name,0,$curr_month);
$datax = array_merge($datax,$new_year);

$db = new DB;

$sql .= "left(monthname(received),3) as Month from searches
where extract(YEAR_MONTH from received)>=period_add(extract(YEAR_MONTH from now()),-12) and
extract(YEAR_MONTH from received) <> extract(YEAR_MONTH from now())
group by Month
order by extract(YEAR_MONTH from received)";
$res = $db->prepare($sql);
$res->execute();

//create an array of values, insert 0 for a month if no data exists
$arr = $res->fetch(PDO::FETCH_ASSOC);
for ($i=0;$i<12;$i++) {
  $index = $datax[$i];
  if ($arr['Month']==$index) {
    $datay[$i] = $arr[$xaxis];
    $arr = $res->fetch(PDO::FETCH_ASSOC);
  } else {
    $datay[$i] = 0;
  }
}

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

// Adjust the margin a bit to make more room for titles
$graph->img->SetMargin(40,30,20,40);

// Create a bar pot
$bplot = new BarPlot($datay);
$graph->Add($bplot);

// Setup the titles
$graph->title->Set($gtitle);
$graph->xaxis->title->Set("Month");
$graph->yaxis->title->Set($ytitle);

$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->SetTickLabels($datax);
// Display the graph
//$graph->Stroke();
//$result= $graph->Stroke();
$graph->Stroke();
}

In Eclipse Galileo debug it appears that a PNG stream results from the function, but no image appears.

Any and all enlightenment will be immensely appreciated.

Thanks.

g


Last edited by geoB on Mon Oct 04, 2010 1:16 pm; edited 1 time in total
Back to top
View user's profile Send private message
geoB
Smarty Regular


Joined: 09 Sep 2010
Posts: 46

PostPosted: Sun Oct 03, 2010 5:21 pm    Post subject: progress of a sort Reply with quote

So far I've discovered a small error in the DB class that, once eliminated, allows SOME graphs to be displayed. If I have a file called graphgenerator.php that takes a $_GET variable (e.g.,
Code:
<img src="graphgenerator.php?type=visit">
) , the graphs display. If instead I use a function with the identical variable as an element of $params (e.g.,
Code:
{make_graph type='visit'}
) I get an error message "The image "http://smart/graphs.php" cannot be displayed, because it contains an error." Ok, but what?

Or should I stop hitting myself on the head and take what works?

g
Back to top
View user's profile Send private message
fengzbao@qq.com
Smarty n00b


Joined: 01 Sep 2013
Posts: 1

PostPosted: Sun Sep 01, 2013 10:50 am    Post subject: Smarty&#20013;&#22788;&#29702;Jpgraph&#22270 Reply with quote

  Smarty是PHP种最常使用的模板引擎,而因为要做的系统的原因,我们还要使用在图像处理方面很具优势的Jpgraph插件来进行图像的数据化查询。因此,怎么样将两者玩完美地结合,是我将要努力实现的地方。
  起初,我们为了让系统有生机,我直接把写好的用来显示图形的文件以图像的形式导进模板:
  
Code:
<img src="model/plotline.php">

  这样直接显示我们想要的图形即可,当然,这只是静态的,因为还没有进行数据库的查询,也就没有数据传递了。到后来我们要和数据库想联系的时候呢,一般的情况,应该是将处理完的数据放在这个页面,交给这个页面进行处理,问题就在这里,怎么样将查询数据库的结果传递到这个页面,在smarty社区有这样一位兄台,他是自己写了一个smarty自定义的函数,用来完成这样一个自动生成图像的函数,直接在模板页面调用,调用的时候当然还有参数之类的。但是方法是有了,他自己提问了1年之后自己有回复了一下,说自己只是找到自己的DB类出现了个小问题,但还是不能成功我以为麻烦了,经过细心研究,发现我之前用的那个办法其实就是最好的办法,将数据通过SESSION传递到plotline.php,然后处理即可。
  //module_*_php.php
Code:

   //查询数据库
   //详细数据 and 提交时间
   $sql_graph="select data,submit_date from  data
   where node_id ='$node'";
   $data=$conn->query($sql_graph);
   $arrData['data']=$data->fetchAll();
   
   //入库时间
   /* $_SESSION['date']=$arrData['data'][0]['submit_date'];
   var_dump($_SESSION['date']); */
   
   
   //数据量的情况
   $rows=$data->rowCount();
   //获取数据类型
   $sql_type="select node.node_name,node.alert,data_type.data_type_title,data_type.unit  from node
         LEFT JOIN `data_type`
         ON node.data_type_id=data_type.data_type_id
         where node_id='$node'";
   
   $type=$conn->query($sql_type)->fetchAll();
   //数据类型
    $_SESSION['type']=$type[0]['data_type_title'];
   //数据单位
    $_SESSION['u']=$type[0]['unit'];
    //节点名称
    $_SESSION['node']=$type[0]['node_name'];
    //节点阈值
      $_SESSION['alert']=$type[0]['alert'];


Plotline.php:
Code:

//数据来自graph_model.php
session_start();
$datay=$_SESSION['datay'];
$date_original=$_SESSION['date_original'];
// print_r($date_original);
$type=$_SESSION['type'];
$unit=$_SESSION['u'];
$node=$_SESSION['node'];
$alert=$_SESSION['alert'];
//将获取的数据处理成下面的数组形式
//  $datay=array(23,33,25,40,20,50,70);
 // Create the graph.
$graph = new Graph(980,400,'auto');
$graph->SetScale("textlin");
$graph->SetMargin(50,20,50,70);
$graph->SetShadow();
$graph->legend->SetPos(0.5,0.97,'center','bottom');

$graph->title->Set($node.'单站含阈值柱状图');//节点名称 标注
// $graph->title->SetFont(FF_ARIAL,FS_BOLD,14);
$graph->title->SetFont(FF_FONT1,FS_BOLD);
$graph->title->SetFont(FF_SIMSUN,FS_BOLD,12);

$graph->subtitle->SetFont(FF_SIMSUN,FS_BOLD,10);
$graph->subtitle->Set('(横轴为数据入库时间,纵轴为数据值)');

//x
$graph->xaxis->title->Set("时间轴(入库时间)");
$graph->xaxis->title->setfont(FF_SIMSUN,FS_BOLD,10);
$graph->xaxis->SetFont(FF_SIMSUN,FS_BOLD);
// $graph->xaxis->SetLabelAngle(90);
//yaxis
$graph->yaxis->title->Set($type."/".$unit);//数据类型与数据单位  标注
$graph->yaxis->title->setfont(FF_SIMSUN,FS_BOLD,10);
$graph->yaxis->SetFont(FF_SIMSUN,FS_BOLD);

// Create a bar pot
$bplot = new BarPlot($datay);
$bplot->value->Show();
$bplot->value->SetFont(FF_VERDANA,FS_BOLD,8);
$bplot->SetValuePos('top');
$bplot->SetLegend("节点".$type."实值");

$graph->Add($bplot);
//阈值部分设置
$pline = new PlotLine(HORIZONTAL,$alert,'red',4);//阈值部分
$pline->SetLegend('阈值');

$graph->legend->SetColumns(10);
$graph->legend->SetFont(FF_SIMSUN,FS_BOLD,10);

$graph->Add($pline);
$graph->Stroke();

模板页面:
Code:

<!-- 图形调取   -->
      <?{if !empty($arrData.img)}?>
         <?{if $smarty.post.graph eq 2}?><img src="model/plotline.php">
         <?{elseif $smarty.post.graph eq 1}?><img src="model/dateaxis.php">
         <?{/if}?>
         <?{html_image file=templates/images/okk.png }?>Generated by Mine Hydrogeology System <?{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}?>
      <?{/if}?>

  我还尝试过将数据在模板页面通过GET方式提交到plotlinr.php的方法。但这样的话因为传递的总是一个数组,所以试过多次之后总是不能成功,也就放弃了,也算是个经验之谈:这样的数据数组怎么能通过GET方式来传递呢,PHP最强势的地方不就是处理数据吗?还是抓住PHP的核心解决问题最好。[/code]
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP