canvasjs饼图从mysql获取数据

p5fdfcr1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(347)
  1. $con = mysqli_connect("localhost","root","","final_osa");
  2. if (mysqli_connect_errno($con))
  3. {
  4. echo "Failed to connect to DataBase: " . mysqli_connect_error();
  5. }else
  6. {
  7. $data_points = array();
  8. $result = mysqli_query($con, "SELECT * FROM scholars_list");
  9. while($row = mysqli_fetch_array($result))
  10. {
  11. $point = array("label" => $row['course'], "y" => $row['student_name']);
  12. array_push($data_points, $point);
  13. }
  14. echo json_encode($data_points, JSON_NUMERIC_CHECK);
  15. }
  16. mysqli_close($con);

我怎样才能做一个饼图来显示一门课程中有多少学生被学者录取的百分比?谢谢
这是我的js$(document).ready(function(){

  1. $.getJSON("data.php", function (result) {
  2. var chart = new CanvasJS.Chart("chartContainer", {
  3. animationEnabled: true,
  4. exportEnabled: true,
  5. data: [
  6. {
  7. type: "pie",
  8. showInLegend: "true",
  9. legendText: "{label}",
  10. indexLabelFontSize: 16,
  11. indexLabel: "{label} - #percent%",
  12. yValueFormatString: "฿#,##0",
  13. dataPoints: result
  14. }
  15. ]
  16. });
  17. chart.render();
  18. });
  19. });
uyhoqukh

uyhoqukh1#

像这样格式化数据
假设$records包含您的数据:

  1. $data=array();
  2. foreach ($records as $record) {
  3. $data[] = array(
  4. "label"=>$record["student_name"],
  5. "y"=>$record['course'], //percentage value
  6. );
  7. }
  8. echo json_encode($data);

js部分:

  1. var chart = new CanvasJS.Chart("chartContainer", {
  2. title:{
  3. text: "Monthly User work details"
  4. },
  5. data: [
  6. {
  7. // Change type to "doughnut", "line", "splineArea", etc.
  8. type: "pie",
  9. showInLegend: "true",
  10. legendText: "{label}",
  11. indexLabelFontSize: 16,
  12. indexLabel: "{label} - #percent%",
  13. yValueFormatString: "฿#,##0",
  14. dataPoints: JSON.parse(result)
  15. }
  16. ]
  17. });
  18. chart.render();
展开查看全部

相关问题