2

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.

1
  • what is the mean of My problem is that I can't pass this string for the data input as it waits number and not string? Commented Sep 9, 2013 at 15:53

1 Answer 1

1

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.

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

4 Comments

Thanks for the fast reply. Still, I can't plot anything. I alert the result before returning it and saw that it is actually a one dimensional array - or I am wrong ? I believe that the returned data should be in a form like this : {data: [[47,47,21],[20,12,4],[6,76,91],[38,30,60],[57,98,64]]} while I have this, as provided from the returning result: {data: [47,47,21,20,12,4,6,76,91,38,30,60,57,98,64]} Could you please help me further ?
Hi. If you take a look at the jsFiddle I created, open the javascript console, and run the jsFiddle, you should see that it's returning a 2-dimensional array. You should do a console.debug (instead of an alert) of the results of getData and see if it is really a 2-dimensional array.
I added another example/jsFiddle to my answer showing how the getData function works with highcharts. I think you have an extra [ and ] before and after the call to getData.
I had to add parseFloat(stats[j]) to data assignment and now it's working great. Thank you sir for the interest and the time spend helping me :)

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.