Phobie
Phobie

Reputation: 99

Highcharts with PHP var

I am novice with JavaScript, and in my website I need to use highchart with some PHP var. And I have a question : With Highchart, I need to complete some information :

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

I have my array in PHP coverted in JavaScript with :

var array = <?php echo json_encode($array); ?>;

But, the length of my array is never the same length between differents users. And I need to complete "categories" with all the values in my array.

How can I do this?

Upvotes: 0

Views: 111

Answers (2)

Phobie
Phobie

Reputation: 99

My actual code is the following :

xAxis: {
    categories: [<?php foreach($array as $value) {
        echo "'".$value."', ";
    }?>]
},

But it doesn't work, because the graph don't appear. :/

EDIT : Solution is :

var array = <?php echo json_encode($array); ?>;

And then

xAxis: {
    categories: array 
},

Upvotes: 0

jlbriggs
jlbriggs

Reputation: 17800

The categories will only show on the chart if there is also data for them in the series. For instance, if you supply 4 categories, but only 3 data points, only 3 categories will display.

If you want to show 4 categories, but you only have 3 data points currently, you can set the 4th data point to 0 or null, and the 4th category will display.

If this doesn't answer your question you'll need to be more specific and provide a fiddle or live example.

Upvotes: 1

Related Questions