我正在尝试使用chart.js甜甜圈图表。我的JSON数据是从数据库中正确获取的。 但是在值的填充时间图表将不会显示。如果我将示例代码静态粘贴,则显示正确的数据。
function playerPrel2Res(qID){
var tmp="#playerVoteId"+qID;
var lab=$(tmp).text();
$.ajax({
type : "POST",
url : contextPath + '/common/playerPredCounter.action', //contextPath for dynamic
dataType : 'json',
data : {predictionID:qID},
success : function(data) {
var id=new Array();
var name=new Array();
for ( var i = 0; i < data.properties.length; i++) {
id[i]= data.properties[i].id;
name[i]= data.properties[i].name;
}
alert("hi...");
var doughnutData = [
{
value: id[0],
color:"#F7464A",
highlight: "#FF5A5E",
label: name[0]
},
{
value: id[1],
color: "#46BFBD",
highlight: "#5AD3D1",
label: name[1]
},
{
value: id[2],
color: "#FDB45C",
highlight: "#FFC870",
label: name[2]
},
];
window.onload = function(){
var ctx = document.getElementById('playerChartArea').getContext("2d");
alert(ctx +"path");
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {responsive : true});
};
}
});
}
这不会显示任何输出,但我写这样静态然后显示图表
var doughnutData = [
{
value: id[0].val(),
color:"#F7464A",
highlight: "#FF5A5E",
label: name[0].val()
},
{
value: 50,
color: "#46BFBD",
highlight: "#5AD3D1",
label: "Green"
},
{
value: 100,
color: "#FDB45C",
highlight: "#FFC870",
label: "Yellow"
},
{
value: 40,
color: "#949FB1",
highlight: "#A8B3C5",
label: "Grey"
},
{
value: 120,
color: "#4D5360",
highlight: "#616774",
label: "Dark Grey"
}
];
window.onload = function(){
var ctx = document.getElementById('playerChartArea').getContext("2d");
alert(ctx +"path");
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {responsive : true});
};
有一件事是ctx
没有显示我的提醒
请帮帮我。
答案 0 :(得分:1)
我已经完成了这个,只是这样做,我粘贴我的代码
var doughnutData="";
$.ajax({
type : "POST",
url : contextPath + '/common/action', //contextPath for dynamic
dataType : 'json',
data : {predictionID:oID},
success : function(data) {
var id=new Array();
var name=new Array();
for ( var i = 0; i < data.properties.length; i++) {
id[i]= data.properties[i].id;
name[i]= data.properties[i].name;
}
doughnutData = [
{
value: id[0],
color:"#F7464A",
highlight: "#FF5A5E",
label: name[0]
},
{
value: id[1],
color: "#46BFBD",
highlight: "#5AD3D1",
label: name[1]
},
{
value: id[2],
color: "#FDB45C",
highlight: "#FFC870",
label: name[2]
},
{
value: id[3],
color: "#949FB1",
highlight: "#A8B3C5",
label: name[3]
},
{
value: id[4],
color: "#4D5360",
highlight: "#616774",
label: name[4]
}
];
var ctx = document.getElementById("canvas").getContext("2d");
window.myDoughnut = new Chart(ctx).Doughnut(doughnutData, {responsive : true});
}
});
所以只需声明doughnutData变量全局并删除windows.load
函数并保留内部代码。那是