Jpgraph从mysql timestamp列添加xaxis

时间:2015-06-01 14:32:45

标签: php mysql jpgraph

首先,我的编码知识仍然是基本的,所以请耐心等待。

我正在尝试从mysql数据库中绘制图形。 mysql数据库看起来像这样

    mysql> SELECT * FROM usage_status_difference;
+-----+---------------------+------------+-----------+
| id  | on                  | cold_water | hot_water |
+-----+---------------------+------------+-----------+
| 146 | 2014-04-26 02:00:02 |       3.14 |      1.86 | 
| 147 | 2014-05-26 02:00:01 |       2.69 |      1.63 | 
| 148 | 2014-06-26 02:00:01 |       3.58 |      1.41 | 
| 149 | 2014-07-26 02:00:01 |       1.46 |      0.61 | 
| 152 | 2014-08-26 02:00:01 |       3.03 |      1.47 | 
| 153 | 2014-09-26 02:01:02 |       2.39 |      1.47 | 
| 154 | 2014-10-26 02:01:02 |       3.89 |      1.50 | 
| 155 | 2014-12-03 17:52:27 |       1.76 |      1.88 | 
| 156 | 2014-12-26 02:01:01 |       1.50 |      0.98 | 
| 157 | 2015-01-26 02:01:01 |       4.19 |      2.44 | 
| 158 | 2015-02-26 02:01:02 |       3.38 |      2.07 | 
| 159 | 2015-03-26 02:01:01 |       2.67 |      1.79 | 
| 161 | 2015-04-26 09:46:07 |       2.48 |      1.73 | 
| 162 | 2015-05-26 02:01:02 |       2.67 |      1.77 | 
+-----+---------------------+------------+-----------+

使用Jpgraph示例我已设法提取数据并创建TWO BAR图。这是我使用的代码

    <?php // content="text/plain; charset=utf-8"
// $Id: groupbarex1.php,v 1.2 2002/07/11 23:27:28 aditus Exp $
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_bar.php');

// MYSQL database
$con=mysqli_connect("localhost","xxxx","xxxxx","xxxxx");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM `usage_status_difference` WHERE `on` >= '2015-01-01' AND `on` <= '2015-12-01'");

while($row = mysqli_fetch_array($result)) {
$datay1[]= $row["cold_water"];
$datay2[]= $row["hot_water"];
//  $count[]= $row["on"]

}

$graph = new Graph(450,200,'auto');    
$graph->SetScale("textlin");
$graph->SetShadow();
$graph->img->SetMargin(40,30,40,40);
$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());

$graph->xaxis->title->Set('Year 2015');
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);

$graph->title->Set('Poraba Vode');
$graph->title->SetFont(FF_FONT1,FS_BOLD);

$bplot1 = new BarPlot($datay1);
$bplot2 = new BarPlot($datay2);

$bplot1->SetFillColor("orange");
$bplot2->SetFillColor("red");


$bplot1->SetShadow();
$bplot2->SetShadow();


$gbarplot = new GroupBarPlot(array($bplot1,$bplot2));
$gbarplot->SetWidth(0.8);


$graph->Add($gbarplot);
$bplot1->value->Show();
$bplot2->value->Show();
$graph->Stroke();
?>

我想要的是在xaxis上我会从&#34; ON&#34;并将其作为Xaxis。现在它只是从JAN,FEB等开始。

表格的结构是

DESCRIBE usage_status_difference;
+------------+-------------+------+-----+-------------------+----------------+
| Field      | Type        | Null | Key | Default           | Extra          |
+------------+-------------+------+-----+-------------------+----------------+
| id         | int(11)     | NO   | PRI | NULL              | auto_increment | 
| on         | timestamp   | NO   |     | CURRENT_TIMESTAMP |                | 
| cold_water | float(10,2) | NO   |     | NULL              |                | 
| hot_water  | float(10,2) | NO   |     | NULL              |                | 
+------------+-------------+------+-----+-------------------+----------------+

感谢任何帮助。

0 个答案:

没有答案