0

I'm new in this community. Since a few days I'm trying to implement a chart using Highstock. This chart will display the monitoring of temperatures in function of the time.

The values are acquired through PHP and stored in MySQL. I am able to display the temperatures values but not the DateTime in the XAXIS of Highchart.

I am not used with the Javascript and I think my problem could be issued if I find the solution how to convert DateTime into Timestamp in Javascript and displays it as DateTime.

Please find my code below :

    <?php

        //Récupération des valeurs stockées dans MySQL
        mysql_connect("localhost","root","root");       //connexion à la base de données mysql
        mysql_select_db("Chart");                       //connexion à la base de données concernées

?>

<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="https://code.highcharts.com/stock/highstock.js"></script>
<script src="https://code.highcharts.com/stock/modules/exporting.js"></script>


<div id="container">

<script>

<?php
        //récuparation de la colonne "value" dans le tableau "temperature"
        $query = mysql_query("SELECT value FROM inside_temp");
        while($ligne = mysql_fetch_array($query)){
                $inside_temp[] = $ligne[0];
        }
        //récupération de la colonne date dans le tableau date
        $query = mysql_query("SELECT date FROM inside_temp");
        while($row = mysql_fetch_array($query)){
                //$date_inside[] = $row[0];
                $date_inside_temp[] = strtotime($row[0]) *1000;
                $date_inside_temp[] = date_format($row[0]," Y-m-d H:i:s,");
        }
        //récuparation de la colonne "value" dans le tableau "temperature"
        $query = mysql_query("SELECT value FROM outside_temp");
        while($ligne = mysql_fetch_array($query)){
                $outside_temp[] = $ligne[0];
        }
        //récuparation de la colonne "value" dans le tableau "temperature"
        $query = mysql_query("SELECT value FROM thermostat_setpoint");
        while($ligne = mysql_fetch_array($query)){
                $therm_setpoint[] = $ligne[0];
        }
        //récuparation de la colonne "value" dans le tableau "temperature"
        $query = mysql_query("SELECT value FROM consumption");
        while($ligne = mysql_fetch_array($query)){
                $consumption[] = $ligne[0] / 1000;
        }

?>

    Highcharts.stockChart('container', {

        rangeSelector: {
            selected: 4
        },

        title: {
                text: 'Consumption & Temperature monitoring'
        },

        subtitle: {
                text: 'Heating experiment - LTU, Summer 2017'
        },

        xAxis: {
                type: 'datetime',
                        //categories: [<?php echo join($date_inside_temp,',') ?>],
                        categories: Date.UTC(<?php echo $date_inside_temp;?>),
                tickInterval: 15,
                labels: {
                        format: '{value:%Y-%b-%e %H:%m}'
                }
        },

        yAxis: {
                title: {
                        text: 'Measured values (°C or kW)'
                        }
        },


        series: [{
                name: 'Wall plug consumption',
                data:[<?php echo join($consumption,',') ?>]

        }, {

                name: 'Inside temperature',
                data: [<?php echo join($inside_temp,',') ?>]

        }, {

                name: 'Thermostat setpoint',
                data:[<?php echo join($therm_setpoint,',') ?>]

        }, {

                name: 'Outside temperature',
                data:[<?php echo join($outside_temp,',') ?>]

     }] //fin de series

    });

The current result in picture

Sascha E.
  • 1
  • 3

2 Answers2

2

For highcharts you must convert dateTime not only into timestamp, but in timestamp with milliseconds.

When i had such a problem. I converted dateTime to timeStamp with milliseconds in PHP. It is convenient to do in PHP.

PHP

strtotime({your_dateTime})*1000

MYSQL This Query for CONVERT DATETIME to UNIX TIME STAMP

SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))*1000

But if you want to do conversion in JavaScript ONLY, so:

converting-a-datetime-string-to-timestamp-in-javascript

how-do-you-get-a-timestamp-in-javascript


06.08.2017 - update

Official docs: data-from-a-database How to fetch data from mysql and convert to to highcharts.

Convertation in php sandbox : php_sand_box

jsFiddle http://jsfiddle.net/1gbpzeho/

