7

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

        }

    });

});
2
  • It seems that you are first concatenating label data, and then adding it to chart data as one string, instead of iterating through label data and adding one by one. Commented Sep 18, 2018 at 6:58
  • I am fetching data one by one from database Commented Sep 18, 2018 at 7:14

1 Answer 1

1

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



        }

    });

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.