Php 在折线图中显示MySQL数据库中的数据

Php 在折线图中显示MySQL数据库中的数据,php,mysql,linechart,canvasjs,Php,Mysql,Linechart,Canvasjs,我试图在折线图中显示MySQL表中的数据。x轴包含员工姓名,y轴包含id号。我遇到了一个问题,字符串(员工姓名)没有显示在x轴上,它只显示数值 到目前为止,我尝试的代码是: <?php $dataPoints = array(); try{ $link = new PDO('mysql:host=localhost;dbname=aa', 'root', ''); $handle = $link->prepare('select * from staff'); $han

我试图在折线图中显示MySQL表中的数据。x轴包含员工姓名,y轴包含id号。我遇到了一个问题,字符串(员工姓名)没有显示在x轴上,它只显示数值 到目前为止,我尝试的代码是:

<?php

 $dataPoints = array();

 try{

$link = new PDO('mysql:host=localhost;dbname=aa', 'root', '');

$handle = $link->prepare('select * from staff'); 
$handle->execute(); 
$result = $handle->fetchAll(PDO::FETCH_OBJ);

foreach($result as $row){

    array_push($dataPoints, array("x"=> $row->Name, "y"=> $row->id));
}
$link = null;
}
catch(PDOException $ex){
print($ex->getMessage());
 }

?>
  <!DOCTYPE HTML>
  <html>
   <head>  
  <script>
   window.onload = function () {

    var chart = new CanvasJS.Chart("chartContainer", {
   animationEnabled: true,
   exportEnabled: true,
    theme: "light1", 
      title:{
    text: "PHP Column Chart from Database"
   },
   xaxis
   data: [{
    type: "line", //change type to bar, line, area, pie, etc  
     yValueFormatString: "$#,##0K",
     indexLabel: "{y}",
     indexLabelPlacement: "inside",
     indexLabelFontWeight: "bolder",
     indexLabelFontColor: "white",

     dataPoints: <?php echo json_encode($dataPoints, JSON_NUMERIC_CHECK); ?>
     }]
     });
     chart.render();

    }
    </script>
    </head> 
       <body>
          <center><div id="chartContainer" style="height: 370px; width: 50%;"></div></center>
    <script src="https://canvasjs.com/assets/script/canvasjs.min.js"> 
      </script>
       </body>
       </html>  

window.onload=函数(){
var chart=new CanvasJS.chart(“chartContainer”{
animationEnabled:没错,
exportEnabled:true,
主题:“light1”,
标题:{
文本:“数据库中的PHP柱形图”
},
xaxis
数据:[{
类型:“直线”//将类型更改为条形、直线、面积、饼图等
yValueFormatString:“$#,###0K”,
索引标签:“{y}”,
indexLabelPlacement:“内部”,
indexLabelFontWeight:“更粗体”,
indexLabelFontColor:“白色”,
数据点:
}]
});
chart.render();
}

如果您看到有关CanvasJS数据点属性的文档,则只接受您应该使用的数字值。 尝试改变

array_push($dataPoints, array("x"=> $row->Name, "y"=> $row->id));


如果您看到关于CanvasJS数据点属性的文档,则只接受您应该使用的数字值。 尝试改变

array_push($dataPoints, array("x"=> $row->Name, "y"=> $row->id));

可以是数字或日期时间值。但在你的情况下,它似乎是一根弦。在您的案例中,可以使用,而不是x值

可以是数字或日期时间值。但在你的情况下,它似乎是一根弦。在您的案例中,可以使用,而不是x值

array_push($dataPoints, array("label"=> $row->Name, "y"=> $row->id));