Constantine
  • 544
  • 5
  • 15
  • Yes I did the conversion in PHP as you noticed it in your comment. However, as you can see it on my code and the attached picture, it doesn't work. – Sascha E. Aug 03 '17 at 11:43
  • so you have timestamp in milliseconds and it still doesn't work?? Can you show your timestamp for any item in x-axis& – Constantine Aug 03 '17 at 12:24
  • You don't need to ceonvert it back to datetime in Js. – Constantine Aug 03 '17 at 12:41
  • My PHP array after converting datetime into Javascript timestamp looks like that : Array ( [0] => 1501771394000 [1] => 1501771426000 [2] => 1501771458000 [3] => 1501771490000 [4] => 1501771521000 [5] => 1501771553000 [6] – Sascha E. Aug 04 '17 at 11:00
  • so it should work. You don't need Date.UTC();. If still won't work, i'll create jsFiddle with your dates. Hope it will help. – Constantine Aug 04 '17 at 11:06
  • Yes it should work but it doesn't work. Maybe I made a mistake during the series implementation. Is it right as shown on my code ? – Sascha E. Aug 05 '17 at 10:04
  • Yes if you can create a jsFiddle it could very help me. Thank you – Sascha E. Aug 05 '17 at 10:05
  • I'm sorry but I don't understand what you mean – Sascha E. Aug 07 '17 at 09:42
  • Ok I understood right now ... Thank you I will try it and feedback ! – Sascha E. Aug 07 '17 at 10:07
  • Ok @Constantine if I tape the data of the series such as you it is also working. However, if I put the data in one PHP array it doesn't work and nothing is plotted on my chart ... – Sascha E. Aug 07 '17 at 11:39
  • I really don't understand because my PHP array ($data) looks like that : Array ( [0] => Array ( [0] => 1501771394000 [1] => 1501771426000 [2] => 1501771458000 [3] ) [1] => Array ( [0] => 25.69 [1] => 25.63 [2] => 25.63 [3] => 25.5) ) And this looks like right ... and then in the highchart I inserted it like that : series: [{ name: 'Wall plug consumption', data:[] – Sascha E. Aug 07 '17 at 11:45
  • @Sascha E. because data:[] will look like [Array, Array]. Js don't understand such construction. Try to use csv file or table to transfer your data to js in a correct way – Constantine Aug 07 '17 at 12:08
  • @Sascha E. Or try smth like [that](http://sandbox.onlinephpfunctions.com/code/4458e0ebdaa6e08ccb06bb0cbeab242e0c80ab26) – Constantine Aug 07 '17 at 12:14
  • Your php code doesn't work or it maybe me who don't know how to use it... I will look how I could convert my $data php array into a JS array ... Not easy the javascript ! – Sascha E. Aug 07 '17 at 12:21
  • Sascha E yeah) i know. it won't work. it was just example, how u can change output of your data. i think that using table is better for your situation. Or use ajax with json. It is not very good to implement such php constructions in js code... – Constantine Aug 07 '17 at 12:41
  • It is quite difficult, I don't find any solution for my problem – Sascha E. Aug 07 '17 at 14:49
  • @SaschaE. Try this [code](http://sandbox.onlinephpfunctions.com/code/374addf631dc630ebaf0d496989e5765b7a010f6) to prepare your php array for JS. Hope it will help you. – Constantine Aug 07 '17 at 17:50
  • Ok thank you. If I try it I need to create a new variable with this function ? $js_data = prepare_for_js($data) ?? – Sascha E. Aug 08 '17 at 07:11
  • Thank you for your determination Constantine, but it also doesn't work – Sascha E. Aug 08 '17 at 09:00
  • PHP `strtotime({your_dateTime})*1000` work for me. Thx alot – inMILD Apr 23 '18 at 08:25
0

Refer to Highcharts API on series.data.x

x: Number

The x value of the point. For datetime axes, the X value is the timestamp in milliseconds since 1970.

In your code you first convert DateTime to milliseconds

$date_inside_temp[] = strtotime($row[0]) *1000;

then you add a datetime format in your code

$date_inside_temp[] = date_format($row[0]," Y-m-d H:i:s,");`. 

Then you try to convert that back into milliseconds by using Date.UTC() on the array of mixed dateobjects.

categories: Date.UTC(<?php echo $date_inside_temp;?>),
Community
  • 1
  • 1
ewolden
  • 5,722
  • 4
  • 19
  • 29