Reputation: 17849
I know this title seems a bit odd, i couldn't explain it better.
Here is what i need. I have this javascript function and i need to pass the data as a String variable to it:
var chart;
function drawChart() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
series: [{
type: 'pie',
name: 'Browser share',
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 need to pass this data as a parameter to my function
[ ['Firefox', 45.0], ['IE', 26.8], { name: 'Chrome', y: 12.8, sliced: true, selected: true }, ['Safari', 8.5], ['Opera', 6.2], ['Others', 0.7] ]
How can i do it?
I want it to look like something like this
var chart;
function drawChart(dataString) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
title: {
text: 'Browser market shares at a specific website, 2010'
},
tooltip: {
pointFormat: '{series.name}: <b>{point.percentage}%</b>',
percentageDecimals: 1
},
plotOptions: {
pie: {
allowPointSelect: true,
cursor: 'pointer',
dataLabels: {
enabled: true,
color: '#000000',
connectorColor: '#000000',
formatter: function() {
return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
}
}
}
},
series: [{
type: 'pie',
name: 'Browser share',
data: dataString
}]
});
}
I have tried solution by @moonwave99 :
var browsers = [
['Firefox', 45.0],
['IE', 26.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
];
and
...........
series: [{
type: 'pie',
name: 'Browser share',
data: JSON.stringify(browsers)
}]
............
And my outcome is this:
Solution : http://jsfiddle.net/USzVy/1/
var browsers = [
['Firefox', 45.0],
['IE', 26.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
];
function drawChart() {
var data_str = JSON.stringify(browsers);
var options ={
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
series: [{
type: 'pie',
name: 'Browser share',
data: JSON.parse(data_str)
}]
}
chart = new Highcharts.Chart(options);
}
Thanks
Upvotes: 4
Views: 11827
Reputation: 11
Excelent, solutions, i was tryng your codes, but my chart do not show nothing, so, i was tracking my values that i pass like parameters, and my problem was that i sending my values like string, i solve this just converting my data with the parseInt() command.
function graficar(var1,var2,var3,var4){
var chart;
var datos=[];
datos[0]=parseInt(var1);
datos[1]=parseInt(var2);
datos[2]=parseInt(var3);
datos[3]=parseInt(var4);
var datos_str =JSON.stringify(datos);
alert(datos_str);
chart = new Highcharts.Chart({
chart: {
renderTo: 'grafico',
type: 'column'
},
title: {
text: 'Titulo de ventas'
},
xAxis: {
categories: ['30%', '40%', '50%', '60%']
},
tooltip: {
formatter: function() {
return ''+
this.series.name +': '+ this.y +'';
}
},
credits: {
enabled: false
},
series: [{
name: 'Valores',
data: JSON.parse(datos_str)
.. .. ...
Thanks
Upvotes: 1
Reputation: 12447
var chart;
function drawChart(dataString) {
// options to create the chart
var options = {
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
series: [{
type: 'pie',
name: 'Browser share',
data: dataString // using the parameter
}]
};
chart = new Highcharts.Chart(options);
}
// here you get the dataString some way
var dataString = [
['Firefox', 45.0],
['IE', 26.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
];
drawChart(dataString);
Upvotes: 1
Reputation: 2153
First define your data as an array, not a string:
var 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]];
Then stringify and pass as parameter to your function:
var data_str = JSON.stringify(data);
drawChart(data);
Then convert to back to JSON
series: [{
...
data: JSON.parse(data)
}
Upvotes: 5
Reputation: 22810
Use JSON.stringify
:
var browsers = [
['Firefox', 45.0],
['IE', 26.8],
{
name: 'Chrome',
y: 12.8,
sliced: true,
selected: true
},
['Safari', 8.5],
['Opera', 6.2],
['Others', 0.7]
]
...
data : JSON.stringify(browsers)
...
EDIT see fiddle.
EDIT2: I had a look at this library API, and you don't need a string, but an array of options! Read the docs and you'll have it done.
Upvotes: 3
Reputation: 841
function drawChart(data) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
plotBackgroundColor: null,
plotBorderWidth: null,
plotShadow: false
},
series: [{
type: 'pie',
name: 'Browser share',
data: JSON.parse(data)
}]
});
}
var 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] ]";
drawChart(data);
Upvotes: 1