Reputation: 363
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.
Upvotes: 0
Views: 796
Reputation: 45135
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)
}]
});
Upvotes: 1