1

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

6
  • 1
    Parse json array. Refer: stackoverflow.com/questions/18064226/… Commented Jan 28, 2016 at 10:07
  • 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. Commented Jan 28, 2016 at 16:00
  • 2
    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. Commented Jan 29, 2016 at 10:26
  • 1
    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. Commented Jan 29, 2016 at 10:49
  • 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. Commented Feb 3, 2016 at 15:34

1 Answer 1

0

perhaps

  • iterate on your {{ my_array }} twig variable
  • use only unicode character for your JSON : JSON specification
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.