1
votes

I'm using highcharts to display some database informations, so when I use one series I haven't any problems, but when I want to add several series on my chart, it's a little bit difficult.

First my PHP script :

foreach ($informationslier as $keyFille => $fille) {
    $grafData = array();

    foreach ($fille['information'] as $keyInfos => $information) {
        $mois = $information['created']->format('m');
        $mois = $mois - 1;

        $timestamp = strtotime($information['created']->format('j').'-'.$mois.'-'.$information['created']->format('Y'));
        //Multiply by 1000 to get seconds in js
        $timestamp = $timestamp * 1000;
        $grafData[$keyInfos] = array($timestamp, (float)$information['valeur']);
        //$grafData[$keyInfos] = implode(', ', $grafData[$keyInfos]);
        //$grafData[] = "[Date.UTC(".$information['created']->format('Y').','.$mois.','.$information['created']->format('j')."),".$information['valeur']."]";
    }

    $tabInformationsData[$keyFille]['name'] = 'Evolution '.$fille['titre'];
    $tabInformationsData[$keyFille]['color'] = '#FFA020';
    //$tabInformationsData['infos'][$keyFille]['data'] = array();

    //$implodeGraf = implode(',', $grafData);
    $tabInformationsData[$keyFille]['data'] = $grafData;
}

The php script return me something like this :

Array
(
     [0] => Array
         (
            [name] => Evolution pourcentage
            [color] => #FFA020
            [data] => Array
                (
                    [0] => Array
                        (
                            [0] => 1451084400000
                            [1] => 17.00
                        )
                    [1] => Array
                        (
                            [0] => 1451170800000
                            [1] => 19.00
                        )
                    [2] => Array
                        (
                            [0] => 1451257200000
                            [1] => 14.00
                        )
                    [3] => Array
                        (
                            [0] => 1451343600000
                            [1] => 6.00
                        )
                )
        )
)

I send this array to my view and I do this :

series: {{ graphique | json_encode() | raw }},

But in the HTML, the rendering looks like this and doesn't work :

series: [{
    "name":"Evolution pourcentage",
    "color":"#FFA020",
    "data":[[1451084400000,"17.00"],[1451170800000,"19.00"],[1451257200000,"14.00"],[1451343600000,"6.00"]]
}],

I don't know why it doesn't work... I try to multiply by 1000 the timestamp or not but nothing work. So, how can I display all my series ?

EDITED

Work by passing all date to timestamp and adding (float) before all the values... I edited my post with the response.

Thanks

1
Hey, thanks but I found a solution : {{ graphique | json_encode() | raw }}, I still get a problem with two quotes in each of my data arrays, i updated my post. - Cracs
Removing quotes won't be enough. You are trying to use Date.UTC() which is JavaScript function, so it always will be returned as string by PHP. Instead you should calculate timestamp in PHP, so returned will be number. Then there should be: [0] => [1453852800000, 20.00], [1] => [1453939200000, 30.00] ... etc. - Paweł Fus
In the PHP you can use strtotime function which returns unix timestamps. Please note that you need to mutliply all unix timestmaps by 1000 to have javascript format. - Sebastian Bochan
I tried to use timestamp and multiply them by 1000 for the javascript, the array of series seems good, but nothing appear on my chart...I edited my first post again. - Cracs

1 Answers

0
votes

perhaps

  • iterate on your {{ my_array }} twig variable
  • use only unicode character for your JSON : JSON specification