php - 如何正确从mysql底部轴上获取时间序列数据

标签 php mysql teechart

我正在尝试根据 mysql 的时间序列数据呈现折线图。代码如下所示:

$chart2 = new TChart(1200,600);
$chart2->getAspect()->setView3D(false);
$chart2->getHeader()->setText("Line Chart 369874");

$chart2->getAxes()->getLeft()->setVisible(true);
$chart2->getAxes()->getLeft()->setMinimumOffset(10);
$chart2->getAxes()->getLeft()->setMaximumOffset(10);
$chart2->getAxes()->getLeft()->setIncrement(5);
$chart2->getAxes()->getLeft()->getTitle()->setText("Count 369");
$chart2->getAxes()->getLeft()->getAxisPen()->setVisible(false);

//$chart2->getAxes()->getBottom()->getLabels()->setDateTimeFormat('yyyy-mm-dd');
//$chart2->getAxes()->getBottom()->setIncrement(DateTimeStep::$ONEDAY);
//$chart2->getAxes()->getBottom()->setIncrement(1);


$lines=new Line($chart2->getChart()); 

$chart2->getSeries(0)->getXValues()->setDateTime(true);

...
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(!$mysqli){
  die("Connection failed: " . $mysqli->error);
}

$result = $mysqli->query("SELECT date, (((nav/(SELECT nav FROM fund1 ORDER BY date LIMIT 1))-1)*100) AS zmiana FROM fund1");
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {

    $lines->addXY($row["date"], $row["zmiana"]);

}
$result->close();
$mysqli->close();

$chart2->getPanel()->getGradient()->setVisible(false);
$chart2->getPanel()->setColor(Color::fromRgb(230,235,225));
$chart2->getWalls()->getBack()->setTransparent(true);
$chart2->getHeader()->setAlignment(StringAlignment::$NEAR);
$chart2->getLegend()->setVisible(false);

$chart2->render("charts/chart2.png");

数据库中的数据如下所示:
日期 |导航
2007-10-17 | 1000.00
2007-10-18 | 1000.00
2007-10-19 | 1000.00
2007-10-22 | 1000.00
2007-10-23 | 1000.00
2007-10-24 | 1000.81
2007-10-25 | 1000.81
2007-10-26 | 1000.81
...
2018-10-23 | 1646.52

不幸的是,X 轴(日期)上的数据以一种奇怪的方式映射,如附图所示:

teechart php x axis time series data problem

我尝试了底轴的 setIncrement、setDateTimeFormat 的不同设置,但没有成功。

有人遇到过类似的问题吗?

最佳答案

似乎 TeeChart 需要 Unix 时间戳日期格式。
有效的代码:

...
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if(!$mysqli){
  die("Connection failed: " . $mysqli->error);
}
$result = $mysqli->query("SELECT date, (((nav/(SELECT nav FROM fund1 ORDER BY date LIMIT 1))-1)*100) AS zmiana FROM fund1");
while ($row = mysqli_fetch_array($result)) {

    $time = DateTime::createFromFormat('Y-m-d', $row["date"]);
    $lines->addXY(date_timestamp_get($time) , $row["zmiana"]);

}
$result->close();
$mysqli->close();
...

关于php - 如何正确从mysql底部轴上获取时间序列数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54577788/

相关文章:

php - 加载特定的 PHP 版本

防盗链PHP代码

php - 在 MySQL 中存储用户通知的最佳方式是什么?

mysql - Toad for Mysql Server - 连接到远程主机时出现错误的握手错误

linux - Linux 中的 TeeChart 库替代品是什么?

c++ - 如何在执行时更新图表中的数据(在 C++ 构建器中)?

c++ - TeeChart v8 : How to change fonts in runtime?

php - Wordpress ACF get_field() 不返回值

php - 使用 PHP 从 MySQL 打印两个日期之间的 Y-m-d 业务日期列表

php - 如何从数组中找到最大的数字