dream07769 2016-07-17 17:32
浏览 79
已采纳

如何按日期为Google图表迭代数据库值

I have seen similar questions but still do not understand how it woks.. I have this while loop looping my DB for dates, leads and sold. For each date in the DB I would like to show the daily leads and sold for each date in the DB in a line chart.

$sql = "SELECT * FROM customers WHERE source = 'website' ORDER BY date ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
         $sold = $row['sold'];
         $visit= $row['visit'];
         $date= $row['date'];
    }else{

    }       
}

Here is the chart script -

<script type="text/javascript">

  google.charts.setOnLoadCallback(drawChartl);

  function drawChartl() {
    var data = google.visualization.arrayToDataTable([
      ['Date', 'Leads', 'Sold'],
        ['1st',  6,       2],
        ['2nd',  3,       1],
        ['3rd',  2,       3],
    ]);

    var options = {
      title: 'Internet Performance',
      curveType: 'function',
      legend: { position: 'top' }
    };

    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

    chart.draw(data, options);
  }
</script>


<div id="curve_chart" style="width: 1900px; height: 500px"></div>
  • 写回答

1条回答 默认 最新

  • doucang2871 2016-07-17 19:33
    关注

    see following snippet...

    need an array that holds all the data --> $json

    then add each row to $json --> $dataRow

    then write the result to the javascript --> data.addRows(<?= $jsonstring ?>);

    try something like this, probably need to format the date too...

    <?php
      $json = array();
      $sql = "SELECT * FROM customers WHERE source = 'website' ORDER BY date ASC";
      $result = $conn->query($sql);
      if ($result->num_rows > 0) {
          while($row = $result->fetch_assoc()) {
              $dataRow = array(
                  $row['date'],
                  $row['visit'],
                  $row['sold']
              );
              array_push($json, $dataRow);
          }
      }
    
      $jsonstring = json_encode($json);
    ?>
    
    <script src="https://www.gstatic.com/charts/loader.js"></script>
    <script type="text/javascript">
      google.charts.load('current', {
        'callback': function () {
          var data = google.visualization.arrayToDataTable([
            [{type: 'string', label: 'Date'}, {type: 'number', label: 'Leads'}, {type: 'number', label: 'Sold'}]
          ]);
    
          data.addRows(<?= $jsonstring ?>);
    
          var options = {
            title: 'Internet Performance',
            curveType: 'function',
            legend: { position: 'top' }
          };
    
          var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));
          chart.draw(data, options);
        },
        'packages': ['corechart']
      });
    </script>
    
    <div id="curve_chart" style="width: 1900px; height: 500px"></div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面