Php 数据不会绘制到我的图表上
我正在尝试使用chart.js库将数据输出到图表上。我已经成功地提取了所需的数据并创建了数据集,但由于某些原因,它不会呈现在图表上 我有三个文件: teamData.jsPhp 数据不会绘制到我的图表上,php,jquery,mysql,ajax,chart.js,Php,Jquery,Mysql,Ajax,Chart.js,我正在尝试使用chart.js库将数据输出到图表上。我已经成功地提取了所需的数据并创建了数据集,但由于某些原因,它不会呈现在图表上 我有三个文件: teamData.js $(document).ready(function(){ $.ajax({ url : "http://localhost/acredashAdv/teamData.php", type : "GET", success :function(data){
$(document).ready(function(){
$.ajax({
url : "http://localhost/acredashAdv/teamData.php",
type : "GET",
success :function(data){
console.log(data);
var score_1 = [];
var score_2 = [];
for (var i in data) {
score_1.push(data[i].score_1);
score_2.push(data[i].score_2);
}
var chartata = {
labels: [
"Strategic Development and Ownership",
"Driving change through others",
"Exec Disposition",
"Commercial Acumen",
"Develops High Performance Teams",
"Innovation and risk taking",
"Global Leadership",
"Industry Leader"
],
datasets : [
{
label: "user 1",
backgroundColor: "rgba(0,0,0,0)",
borderColor: "#B71C1C",
data: score_1,
},
{
label: "user 2",
backgroundColor: "rgba(0,0,0,0)",
borderColor: "#B71C1C",
data: score_2
},
]
};
var ctx = $("#mycanvas");
var LineGraph = new Chart(ctx, {
type: 'radar',
data: chartata,
animationEasing: 'linear'
});
},
error :function(data){
},
});
});
teamData.php
<?php
include 'config.php';
$query = sprintf("SELECT member_id, firstName, lastName, score_1, score_2, score_3, score_4, score_5, score_6, score_7, score_8 FROM members");
$result = $conn->query($query);
$data = array();
foreach ($result as $row) {
$data[] = $row;
}
$result->close();
$conn->close();
print json_encode($data);
?>
但是,除非我搞混了,否则它不会提取数据。。。我最终得到了一张空的图表
可能是Chart.js的版本,下面是您正在运行的代码: 使用此版本的Chart.js 尝试采纳PabloMartinez的建议,在打印行之前向PHP文件添加标题:
header('Content-type: application/json');
print json_encode($data);
通常,这些东西需要相同数量的数据来绘制和标记,您在控制台中有8个标签,数据中只有3个值3个用户。log datahey man感谢您的回复,我最初有8组数据,但为了节省空间,我减少了它,但它只是做了同样的事情。这真的很棘手…奇怪的是,如果我使用member_id或score_1作为标签,会产生大量逗号,比如,,,,,,,,,,,,,,,,,,,,,尝试放置标题“Content-type:application/json”;在php中,如果不是,则返回文本而不是json@PabloMartinez其中是标题“Content-type:application/json”;你要走了吗?对不起?
[{"member_id":"144","firstName":"Dan","lastName":"Barrett","score_1":"4","score_2":"2","score_3":"3","score_4":"5","score_5":"1","score_6":"3","score_7":"5","score_8":"4"},{"member_id":"145","firstName":"Jon","lastName":"Smith","score_1":"3","score_2":"4","score_3":"1","score_4":"2","score_5":"1","score_6":"2","score_7":"3","score_8":"4"},{"member_id":"146","firstName":"Dan","lastName":"Barrett","score_1":"1","score_2":"2","score_3":"1","score_4":"1","score_5":"4","score_6":"1","score_7":"4","score_8":"3"}]
header('Content-type: application/json');
print json_encode($data);