$con = mysqli_connect("localhost","root","","final_osa");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to DataBase: " . mysqli_connect_error();
}else
{
$data_points = array();
$result = mysqli_query($con, "SELECT * FROM scholars_list");
while($row = mysqli_fetch_array($result))
{
$point = array("label" => $row['course'], "y" => $row['student_name']);
array_push($data_points, $point);
}
echo json_encode($data_points, JSON_NUMERIC_CHECK);
}
mysqli_close($con);
我怎样才能做一个饼图来显示一门课程中有多少学生被学者录取的百分比?谢谢
这是我的js$(document).ready(function(){
$.getJSON("data.php", function (result) {
var chart = new CanvasJS.Chart("chartContainer", {
animationEnabled: true,
exportEnabled: true,
data: [
{
type: "pie",
showInLegend: "true",
legendText: "{label}",
indexLabelFontSize: 16,
indexLabel: "{label} - #percent%",
yValueFormatString: "฿#,##0",
dataPoints: result
}
]
});
chart.render();
});
});
1条答案
按热度按时间uyhoqukh1#
像这样格式化数据
假设$records包含您的数据:
js部分: