Php Jpgraph从mysql时间戳列添加xaxis
首先,我的编码知识仍然是基本的,所以请耐心等待 我试图从mysql数据库中绘制一个图表。mysql数据库如下所示Php Jpgraph从mysql时间戳列添加xaxis,php,mysql,jpgraph,Php,Mysql,Jpgraph,首先,我的编码知识仍然是基本的,所以请耐心等待 我试图从mysql数据库中绘制一个图表。mysql数据库如下所示 mysql> SELECT * FROM usage_status_difference; +-----+---------------------+------------+-----------+ | id | on | cold_water | hot_water | +-----+---------------------+-
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示例,我成功地提取了数据并创建了两个条形图。这是我使用的代码
<?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上会有相应的日期从列中提取出来。我想我需要修改“$graph->xaxis->SetTickLabels($gDateLocale->GetShortMonth());”但我尝试的一切都不起作用。所以我在寻求帮助
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 | |
+------------+-------------+------+-----+-------------------+----------------+