0

I want to implement plugin chart into my code, the chart code is like

 chart = new Highcharts.Chart({
            },
            series: [{
                type: 'pie',
                name: 'criterin',
                data: [
                    ['Firefox',   45.0],
                    ['IE',       26.8],
                    {
                        name: 'Chrome',
                        y: 12.8,
                        sliced: true,
                        selected: true
                    },
                    ['Safari',    8.5],
                    ['Opera',     6.2],
                    ['Others',   0.7]
                ]
            }]
        });

I have array that I got from ajax function which contains the information that I want to replace into the chart.

for example, I alert sample array and result like:

post work,0.64,quality,0.35

How can I use my array to integrate with chart code.

1
  • I suggest you to format on your backend and avoid process on client side. Commented Oct 22, 2012 at 16:14

1 Answer 1

1

So, if I'm understanding you correctly, you have an array that looks like this

['post work',0.64,'quality',0.35]

And you want it to look like this:

[
    ['post work', 0.64],
    ['quality', 0.35]
]

Something like this should work

function array1Dto2D(sourceArray) {
    var array2D = [];
    for (var i=0; i < sourceArray.length - 1; i+=2) {
        var pair = [sourceArray[i], sourceArray[i+1]];
        array2D.push(pair);
    }
    return array2D;
}    

and be used something like this (Note I don't use highcharts, but I don't think the way you had it in your question was right, with the empty object as a first argument):

 chart = new Highcharts.Chart({
        series: [{
            type: 'pie',
            name: 'criterin',
            data: array1Dto2D(my1dsourcedata)
        }]
    });
Sign up to request clarification or add additional context in comments.

3 Comments

If you find yourself doing a lot of different array operations and need more tools I'd take a peek at underscore.js as well.
it gives me 2D array, but when I replaced into the chart code. the chart doesn't load. I don't know why
@Kamal: What do you mean by replaced into the chart code? The data property of your object literal you are passing to the charting library should be set to the output of this function.

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.