如何使用mysqldatabase中的数据源显示图表JavaScript饼图上的百分比和值

时间:2017-09-06 11:31:29

标签: javascript jquery

我想要计算每个用户在我的表中的条目数量,并使用chartjs饼图来显示金额和百分比。我搜索了差不多一个星期但没有成功。

$(document).ready(function(){
    $.ajax({
        url : "http://localhost:8080/targetadmin3/theme/flowerdata.php",
        type : "GET",
         dataType: "json",
        success : function(data){
            console.log(data);

            var number = [];
            var inserted_by = [];
                        var arrayColor=[];
            var myColor=function (){   
                        return "rgba("+Math.floor((Math.random() * 256) + 1).toString()+"," +Math.floor((Math.random() * 256) + 1).toString()+","+Math.floor((Math.random() * 256) + 1).toString()+",1)";
                        };
            //var twitter_follower = [];
            //var googleplus_follower = [];



            for(var i in data) {
                inserted_by.push("User " + data[i].inserted_by);
                number.push(data[i].number);
                arrayColor.push(myColor());
                //twitter_follower.push(data[i].twitter);
                //googleplus_follower.push(data[i].googleplus);


                //console.log(googleplus_follower);
            }



            var chartdata = {
                labels: inserted_by,
                datasets: [
                    {
                        label: "number",
                        fill: false,
                        lineTension: 0.1,
                        backgroundColor: arrayColor,
                        borderColor: "rgba(59, 89, 152, 1)",
                        pointHoverBackgroundColor: "rgba(59, 89, 152, 1)",
                        pointHoverBorderColor: "rgba(59, 89, 152, 1)",
                        data: number
                    },

                ]
            };

            var ctx = $("#mycanvas");

            var LineGraph = new Chart(ctx, {
                type: 'pie',
                data: chartdata                
            });


        },
        error : function(data) {

        }
    });
});

0 个答案:

没有答案