如何使用chart.js动态显示饼图中标签的标签和值?

时间:2022-12-03 17:52:10

My Pie chart 如何使用chart.js动态显示饼图中标签的标签和值? but actually i want this type of chart 如何使用chart.js动态显示饼图中标签的标签和值?

我的饼图,但实际上我想要这种类型的图表

I want to display dynamically label and label values in pie chart using chart js but according to my code which i have written ,it display all label in one label. I don't know where is the issue in my code.I don't know as much about js. Please guide me.Thanks in advance.

我想使用图表js在饼图中动态显示标签和标签值,但根据我编写的代码,它会在一个标签中显示所有标签。我不知道我的代码中的问题在哪里。我不太了解js。请指导我。谢谢。

$("#get_data").click(function(){

    var employees = $("#employees").val();
    //var fairs     = $("#fairs").val();

    $.ajax({

        url     : 'php_script/chart_values.php',
        method  : 'POST',
        data    : {employees:employees},

        success : function(data){

            var obj = JSON.parse(data);

            var a = obj[0]; // labele data "Negotiation on 
            proposal","Won","Contracted","Intersted",
            var b = obj[1]; // label values "100","90","70"
            var labeldata;
            for( i=0; i<a.length;i++){ // loop to fetch label data one by one
                labeldata += [a][i];

            }
            console.log(labeldata);

            var chart = new CanvasJS.Chart("chartContainer", {
            title: { 
                //text: "Worldwide Smartphone sales by brand - 2012",
                fontSize:15
            }, 
            axisY: { 
                title: "Products in %" 
            }, 
            legend :{ 
                verticalAlign: "center", 
                horizontalAlign: "right" 
            },
            data: [{
            type: "pie", 
            showInLegend: true, 
            toolTipContent: "{label} <br/> {y} %", 
            indexLabel: "{y} %", 
            dataPoints: [ 
                { 

                 label: [labeldata],y:19 // dispaly lable data here


                } 
             /*{ label: "Apple",    y: 19.1, legendText: "Apple"  }, 
            { label: "Huawei",   y: 4.0,  legendText: "Huawei" }, 
            { label: "LG",       y: 3.8,  legendText: "LG Electronics"}, 
            { label: "Lenovo",   y: 3.2,  legendText: "Lenovo" }, 
            { label: "Others",   y: 39.6, legendText: "Others" }  */
           ]
        } 
     ] 
}); 
chart.render();

        }

    });

});

1 个解决方案

#1


0  

This is my complete code and this is working perfectly. $("#get_data").click(function(){

这是我的完整代码,这是完美的。 $( “#GET_DATA”)。点击(函数(){

    var employees = $("#employees").val();
    //var fairs     = $("#fairs").val();

    $.ajax({

        url     : 'php_script/chart_values.php',
        method  : 'POST',
        data    : {employees:employees},

        success : function(data){
            $("#page_content").fadeIn();
            $("#bar_chart_div").fadeOut();
            var obj = JSON.parse(data);

            var a = obj[0]; // labele data "Won","Contracted","Intersted"
            var b = obj[1]; // label values "100","90","70"
            var labeldata=[];
            for( i=0; i<a.length;i++){ 

                labeldata.push({label:a[i],y:parseInt(b[i]),legendText:a[i]});
            }

            debugger;
           console.log(JSON.stringify(labeldata));

            var chart = new CanvasJS.Chart("chartContainer", {
                title: { 

                    fontSize:15
                }, 
                axisY: { 
                    title: "Products in %" 
                }, 
                legend :{ 
                    verticalAlign: "center", 
                    horizontalAlign: "right" 
                },
                data: [{
                type: "pie", 
                showInLegend: true, 
                toolTipContent: "{label} <br/> {y} ", 
                indexLabel: "{y} %", 
                dataPoints: labeldata



        }] 
}); 
chart.render();



        }

    });

});

#1


0  

This is my complete code and this is working perfectly. $("#get_data").click(function(){

这是我的完整代码,这是完美的。 $( “#GET_DATA”)。点击(函数(){

    var employees = $("#employees").val();
    //var fairs     = $("#fairs").val();

    $.ajax({

        url     : 'php_script/chart_values.php',
        method  : 'POST',
        data    : {employees:employees},

        success : function(data){
            $("#page_content").fadeIn();
            $("#bar_chart_div").fadeOut();
            var obj = JSON.parse(data);

            var a = obj[0]; // labele data "Won","Contracted","Intersted"
            var b = obj[1]; // label values "100","90","70"
            var labeldata=[];
            for( i=0; i<a.length;i++){ 

                labeldata.push({label:a[i],y:parseInt(b[i]),legendText:a[i]});
            }

            debugger;
           console.log(JSON.stringify(labeldata));

            var chart = new CanvasJS.Chart("chartContainer", {
                title: { 

                    fontSize:15
                }, 
                axisY: { 
                    title: "Products in %" 
                }, 
                legend :{ 
                    verticalAlign: "center", 
                    horizontalAlign: "right" 
                },
                data: [{
                type: "pie", 
                showInLegend: true, 
                toolTipContent: "{label} <br/> {y} ", 
                indexLabel: "{y} %", 
                dataPoints: labeldata



        }] 
}); 
chart.render();



        }

    });

});