gpt4 book ai didi

javascript - Chart.js 圆环图无法正常工作

转载 作者:可可西里 更新时间:2023-11-01 14:48:03 25 4
gpt4 key购买 nike

我正在尝试使用 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请帮帮我。

最佳答案

我已经用这个做了,我正在粘贴我的代码

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 函数,只保留内部代码。就是这样

关于javascript - Chart.js 圆环图无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25665019/

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