doufei4418
doufei4418
2015-10-05 11:21

使用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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • drnxnbf199049 drnxnbf199049 6年前

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

    点赞 评论 复制链接分享