french_dev
french_dev

Reputation: 2177

return json arrays from symfony controller for many usage on highcharts.js

This is my code from my symfony controller:

$em=$this->getDoctrine()->getManager();

$queryIndex = $em->createQuery( 'SELECT g.index
                                    FROM MySpaceMyBundle:Graphique g');

$array = array_map('current', $queryIndex);

$response = new Response();
$data = json_encode($array, JSON_NUMERIC_CHECK);
$response->headers->set('Content-Type', 'application/json');
$response->setContent($data);

return $response;

This php script returns me an array with numeric caracters, so this code returns me the following json response:

[1700,1200,1200,1304,1800,2012,2048,1048,3000,5421]

But how can I proceed if I have many queries in my controller in order to use them for different things in highchart.js?

That is to say, I want:

  • the first response of the first doctrine query in order to have the series (column of the graphic)
  • the second response of a second doctrine query is made for set datas in yAxis
  • the third response of a third doctrine query is made for set datas in xAxis

So I would like to return many arrays corresponding to many doctrine queries, but in order to use them for set my chart in my view.

Note that the result I would like is not an array of arrays, I would like to return many arrays like this and use them in my highchart script:

/*the first array of my first query*/
[1700,1200,1200,1304,1800,2012,2048,1048,3000,5421]

/*the second array of my second query*/
[200,558,...........,154]

/*the third array of my third query*/
[another array of numeric values]

...

Not this:

{ "my_first_result": [1700,1200,1200,1304,1800,2012,2048,1048,3000,5421], "my_other_result" : [200,558,...........,154] }

or

[[first array], [second array], ...]

This is the script for render a chart in my view:

$(document).ready(function() {
    var options = {
        chart: {
            renderTo: 'container',
            type: 'column'
        },
        series: [{}]
    };

    var url =  "{{ path('myPathToMyPage') }}";
    $.getJSON(url,  function(data) {
        options.series[0].data = data;
        var chart = new Highcharts.Chart(options);
    });
});

So here, the only array I have for now is from my first query, corresponding to numeric value in my database and used here for the serie of highchart graphic (column chart). Now I would like to make two another queries in order to set the yAxis and the xAxis.

Upvotes: 1

Views: 702

Answers (1)

maalls
maalls

Reputation: 759

encoded json is just plain text, you can do like this for example:

$data = json_encode($array, JSON_NUMERIC_CHECK) . "\n" . json_encode($array2, JSON_NUMERIC_CHECK);

In this example, json string are delimited by a line break.

Upvotes: 1

Related Questions