我正在尝试向我的数据添加图表。该数据是从 phpmyadmin 导入的。数据经过,我在它下面有表格。当我尝试添加数据时,出现错误,因为它将我的 php 转换为 [<br />
.
<script type='text/javascript'>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [<?php echo $Timers; ?>],
datasets: [{
label: 'Time',
data: [<?php echo $Wheights; ?>],
PHP
while ($stelling = mysqli_fetch_array($records1)){
$Timer = date('M, Y', strtotime($stelling['Timer']));
$Wheight = $stelling['Wheight'];
$Title = $stelling['Title'];
$Timers = '';
$Wheights = '';
$Titles = '';
$Timers = $Timers.'"'.$Timer.'",';
$Wheights = $Wheights.$Wheight.',';
$Titles = $Titles.$Title.',';
$Timers = trim($Timers, ",");
$Wheights = trim($Wheights, ",");
$Titles = trim($Titles, ",");
}include ("script/graph.php");
}
这是错误:
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: [<br />
< etc
最佳答案
您的 javascript 代码似乎存在问题。它没有正确地将 php 数组转换为 javascript。
试试这个:
<script type='text/javascript'>
var ctx = document.getElementById('myChart').getContext('2d');
var labels = '<?php echo json_encode($Timers); ?>';
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
datasets: [{
label: 'Time',
data: [<?php echo $Wheights; ?>],
关于javascript - 尝试通过 php 在数据(chart.js)中输入数据时如何修复[<br/>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56445200/