Javascript Chart.js甜甜圈图表无法正常工作

Javascript Chart.js甜甜圈图表无法正常工作,javascript,html,json,Javascript,Html,Json,我正在尝试使用chart.js甜甜圈图表。我的JSON数据是从数据库中正确获取的。 但在填充时,值图表将不显示。如果我粘贴的示例代码是静态的,那么它将显示正确的数据 function playerPrel2Res(qID){ var tmp="#playerVoteId"+qID; var lab=$(tmp).text(); $.ajax({ type : "POST", url : contextPath + '/common/play

我正在尝试使用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});
        };              

        }
    });
}
还有一件事是我的警报不会显示在
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});
        }
    });
var doughnutData=”“;
$.ajax({
类型:“POST”,
url:contextPath+'/common/action',//动态的contextPath
数据类型:“json”,
数据:{predictionID:oID},
成功:功能(数据){
var id=新数组();
var name=新数组();
对于(var i=0;i

所以只需声明doughnutData变量为全局变量,并删除
windows.load
函数,只需保留内部代码即可。就这样

我也有同样的问题。如果值来自数据库,甜甜圈图表不显示,我打印数组,则值是正确的。但是如果我设置静态值,它就会显示。任何人都可以……嘿。。我通过对值使用
intval($value)
成功地显示了甜甜圈。我正在使用PHP生成数据数组。也许您可以通过获取值的整数值在Javascript中执行相同的操作。我现在不知道如何在Javascript中实现它。
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});
        }
    });