简体   繁体   English

使用PHP和mysql的morrisJS条形图填充

[英]morrisJS Bar chart population using PHP and mysql

I'm struggling with getting data to populate my MorrisJS bar chart using data from my mySQL database. 我正在努力使用来自mySQL数据库的数据来获取数据以填充MorrisJS条形图。 Could anyone assist in getting it to display on my bar chart? 有人可以协助将其显示在我的条形图上吗?

Below is my current code and pages. 以下是我当前的代码和页面。

    <?php
$link = mysql_connect('127.0.0.1', 'root', 'password')
   or die('Could not connect: ' . mysql_error());

mysql_select_db('database') or die('Could not select database');

$dataArray=array();

//get data from database
$sql="SELECT * FROM table";
$result = mysql_query($sql) or die('Query failed: ' . mysql_error());
if ($result) {
  while ($row = mysql_fetch_assoc($result)) {
      $Version=$row["Version"];
      $Live=$row["Live"];
      $Preprod=$row["Preprod"];
      //add to data areray
      $dataArray[$Version]=$count;
  }
}
?>

index.php (main page) index.php(主页)

div id="morris-bar-chart"></div>
  <?php include ('database.php') ?>

<script src="../js/morris-data.js"></script> <----- script on index.php page linking to morris chart page.

morris.js page morris.js页面

 Morris.Bar({
    element: 'morris-bar-chart',
    data: [{


while ($row = mysql_fetch_assoc($result)) {
        y:<?=$row['versions']?>,
        Live:<?=$row['Live']?>,
        PreProd: <?=$row['Preprod']?>
    }, {

          ],
    xkey: 'y',
    ykeys: ['Live', 'PreProd'],
    labels: ['Live', 'PreProd'],
    hideHover: 'auto',
    resize: true
});

Hope this all makes sense. 希望这一切都有道理。 Thanks in advance. 提前致谢。

I think the while is the problem. 我认为这是问题所在。

Morris expects data line-wise, eg { y: '2007', a: 75, b: 65 }, 莫里斯(Morris)期望数据按行排列,例如{ y: '2007', a: 75, b: 65 },

I've adjusted the PHP code part in the morris.js script a bit. 我已经morris.js调整了morris.js脚本中的PHP代码部分。

  • Normally, you can not execute PHP within a JS file. 通常,您无法在JS文件中执行PHP。 Except, when you added a rule to the server, that JS files should be processed by PHP, too. 除了向服务器添加规则外,JS文件也应由PHP处理。

  • The normal way to fetch the data would be an AJAX query to a PHP script, which returns the json_encoded($result) . 提取数据的通常方法是对PHP脚本进行AJAX查询,该查询返回json_encoded($result) And then add it to Morris with the setData() function. 然后使用setData()函数将其添加到Morris中。

  • But this should work, too: generate the JS file by PHP. 但这也应该起作用:通过PHP生成JS文件。 You could rename the morris-data.js to morris-data.js.php . 您可以将morris-data.js重命名为morris-data.js.php The file needs <?php header("Content-type: application/javascript"); ?> 该文件需要<?php header("Content-type: application/javascript"); ?> <?php header("Content-type: application/javascript"); ?> at the top. <?php header("Content-type: application/javascript"); ?>在顶部。 And then adjust your <script include to match the new filename. 然后调整您的<script include以匹配新文件名。

File morris-data.js.php 文件morris-data.js.php

<?php header("Content-type: application/javascript"); ?>

Morris.Bar({
  element: 'morris-bar-chart',
  data: [        
  <?php 
      while ($row = mysql_fetch_assoc($result)) {

        // output a line like:
        // { y: '123', Live: 123,  PreProd: 123 },
        sprintf(
            "{ y: '%s', Live: %s, PreProd: %s },",
            $row['version'], 
            $row['Live'],
            $row['Preprod']
        );
      } 
  ?>
  ],
  xkey: 'y',
  ykeys: ['Live', 'PreProd'],
  labels: ['Live', 'PreProd'],
  hideHover: 'auto',
  resize: true
});

that should render something like (see also the bar chart example ) 应该呈现出类似效果(另请参见条形图示例

data: [       
    { y: '1.2.3', Live: 20,  PreProd: 23 },
    { y: '1.2.4', Live: 30,  PreProd: 24 },
    { y: '1.2.5', Live: 40,  PreProd: 25 },
    { y: '1.2.6', Live: 50,  PreProd: 26 }
],

(I'm not sure what the last part in the database.php file is supposed to do. Maybe you can drop that (the code inside the if($result) ). But maybe its for calculations..) (我不确定database.php文件的最后一部分应该做什么。也许您可以删除它( if($result)内部的代码)。但是也许是为了计算..)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM