使用Ajax / PHP获取Google图表的每小时,每周,每月数据

时间:2018-08-20 06:37:33

标签: javascript php ajax mysqli google-visualization

我正在使用Google图表从表格中获取数据,并将其显示在历史记录页面的图表中。

当我尝试获取每小时,每周和每月数据的图表时,它显示的是空值。

enter image description here

图像是当我每小时单击一次时,它发出警报并且不显示任何图表。

history.php

 google.charts.setOnLoadCallback(Hourly);
function Hourly() {
  <?php $currentvalue =''; ?>
  var currentvalue = '';
   currentvalue = document.getElementById('hiddentabvalue').value;
   if(currentvalue == "Oxygen") {
      <?php $currentvalue = 'Oxygen';?>
      alert("Oxygen");
   }

   if(currentvalue == "Nitrogen") {
       <?php $currentvalue = 'Nitrogen';?>
      alert("Nitrogen");
   } 

    var jsonData = $.ajax({
          url: "getData.php",
          type: "POST",
          dataType: 'json',
          data: {'currentvalue': currentvalue},
          success: function (currentvalue) {
            console.log(currentvalue);
            alert("hie");
          },
          error: function(request, status, error){
          alert("Error: Not working");
        }
    });
    var options= {
      chart: {
      title: 'Anesthesia',
      }
    };
      // Create our data table out of JSON data loaded from server.
    var data = new google.visualization.DataTable(jsonData);
    data.addColumn({ type: 'string', id: 'Floors' });
    data.addColumn({ type: 'number', id: 'Anesthesia(Total)' });
    data.addColumn({ type: 'number', id: 'Anesthesia(used)' });
    data.addColumn({ type: 'number', id: 'Anesthesia(remaining)' });
      // Instantiate and draw our chart, passing in some options.
    var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
    chart.draw(data, options);
    }
 <div class="container-fluid">
  <div class="row"> 
    <div class="col-md-12">
      <div class="col-md-6" id="fragment-1">
         <br/>
         <ul class="WinnerButton first_check">      
        <li role="presentation" class="active">
            <a href="#Hourly" method="POST" aria-controls="home" class="btn btn-md WinnerButton" role="tab" onclick="Hourly()" data-toggle="tab" data-bind="click: StatisticalHourly">
                Hourly
            </a>
        </li>
        <li role="presentation">
            <a href="#Weekly" id="oxygen" aria-controls="profile" class="btn btn-md WinnerButton" role="tab" onclick="weekly()" data-toggle="tab" data-bind="    click: StatisticalWeekly">
                WEEKLY
            </a>
        </li>
        <li role="presentation">
            <a href="#Monthly" id="oxygen" aria-controls="messages" class="btn btn-md WinnerButton" role="tab" data-toggle="tab" onclick="monthly()" data-bind="click: StatisticalMonthly">
                MONTHLY
            </a>

        </li>
        </ul>

        <div id="columnchart_material" style="width: 400px; height: 300px;"></div>
      </div>

getData.php

    <?php 
header("Content-Type: application/json", true);
// This is just an example of reading server side data and sending it to the client.
// It reads a json formatted text file and outputs it.

// $string = file_get_contents("sampleData.json");
// echo $string;
            $connect = mysqli_connect("localhost", "root", "", "test2");

            $date = strtotime('now') - 3600;  
            $week = strtotime('now') - 604800;  
            $month = strtotime('now') - 2592000 ; 

            $currentvalue = $_POST['currentvalue'];
            $sql1 = "SELECT total,SUM(total) AS sum_sales FROM history WHERE chart_date > 
             DATE_SUB(NOW(), INTERVAL 1 HOUR) AND asset_type = '".$currentvalue."' AND department='Anesthesia' ";
            $result = mysqli_query($connect, $sql1); 
            $sales_query = $connect->query($sql1);
            $sales_row = $sales_query->fetch_assoc();

            $sql1 = "SELECT total,SUM(expenses) AS sum_expenses FROM history WHERE chart_date > 
            DATE_SUB(NOW(), INTERVAL 1 HOUR) AND asset_type = '".$currentvalue."' AND department='Anesthesia'";
            $expense_query = $connect->query($sql1);
            $expense_row = $expense_query->fetch_assoc();
            $profit = $sales_row['sum_sales'] - $expense_row['sum_expenses'];
            //displaying the needed data
            while($row = mysqli_fetch_assoc($result)) {

                echo '['.$sales_row['total'].','.$sales_row['sum_sales'].', '.$expense_row['sum_expenses'].', '.$profit.'],';
                echo "hie";
                echo json_encode($row);
            }
?>

0 个答案:

没有答案
相关问题