Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display dynamically label and values of label in pie chart using chart.js?

My Pie chart enter image description here but actually i want this type of chart enter image description here

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.

$("#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();

        }

    });

});
like image 824
Mohd Atiq Avatar asked Sep 18 '18 06:09

Mohd Atiq


1 Answers

This is my complete code and this is working perfectly. $("#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){
            $("#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();



        }

    });

});
like image 99
Mohd Atiq Avatar answered Nov 14 '22 12:11

Mohd Atiq