我知道这个标题看起来有点奇怪,我无法解释它。
这就是我需要的。我有这个javascript函数,我需要将数据作为String变量传递给它:
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]
]
}]
});
}
我需要将此数据作为参数传递给我的函数
[ [' Firefox',45.0], [' IE',26.8], { 名称:' Chrome', y:12.8, 切片:真的, 选中:是的 }, [' Safari',8.5], [' Opera',6.2], ['其他',0.7] ]
我该怎么做?
我希望它看起来像这样
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
}]
});
}
我已经尝试过@ 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]
];
和
...........
series: [{
type: 'pie',
name: 'Browser share',
data: JSON.stringify(browsers)
}]
............
我的结果是:
解决方案: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);
}
由于
答案 0 :(得分:5)
首先将数据定义为数组,而不是字符串:
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]];
然后将字符串化并作为参数传递给您的函数:
var data_str = JSON.stringify(data);
drawChart(data);
然后转换回JSON
series: [{
...
data: JSON.parse(data)
}
答案 1 :(得分:3)
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)
...
编辑see fiddle。
EDIT2:我看过这个library API,你不需要一个字符串,但需要一系列选项!阅读文档,你就可以完成它。
答案 2 :(得分:1)
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);
答案 3 :(得分:1)
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);
答案 4 :(得分:1)
Excelent,解决方案,我尝试了你的代码,但是我的图表没有显示任何内容,因此,我正在跟踪我传递的参数值,我的问题是我发送我的值像字符串,我解决这个问题使用parseInt()命令转换我的数据。
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)
.. .. ...
谢谢