我知道这个标题似乎有点奇怪,我无法更好地解释它。
这是我需要的。我有这个 javascript 函数,我需要将数据作为字符串变量传递给它:
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], { name: 'Chrome', y: 12.8, sliced: true, selected: true }, ['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);
}
谢谢