Static chart with x-axis timestamp

I want to create a static table of values ​​derived from a MySQL database. The format of the chart will be (x-axis: dd / mm / yy hh: mm: ss (corresponds to the timestamp of the mysql database)), and the y-axis will be a double value. I can successfully retrieve these values ​​from the MySql database. I need help building them with ZingChart

+3
source share
2 answers

Nikita.

After you have extracted your values ​​from your MySQL database, you will need to convert the MySQL date values ​​to Unix time in milliseconds. I populated the $date array with MySQL $date values ​​and iterated over the array, calling strtotime for the first conversion to Unix time and multiplying by 1000 to convert to milliseconds. To be able to directly modify array elements in a loop, I was also preceded by a $ value for assignment by reference.

 foreach ($date as &$value){ $value = strtotime( $value ) * 1000; } 

So now that the values ​​in the $date array have been converted to the correct format, it's time to create a JavaScript array from the PHP array. This can be done using join ():

 var dateValues = [<?php echo join($date, ',') ?>]; 

The resulting array is as follows:

 var dateValues = [1356994800000,1357081200000,1357167600000, ... ]; 

To use this array in ZingChart, use the dateValues ​​variable with the values ​​in the scale-x object. To convert Unix time values ​​to dates in ZingChart, add a "transform" object and set it to "type": "date".

 "scale-x":{ "values": dateValues, "transform":{ "type":"date", "item":{ "visible":false } } }, ... 

It takes care of scale. To get other values ​​in the chart, you do almost the same thing. Convert PHP arrays to JavaScript arrays and use the array variable in your JSON diagram.

With an array of PHP $ rows:

 var seriesValues = [<?php echo join($series, ',') ?>]; 

In your JSON diagram:

 "series":[ { "values":seriesValues } ] 

I put it all together for the Github Gist for you. Let me know if you have any questions!

+3
source

Check out our demos repo on GitHub. We have a tutorial on connecting to a MySQL database with PHP .

Here is a step-by-step walkthrough on our website.

If you share your JSON or more details about this, I can help you build your diagram.

I am on the ZingChart team. Please let me know if you have any other questions.

+2
source

All Articles