Landitus
Landitus

Reputation: 1900

How to populate a Highcharts axis with string formatted data from a PHP array

I'm trying to convert a php array of items to labels for a Highcharts X-axis. The default example is:

categories: ['Jan','Feb','Mar','Apr', 'May', 'Jun', 
                    'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

So I have a PHP array ($items) and end up with a Javascript array (var items). Firebug outputs the following:

console.log(items);
["item1_10_20", "item2_2011_10_14", "item3_2011_10_07", "item3_2011_09_12"]

I can see that I should have single quotes, instead. How can I do that? Or maybe I should feed Highcharts with a comma and single-quoted separated string instead? How do I do either?


EDIT

I've tried this, but no luck at all!

in my view file I have a javascript script:

var items = "'<? echo join("','", $items) ?>'"; // Note the enclosing single quotes

And in the Highcharts JS file:

xAxis: {
 categories: [items]
},

The whole array is placed in the first value of the x-axis and the rest of the values are blank. The item variable is shown in firebug:

'item1_2011_10_20','item2_2011_10_14','item3_2011_10_07','item4_2x1_2011_09_12'

How weird is this not working?


EDIT

When using json_encode to format the php array, I still can't get highcharts to recognize the array. If I manually copy and hardcode the array with single quotes, it works. But I really need to pass the array from php to highcharts' js via variables.

Here's the resulting x-axis with an array via json_encode: Chart with bad x-axis

Upvotes: 3

Views: 9458

Answers (2)

Marco
Marco

Reputation: 86

I had the same problem with a c# array, I solve in this way with razor :

@{
    string[] myIntArray = new string[5] {"Apples", "Oranges", "Pears", "Grapes", "Bananas" };
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var jsVariable = serializer.Serialize(myIntArray);  
}


xAxis: {
    categories: @Html.Raw(jsVariable)
 },

Upvotes: 1

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

The following is already an array -

["item1_10_20", "item2_2011_10_14", "item3_2011_10_07", "item3_2011_09_12"]

so, change the following

xAxis: {
    categories: [items]
},

as follows -

xAxis: {
    categories: items
},

Update:

xAxis: {
    categories: eval('(' + items + ')')
},

Update2:

One thing you need to know here is that JSON is not Javascript. You get get JSON reponse from server which is nothing more than a string in Javascript. So, to use that JSON response in Javascript you have to parse the JSON to form a Javascript data structure like object, array, etc (Just like we do with XMl), which is called parsing.

There are a lot of JSON parsing libraries, but we can also use eval JS function to do the job, as long as security is not a concern.

And in your case, the JSON response represents an array so you use eval to turn that into a real Javascript array.

So, items is JSON and you use eval (eval('(' + items + ')')) to parse which returns a Javascript array and then you assign the result to categories.

Upvotes: 5

Related Questions