使用chart.js在php中使用mysql数据的饼图[关闭]

使用chart.js在php中使用mysql数据的饼图[关闭]

问题描述:

I wish to add pie chart using charts.js in my project. I searched and found some code for simple chart. Part of my code for chart is below:

printf( '<script type="text/javascript" src="extlib/Chart.js"></script>' );
printf( '<script type="text/javascript" src="extlib/jquery-min.js"></script>' );
printf( '<p style="text-align: center;">' );
printf( '<canvas id="mycanvas" width="256" height="256"></canvas>' );
printf( '<script type="text/javascript">' );
 ?>
    var ctx = $("#mycanvas").get(0).getContext("2d");
    var piedata = [
    {
    value:30,
    color:"green",
    label:"sta1",
    labelColor : 'white'
    },
    {
    value:60,
    color:"red",
    label:"sta1",
    labelColor : 'white'
    },
    {
    value:50,
    color:"blue"
    }
    ]

    new Chart(ctx).Pie(piedata);
    <?php
    printf( '</script>' );  

What can I do for dynamic chart.That is the value should come from MySQL DB. Number of data value from DB will differ every time.Each data value has specific colors.

With the above code the label is not visible explicitly(only on mouse over).

Please suggest me the way.

Iterate over your data coming from json response and push the data in an Array

var piedata = [];
$.each(yourJson,function(i,val){
piedata.push({value:val.value,color:val.color,label:val.label,labelColor:val.labelColor  });
});

and then set new Chart(ctx).Pie(piedata);