在折线图中显示mysql数据库中的数据

ippsafx7  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(471)

我试图用折线图显示mysql表中的数据。x轴代表职员姓名,y轴代表身份证号码。我遇到了一个问题,字符串(员工姓名)没有显示在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>
f2uvfpb9

f2uvfpb91#

如果你看到关于canvasjs datapoint属性x的文档,只接受数字值,你应该使用label。试着改变

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

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

enxuqcxy2#

x值可以是数字或日期时间值。但在你的情况下,这似乎是一个字符串。在您的案例中,可以使用轴标签,而不是x值。

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

相关问题