Jayme Jeffman
Jayme Jeffman

Reputation: 53

Working with TeeChart DateTime on XAxis

I have an Oracle query which returns date string in the format Y-m-d H:i:s and I need to pass them to the Series::AddXY method. How can I do that?

Upvotes: 0

Views: 293

Answers (2)

Jayme Jeffman
Jayme Jeffman

Reputation: 53

The problem is that I do not have constant time intervals and I can not use a "time machine" as in the Candle example .

The time (X value) I have comes from an Oracle query:

  $query = "SELECT ptm.IDENTIFICACAO,
          mtr.SERIAL,
          TO_CHAR(rtu.DATAHORA, 'yyyy-mm-dd hh24:mi:ss') AS DATAHORA,

So the DateTime value is a string in the PHP date format : Y-m-d H:i:s, which I need to convert to TChart values. I do not know if I am full correct but it seems that DateTime values should be entered as float values (Unix Timestamp)

So I am converting them as follows:

    while( ($row = oci_fetch_array($stmt, OCI_ASSOC)) != false ){
      $thetime = DateTime::createFromFormat('Y-m-d H:i:s', $row["DATAHORA"]);


      if($thetime) 
          $tchart->getChart()->getSeries(0)->addXY((float) $thetime->getTimestamp() , $row["ENERTOT"] / 1000);
      }
      ++$rowCount;
    }

I hope this can help someone else.

Best regards.

Upvotes: 0

Yeray
Yeray

Reputation: 5039

The "CandleChart.php" example in the Features demo shipped with the product uses DateTimes on the horizontal axis.
Here a variation:

<?php
        //Includes
        include "../../../../sources/TChart.php";

        $chart1 = new TChart(600,450);
        $chart1->getChart()->getHeader()->setText("Candle Style");
        $chart1->getChart()->getAspect()->setView3D(false);
        // Clip Series points
        $chart1->getChart()->getAspect()->setClipPoints(true);
        $chart1->getChart()->getLegend()->setVisible(false);

        // Add Candle data using doubles for date values
        $today = time();
        $day = 86400;
        $hour = 3600;

        $chart1->getAxes()->getBottom()->setIncrement(DateTimeStep::$ONEMINUTE);
        $chart1->getAxes()->getBottom()->getLabels()->setDateTimeFormat('d/m/Y H:i:s');
        $chart1->getAxes()->getBottom()->getLabels()->setAngle(90);
        $candle=new Candle($chart1->getChart());

        $chart1->setAutoRepaint(false);
        for ($i=$today;$i<($today+$hour);$i+=60) {
          $candle->addCandle($i,rand(0,100),rand(0,100),rand(0,100),rand(0,100));
        }
        $chart1->setAutoRepaint(true);
        $chart1->doInvalidate();

        $chart1->render("chart1.png");
        $rand=rand();
        print '<font face="Verdana" size="2">Candle Chart Style<p>';
        print '<img src="chart1.png?rand='.$rand.'">';                
?>

Upvotes: 1

Related Questions