简体   繁体   English

Chart.js甜甜圈图表不能正常工作

[英]Chart.js Doughnut chart not properly work

I am trying to use chart.js Doughnut chart. 我正在尝试使用chart.js甜甜圈图表。 my JSON data is properl fetch from data base. 我的JSON数据是从数据库中正确获取的。 But at the fill time of value chart will not display. 但是在值的填充时间图表将不会显示。 If i am pasting example code static then its showing proper data. 如果我粘贴示例代码静态,那么它显示正确的数据。

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

        }
    });
}

this will not display any out put but i write like this static then show chart 这将不显示任何输出,但我写这样静态然后显示图表

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

and one thing is that my alert is not display for ctx Please help me out. 一件事是我的警报不显示为ctx请帮帮我。

i have done by using this just do this, i'm pasting my code 我这样做只是这样做,我粘贴我的代码

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

so just declare doughnutData variable global and remove windows.load function and just remain internal code. 所以只需声明doughnutData变量全局并删除windows.load函数并保留内部代码。 that's it 而已

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM