1

Basically I have a object which defines some properties for a Highchart chart:

var options = {
    chart: {
        renderTo: 'container',
        type: 'line',
        marginRight: 130,
        marginBottom: 25
    },
    title: {
        text: 'Monthly Average Temperature',
        x: -20 //center
    },
    subtitle: {
        text: 'Source: WorldClimate.com',
        x: -20
    },
    xAxis: {
        categories: []
    },
    yAxis: {
        title: {
            text: 'Temperature (°C)'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    tooltip: {
        formatter: function () {
            return '<b>' + this.series.name + '</b><br/>' + this.x + ': ' + this.y + '°C';
        }
    },
    legend: {
        layout: 'vertical',
        align: 'right',
        verticalAlign: 'top',
        x: -10,
        y: 100,
        borderWidth: 0
    },
    series: [{
        name: 'Tokyo',
        data: aCosts
    }]
};

And I want to update this object from a file (ajax call) like so:

$.getJSON(
    "handler.php",
    function (oJSON) {
        var sName,
            sCost,
            aRow;
        for (sName in oJSON) {
            aRow = [];
            //categories = [];
            if (oJSON.hasOwnProperty(sName)) {
                aRow.push(sName);
                categories.push(sName);
            }
            // Create the chart
            var chart = new Highcharts.Chart(options);
        }
    }
);

The problem is, I can't pass the new value to options as it states that categories is not defined. I've tried using:

options.categories.push(sName)
options[categories].push(sName) 

And none work.

How can I update the value of categories inside my options object?

2 Answers 2

5

Assuming that options is visible from the $.JSON call, this should work:

options.xAxis.categories.push( sName );

If you look at the structure of your options object, you see, that categories is below the property xAxis. Thus you should address is like that.

Sign up to request clarification or add additional context in comments.

Comments

0

You just need to define categories first like so:

options.categories = [];

then you can push things into this array.

Edit: You could place categories: [] in your json to begin with also.

1 Comment

I did not see that categories falls under the xAxis object already in your JSON. So as others have already pointed out, you just need to access it in the right spot, within xAxis.

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.