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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作