我正在使用 CanvasJs 并在连接 MySQL 时遇到问题,我的值来了,但我的图表没有显示


$'dbhost' = 'localhost'; $dbname = 'chart'; $dbuser = 'root'; $dbpass = '';
try{ 
$dbcon = new PDO("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass); 
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} 
catch(PDOException $ex)
{ die($ex->getMessage());
} 
$stmt=$dbcon->prepare("SELECT * FROM contribution "); 
$stmt->execute(); 
while ($row=$stmt->fetch(PDO::FETCH_ASSOC))
{ 
extract($row); 
$json[] = $student; 
$json2[] = (int)$contribution; 
} 
echo json_encode($json); echo json_encode($json2); ?>
window.onload = function () 
{ 
var chart = new CanvasJS.Chart("chartContainer", 
{ 
animationEnabled: true
, exportEnabled: true
theme: "light1", // "light1", "light2", "dark1", "dark2" 
title:{ text: "PHP Column Chart from Database" }
, data: [{ type: "column", //change type to bar, line, area, pie, etc 
dataPoints: }]
}
); 
chart.render(); 
}

正确对齐代码后,您会看到 和 标题之前缺少两个逗号

并且您必须将构建的 json 传递给数据点。

animationEnabled: true
, exportEnabled: true
,theme: "light1" // "light1", "light2", "dark1", "dark2" 
,title:{ text: "PHP Column Chart from Database" }
, data: [{ type: "column",
dataPoints:<?php echo json_encode($json2); ?>
}]

但这只是一个例子,你错过了什么。

您必须检查您的$jsons格式是否正确

相关内容

最新更新