【发布时间】:2012-09-27 11:49:06
【问题描述】:
我知道这个标题似乎有点奇怪,我无法更好地解释它。
这是我需要的。我有这个 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]
]
}]
});
}
我需要将此数据作为参数传递给我的函数
[ ['火狐',45.0], ['IE',26.8], { 名称:“铬”, y: 12.8, 切片:真的, 选择:真 }, ['Safari',8.5], ['歌剧',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);
}
谢谢
【问题讨论】:
-
绝对不需要字符串,看library API。
标签: javascript jquery json highcharts