简体   繁体   中英

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

My Pie chart 在此输入图像描述 but actually i want this type of chart 在此输入图像描述

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

        }

    });

});

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



        }

    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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