0

I have the following data:

var data = {"categories":["GLA Age: 65+"],"series":[{"name":"Hendon","data":[11.98]},{"name":"High Barnet","data":[17.86]},{"name":"Mill Hill","data":[13.73]},{"name":"Oakleigh","data":[17.42]},{"name":"Totteridge","data":[17.76]}]};

I need the 'names' to be the categories names so I'm using 'events > load' to apply this to the highcharts code:

var seriesData = this.series;
var tCategories = [];
for (i = 0; i < seriesData.length; i++) {
    tCategories.push(seriesData[i].name);
}
this.xAxis[0].setCategories(tCategories);

Then I need to set the 'data' into one array like

[11.98, 17.86, 13.73, 17.42, 17.76]

The problem I've come across is that the first lot of data shows in the first bar:

example here: http://jsfiddle.net/zidski/13rexxyo/3/

The code to create the new array ([11.98, 17.86, 13.73, 17.42, 17.76])

var arrayofArrays = data.series.map(function (item) {
     return JSON.stringify([item.data]);
});

    var seriesString = arrayofArrays;
    seriesString = seriesString;
    var n = JSON.stringify(seriesString).replace(/\[|]|"/g, '');
    var g = '[' + n + ']';
    var h = JSON.stringify(g);

    var seriesC = JSON && JSON.parse(g) || $.parseJSON(g);

    this.series[0].setData(seriesC);

1 Answer 1

2

It seems like it's being over-complicated, if I understand correctly (though your desired outcome is not entirely clear).

You can loop through the JSON and return a data array easily enough without using stringify or anything like that, and without needing a load event:

Code:

var title = rawData.categories[0];
var chartData = []
$.each(rawData.series, function(i, node) {
  chartData.push({ 'name': node.name, 'y': node.data[0] })
});

And then:

series: [{
    data: chartData
}]

Updated fiddle:

Output:

screenshot

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

Comments

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.