gpt4 book ai didi

javascript - 将数据字符串作为参数传递给 HighCharts 的函数

转载 作者:搜寻专家 更新时间:2023-11-01 04:57:58 25 4
gpt4 key购买 nike

我知道这个标题看起来有点奇怪,我无法更好地解释它。

这是我需要的。我有这个 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], { name: 'Chrome', y: 12.8, sliced: true, selected: true }, ['Safari', 8.5], ['Opera', 6.2], ['Others', 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)
}]
............

我的结果是这样的:

enter image description here

解决方案: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);
}

谢谢

最佳答案

首先将您的数据定义为数组,而不是字符串:

 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)
}

关于javascript - 将数据字符串作为参数传递给 HighCharts 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12620704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com