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

时间:2019-02-07 16:22:22

标签: 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的其他设置,但是没有运气。

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

1 个答案:

答案 0 :(得分:0)

似乎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();
...