Dim
Dim

Reputation: 21

Javascript - Highcharts - input data array of arrays

I am trying to feed the highcharts diagram with input data but face one problem. I get the data via json and then add them to data just like this:

function getGraphValues() {
period.deviceID = user.deviceID;
var postData = JSON.stringify(period);
var postArray = {json:postData};
$.ajax({
    type: 'POST',
    url: "getData.php",
    data: postArray,
    success: function(data)
    {
        data1 = JSON.parse(data);
        if (data1.status == "ok" )
        {
        var stats = data1.metrics.split("/");
        $(function () {
            $('#container').highcharts(
        {
            chart: {type: 'bubble',zoomType: 'xy'},
            title: {text: 'Highcharts Bubbles'},
            series: [{data: [getData(stats)]}]
        });});
        }
           else
           {
           alert("error");
           }}
});}   


function getData(stats)
{
var data;
var stats;
 for (var j = 0; j < stats.length; j++) 
 {
 data += "[" + j + "," + stats[j] + "," + stats[j]*8 + "],";
 }
 stats = data.split(",");
return stats;
}

So, in a way like this I do get the stats in this form: [47,47,21],[20,12,4],[6,76,91],[38,30,60],[57,98,64],[61,17,80],[83,60,13],[67,78,75] stored in string variable. My problem is that I can't pass this string for the data input as it waits number and not string. How should I feed the data attribute in an accpetable way ? How can I create an array of array in the getData function if this is needed ? Thank you.

Upvotes: 2

Views: 2817

Answers (1)

zigdawgydawg
zigdawgydawg

Reputation: 2046

If I read your question right, you want getData to return a 2-dimensional array. I believe the following function does what you need:

function getData(stats) {
    var result = [];
    for (var j = 0; j < stats.length; j++) {
        var data = [j, stats[j], stats[j]*8];
        result.push(data);
    }
    return result;
}

And here's a jsFiddle demo.

Here is another example that uses the getData function defined above inside a highcharts bubble chart:

$(function () {
    function getData(stats) {
        var result = [];
        for (var j = 0; j < stats.length; j++) {
            var data = [j, stats[j], stats[j]*8];
            result.push(data);
        }
        console.debug(result);
        return result;
    }

    $('#container').highcharts({
        chart: {type: 'bubble',zoomType: 'xy'},
        title: {text: 'Highcharts Bubbles'},
        series: [{data: getData([1,2,3])}]
    });
});

And the corresponding jsFiddle demo.

Upvotes: 1

Related Questions