【问题标题】:Display data from MySQL database in line chart以折线图显示 MySQL 数据库中的数据
【发布时间】:2018-10-15 07:05:03
【问题描述】:

我试图在折线图中显示 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>  

【问题讨论】:

    标签: php mysql linechart canvasjs


    【解决方案1】:

    x-value 可以是数字或日期时间值。但在你的情况下,它似乎是一个字符串。在您的情况下,您可以使用axis label,而不是 x-value。

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

    【讨论】:

      【解决方案2】:

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

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

      到

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多