嘿,我需要一些帮助,试图弄清楚如何将字符串传递给图表的数组字段。我使用的图表是[http://www.highcharts.com/]
这是图表代码:
function drawChart(theDATA)
{
var chart = new Highcharts.Chart({
chart: {renderTo: 'container',defaultSeriesType: 'column'},
title: {text: 'March 2010 Confirmed User Visit\'s'},
xAxis: {categories: ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']},
yAxis: {min: 0,title: {text: 'User Visit\'s'}},
legend: {layout: 'vertical',backgroundColor: '#FFFFFF',style: {left: '100px',top: '70px',bottom: 'auto'}},
tooltip: {formatter: function() {return this.y;}},
plotOptions: {column: {pointPadding: 0.2,borderWidth: 0}},
series: [{name: 'Confirmed Users Visit\'s',
data: [theDATA]
}]
});
}
使用上面的代码,您会认为DATA可以正常工作,但它不会在其中显示数据。
假设DATA是'2,5,4,7,8,9'。如果我有这个:
data: [2,5,4,7,8,9]
然后它会工作得很好。但如果我使用相同的数字......
theData = "2,5,4,7,8,9"
data: [theData]
它在图表上什么也没做......
那么我做错了什么????
大卫
修改
加
var theDATA = theDATA.split(",");
for ( var t = 0; t < theDATA.length; ++t ) theDATA[t] = parseInt(theDATA[t]);
所以...
function drawChart(theDATA)
{
var theDATA = theDATA.split(",");
for ( var t = 0; t < theDATA.length; ++t ) theDATA[t] = parseInt(theDATA[t]);
var chart = new Highcharts.Chart({
chart: {renderTo: 'container',defaultSeriesType: 'column'},
title: {text: 'March 2010 Confirmed User Visit\'s'},
xAxis: {categories: ['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']},
yAxis: {min: 0,title: {text: 'User Visit\'s'}},
legend: {layout: 'vertical',backgroundColor: '#FFFFFF',style: {left: '100px',top: '70px',bottom: 'auto'}},
tooltip: {formatter: function() {return this.y;}},
plotOptions: {column: {pointPadding: 0.2,borderWidth: 0}},
series: [{name: 'Confirmed Users Visit\'s',
data: [theDATA]
}]
});
}
的
答案 0 :(得分:0)
数据需要数组。
尝试:
data: theDATA.split(',');
如果您传递字符串。
您还可以在将数据传递给drawChart函数
之前将其转换为数组我发现了问题
data: [3,4,6]
有效,但是:
data["3","4","6"]
不起作用,这就是你在split之后得到的,一个字符串数组,但它需要一个整数数组
一个解决方案是
data: theDATA.split(",").map(function(element){ return parseInt(element)})
另一个解决方案是你从chartData.asp返回JSON数组。 因此,不返回“2,3,4,5,6”,而是返回[3,4,5,6,7],并使用
调用函数drawChart(eval(xmlhttp.responseText));
结合
data: thisData