嗨,我正在使用它zend project
来生成图表,google charts api
它使用数据库中的值来查看表格,从“1 月到 12 月”的年份和月份,它们的值低于它们。下面是我的脚本。一切正常,但是当我单击链接输出图表时:我得到了我的代码......以数组的形式而不是可视图表的形式。
['Year', 'Month', ''Value'], ['2008","january,'65], ['2008","february,'56], ['2008","march,'78], ['2008","april,'3], ['2008","may,'67], ['2008","june,'34], ['2008","july,'74], ['2008","august,'85], ['2008","september,'97], ['2008","october,'57], ['2008","november,'58], ['2008","december,'34]
我究竟做错了什么?请帮忙
IndexController.php
<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$yearlyreport = new Application_Model_DbTable_YearlyReport();
$this->view->yearlyreport = $yearlyreport->fetchAll();
}
public function generategraphAction()
{
//$yearlyreport = new Application_Model_DbTable_YearlyReport();
//$this->view->yearlyreport = $yearlyreport->fetchAll();
//$this->User();
//get id param from index.phtml (view)
$id = $this->getRequest()->getParam('id');
//get model and query by $i
$yearlyreport = new Application_Model_DbTable_YearlyReport();
$row = $yearlyreport->getYearlyReport($id);
//assign data from model to view [EDIT](display.phtml)
$this->view->yearlyreport = $yearlyreport;
/*Create a dtring of the form:
* var data = google.visualization.arrayToDataTable([
['Year', 'Sales', 'Expenses'],
['2004', 1000, 400],
['2005', 1170, 460],
['2006', 660, 1120],
['2007', 1030, 540]
]);
*
*/
$data = "['Year', 'Month', ''Value'],
['" . $row['year'] . '","' . 'january' . ",'" . $row['January'] . "],
['" . $row['year'] . '","' . 'february' . ",'" . $row['February'] . "],
['" . $row['year'] . '","' . 'march' . ",'" . $row['March'] . "],
['" . $row['year'] . '","' . 'april' . ",'" . $row['April'] . "],
['" . $row['year'] . '","' . 'may' . ",'" . $row['May'] . "],
['" . $row['year'] . '","' . 'june' . ",'" . $row['June'] . "],
['" . $row['year'] . '","' . 'july' . ",'" . $row['July'] . "],
['" . $row['year'] . '","' . 'august' . ",'" . $row['August'] . "],
['" . $row['year'] . '","' . 'september' . ",'" . $row['September'] . "],
['" . $row['year'] . '","' . 'october' . ",'" . $row['October'] . "],
['" . $row['year'] . '","' . 'november' . ",'" . $row['November'] . "],
['" . $row['year'] . '","' . 'december' . ",'" . $row['December'] . "],";
$this->view->data = $data;
}
}
?>
Generategraph.phtml
<?php
echo $this->doctype();
echo $this->data;
?>
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = <?php echo $this->data; ?>
var options = {
title: 'Generate',
vAxis: {title: 'Year', titleTextStyle: {color: 'red'}}
};
var chart = new google.visualization.BarChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px;"></div>
</body>
</html>
YearlyReport.php "models"
<?php
class Application_Model_DbTable_YearlyReport extends Zend_Db_Table_Abstract
{
protected $_name = 'yearlyreport';
public function getYearlyReport($id)
{
$id = (int)$id;
$row = $this->fetchRow('id =' . $id);
if (!$row) {
throw new Exception("Could not find row $id");
}
return $row->toArray();
}
public function generategraph($year, $January, $February, $March, $April, $May, $June, $July, $August, $September, $October, $November, $December )
{
$data = array(
'year' => $year,
'January' => $January,
'February' => $February,
'March' => $March,
'April' => $April,
'May' => $May,
'June' => $June,
'July' => $July,
'August' => $August,
'September' => $September,
'October' => $October,
'November' => $November,
'December' => $December,
);
$this->generategraph($data);
}
}
index.phtml
<?php
$this->title = "YearlyReports";
$this->headTitle($this->title);
?>
<table>
<tr>
<th>Year</th>
<th>January</th>
<th>February</th>
<th>March</th>
<th>April</th>
<th>May</th>
<th>June</th>
<th>July</th>
<th>August</th>
<th>September</th>
<th>October</th>
<th>November</th>
<th>December</th>
<th> </th>
</tr>
<?php foreach ($this->yearlyreport as $yearly) : ?>
<tr>
<td><?php echo $this->escape($yearly->year); ?></td>
<td><?php echo $this->escape($yearly->January); ?></td>
<td><?php echo $this->escape($yearly->February); ?></td>
<td><?php echo $this->escape($yearly->March); ?></td>
<td><?php echo $this->escape($yearly->April); ?></td>
<td><?php echo $this->escape($yearly->May); ?></td>
<td><?php echo $this->escape($yearly->June); ?></td>
<td><?php echo $this->escape($yearly->July); ?></td>
<td><?php echo $this->escape($yearly->August); ?></td>
<td><?php echo $this->escape($yearly->September); ?></td>
<td><?php echo $this->escape($yearly->October); ?></td>
<td><?php echo $this->escape($yearly->November); ?></td>
<td><?php echo $this->escape($yearly->December); ?></td>
<td>
<a href="<?php echo $this->url(array('controller'=>'index', 'action'=>'generategraph', 'id'=>$yearly->id));?>">GenerateGraph</a>
</td>
</tr>
<?php endforeach; ?>
</table>