luckasx
luckasx

Reputation: 369

Is it possible? xAxis categories with Json?

Highcharts;

I'm trying to change categories dynamically in a column chart with a json. Something like this...

[{y:1,"other":"other","category":"namecategory"}....]

..

  xAxis: {
                    categories: this.category,
                    maxZoom: 1
                },

Is it possible?

Thanks. :D

Upvotes: 0

Views: 1558

Answers (3)

Alberto Montellano
Alberto Montellano

Reputation: 6266

Ok, maybe you can try it something like this:

var jsonData = [{
    "y": "1",
        "other": "other",
        "category": "namecategory1"
}, {
    "y": "2",
        "other": "another",
        "category": "namecategory2"
}];

// var jsObject = JSON.parse(jsonData);
var categories = new Array();
for (var p in jsonData) {
    categories.push(jsonData[p].category);
}
chart.xAxis[0].setCategories(categories);

Upvotes: 0

Sebastian Bochan
Sebastian Bochan

Reputation: 37588

You can dynamically load your categoreis i.e by json or other and then use setCategories which allows to change categoreis, dynamically.

Upvotes: 1

Alberto Montellano
Alberto Montellano

Reputation: 6266

I think you need to set categories as an Array of primitive js types such as number or string.

Example:

categories: ['Apples', 'Bananas', 'Oranges']

As it is mentioned here: http://api.highcharts.com/highcharts#xAxis.categories : "If categories are present for the xAxis, names are used instead of numbers for that axis."

Maybe, when you mention JSON are you talking about Object Literals? (because "y" key in your sample is not quoted.)

In fact , you CAN use an Array of Object Literals. But they will be displayed as "[Object]" in the xAxis.

An object literal is a pair of curly braces surrounding zero or more name/value pairs. An object literal can appear anywhere an expression can appear:

var empty_object = {};
var stooge = {
first_name: "Jerome",
last_name: "Howard"
};

The quotes around a property’s name in an object literal are optional if the name would be a legal JavaScript name and not a reserved word.

Maybe you can pre-process your JSON data an build an Array to be used in categories.

Upvotes: 0

Related Questions